zoukankan      html  css  js  c++  java
  • 第十一天学习:练习题

    1. 把一个数字的list从小到大排序,然后写入文件,然后从文件中读取出来文件内容,然后反序,在追加到文件的下一行中。
     
    #!/usr/bin/python
    l1 = [1, 23, 22, 5, 65] 
    l2 = sorted(l1)
    with open('1.txt', 'wb') as fd: 
        l3 = [str(i) for i in l2] 
        str1 = ' '.join(l3)
        fd.write(str1)
    with open('1.txt', 'ab+') as fd: 
        content = fd.read()
        l4 = [i for i in content.split(' ')]
        print(l4)
        l4.reverse()
        str2 = ' '.join(l4)
        print(str2)
        fd.write('
    ' + str2 + '
    ')
    

      

    2. 分别把 string, list, tuple, dict写入到文件中
     
    #!/usr/bin/python
    str1 = 'hello world'
    list1 = ['a', 'b', 'c', 'd']
    tuple1 = ('123', '456', 'abc', 'hhh')
    dic1 = {'name':'xiaoming', 'sex':'F', 'age':16}
    with open('2.txt', 'ab+') as fd: 
        fd.write(str1 + '
    ')
        list2 = ' '.join(list1)
        fd.write(list2 + '
    ')
        tuple2 = ' '.join(tuple1)
        fd.write(tuple2 + '
    ')
        for k,v in dic1.items():
            fd.write(k + ':' + str(v) + '
    ')
    

      

  • 相关阅读:
    angularJS(5)
    angularJS(4)
    angularJS(3)
    AngularJS(1)
    angularJS(2)
    关于响应式布局
    PHP+JQUEY+AJAX实现分页【转】
    bootscript/javascript组件
    你必须收藏的Github技巧
    关于php的一些小知识!
  • 原文地址:https://www.cnblogs.com/yshan13/p/7758468.html
Copyright © 2011-2022 走看看