Python之list,string,tuple,dict練習題

qq_32154655發表於2017-10-31

Python之list,string,tuple,dict練習題

#!/usr/bin/env python
# -*- coding:utf8 -*-
# @Time     : 2017/10/30 10:11
# @Author   : hantong
# @File     : threed_week.py

import codecs

#對列表排序
list = [22,4,6,8,10,334,779,234,54,666]
list.sort()
print(list)

#寫入列表到檔案

with open("txt","w") as f:

    f.write('\n')
    f.write(str(list))

#追加反序到檔案下一行

with open('txt','r') as f:
    t2 = f.read()
    print(t2)
    list.reverse()
    with open("txt",'a') as f:
         f.write('\n')
         f.write(str(list))
    print(list)
#分別把stringlisttuplesict寫入到文章中
string = 'akgnehkkd123'
tuple = ('a','r','hhh','fe','hty')
list = [23,'ff',456,'ghj','eryi']
dict = {'a':24,'d':56,'cd':89,'b':65}

#string寫入到txt3檔案中
with open('txt3','w') as f:
     f.write('\n')
     f.write(string)

#將list寫入到txt3檔案中
with open('txt3','a') as f:
     f.write('\n')
     f.write(str(list))

#tuple寫入到txt3檔案中
with open('txt3','a') as f:
     f.write('\n')
     f.write(str(tuple))

#dict寫入到txt3檔案中
with open('txt3','a') as f:
     f.write('\n')
     f.write(str(dict))
     







相關文章