## r+ 模式:    读,并追加,不管前面读多少字符,都是追加到原文件最后
# f = open('rfile',mode='r+',encoding='utf-8')
# content = f.read(6)
# print(f.tell())
# f.write("R+模式")
# print(content)
# f.close()

## r+ 模式:    读,并追加,不管前面读多少字符,都是追加到原文件最后
# f  = open('rfile',mode='r+',encoding='utf-8')
# content = f.read(6)
# print(f.tell())
# f.write("R+模式")
# print(content)
# f.close()

 



# truncaate() ,必须有写入权限

# with 主动关闭文件甸柄,可多打多个文件

# with open('1.txt',encoding='utf-8') as f1, \
# open('2.txt',encoding='utf-8') as f2:
# for x in f1.readlines(): # 缩进 readlines()是一次性工作,读入内存后迭代完成就没有了
# for y in f2.readlines():
# print(x.strip() + y.strip())

# with open('1.txt',encoding='utf-8') as f1, \
# open('2.txt',encoding='utf-8') as f2:
# for x in f1.readlines():
# for y in f2.readline():
# print(x.strip() + y.strip())

# truncaate() ,必须有写入权限

# with  主动关闭文件甸柄,可多打多个文件

# with open('1.txt',encoding='utf-8') as f1, \
#     open('2.txt',encoding='utf-8') as f2:
#     for x in f1.readlines():                            # 缩进   readlines()是一次性工作,读入内存后迭代完成就没有了
#         for y in f2.readlines():
#             print(x.strip() + y.strip())

# with open('1.txt',encoding='utf-8') as f1, \
#     open('2.txt',encoding='utf-8') as f2:
#     for x in f1.readlines():
#         for y in f2.readline():
#             print(x.strip() + y.strip())

 列表的sort()操作:注意事项

key = lambda x: x+1
print(key(15))
ls = [1,5,8,2,3]
ls.sort()
print(ls)

key = lambda  x,y : x+y        # 通过lambda定义了一人匿名函数, x , y 开参列表, :后面的 x+y  是函数体,返回值 是 x + y 的和
print(key(3,5))

# mylist = ['1','5','3','2','7','6','13','11',1]  # 列表中的元素是字符
# mylist.sort()                        # 没有返回值, 直接对原列表进行操作
# print(mylist)      # TypeError: '<' not supported between instances of 'int' and 'str'   不支持对同时含有 字符串 和 整数 的列表进行排序

numList = [1,3,5,8,11,2,4]
numList.sort()
print(numList)                   #  [1, 2, 3, 4, 5, 8, 11]

strList = ['a','1','b','5','14']
strList.sort()
print(strList)               # 输出  ['1', '14', '5', 'a', 'b']

# numAndstr_list = [ 1,8,3,'a','Y']
# numAndstr_list.sort()            #  通过实验,再次验证 进行排序操作时,列表中元素的类型不能是混合型 TypeError: '<' not supported between instances of 'str' and 'int'

list = ['1','11','31','3','5','9']
list.sort()
print(list) # 输出结果 ['1', '11', '3', '31', '5', '9'] 由于列表中的元素是字符串类型 ,sort()操作是字符的ASCII码排序的,并且是从小到大排序的

# 问题来了,如果我想对元素是字符串类型的列表,想按照数字大小的从小到大排序,怎么办呢?

list.sort(key = lambda x: int(x))
print(list) # 输出结果 ['1', '3', '5', '9', '11', '31']

list.sort(key = lambda x:int(x),reverse=True)
print(list) # 输出结果 ['31', '11', '9', '5', '3', '1'] 从大到小排序

[['11', 'YY'], ['2', 'chris'], ['1', 'jiayong']]

# 再次升级 ,对于列表,里面套列表呢?

myList = [ ['1','jiayong'],['2','chris'],['11','YY']]
myList.sort()
print(myList) # 默认是按照列表里的列表元素的第一个元素,按字符串的ASCII码排序的

# myList.sort(key = lambda x: int(x[0])) [['1', 'jiayong'], ['11', 'YY'], ['2', 'chris']]
# print(myList) # [['1', 'jiayong'], ['2', 'chris'], ['11', 'YY']]

myList.sort( key = lambda x: int(x[0]),reverse=True)
print(myList) # [['11', 'YY'], ['2', 'chris'], ['1', 'jiayong']]
 

 

posted on 2018-08-13 16:45  yellwonfin  阅读(154)  评论(0编辑  收藏  举报