list.append(obj) #在列表末尾添加新的对象
list1 = ['Google', 'Runoob', 'Taobao']
list1.append('Baidu')
print ("更新后的列表 : ", list1)
更新后的列表 : ['Google', 'Runoob', 'Taobao', 'Baidu']
list.count(obj) #统计某个元素在列表中出现的次数
aList = [123, 'Google', 'Runoob', 'Taobao', 123];
print ("123 元素个数 : ", aList.count(123))
print ("Runoob 元素个数 : ", aList.count('Runoob'))
123 元素个数 : 2
Runoob 元素个数 : 1
list.extend(seq) #在列表末尾一次性追加另一个序列中的多个值(用新列表扩展原来的列表)
list1 = ['Google', 'Runoob', 'Taobao']
list2=list(range(5)) # 创建 0-4 的列表
list1.extend(list2) # 扩展列表
print ("扩展后的列表:", list1)
扩展后的列表: ['Google', 'Runoob', 'Taobao', 0, 1, 2, 3, 4]
list.index(obj) #从列表中找出某个值第一个匹配项的索引位置
list1 = ['Google', 'Runoob', 'Taobao'] print ('Runoob 索引值为', list1.index('Runoob')) print ('Taobao 索引值为', list1.index('Taobao')) Runoob 索引值为 1 Taobao 索引值为 2
list.insert(index, obj) 将#对象插入列表
list1 = ['Google', 'Runoob', 'Taobao'] list1.insert(1, 'Baidu') print ('列表插入元素后为 : ', list1) 列表插入元素后为 : ['Google', 'Baidu', 'Runoob', 'Taobao']
list.pop([index=-1]]) #移除列表中的一个元素(默认最后一个元素),并且返回该元素的值
list1 = ['Google', 'Runoob', 'Taobao'] list1.pop() print ("列表现在为 : ", list1) list1.pop(1) print ("列表现在为 : ", list1) 列表现在为 : ['Google', 'Runoob'] 列表现在为 : ['Google']
list.remove(obj) #移除列表中某个值的第一个匹配项
list1 = ['Google', 'Runoob', 'Taobao', 'Baidu'] list1.remove('Taobao') print ("列表现在为 : ", list1) list1.remove('Baidu') print ("列表现在为 : ", list1) 列表现在为 : ['Google', 'Runoob', 'Baidu'] 列表现在为 : ['Google', 'Runoob']
list.reverse() #反向列表中元素
list1 = ['Google', 'Runoob', 'Taobao', 'Baidu'] list1.reverse() print ("列表反转后: ", list1) 列表反转后: ['Baidu', 'Taobao', 'Runoob', 'Google']
list.sort(cmp=None, key=None, reverse=False) #对原列表进行排序
1.字符串排序 def list_sort_string(): list=["delphi","Delphi","python","Python","c++","C++","c","C","golang","Golang"] list.sort() #按字典顺序升序排列 print("升序:",list) list.sort(reverse=True) #按降序排列 print("降序:",list) 排序结果: 升序: ['C', 'C++', 'Delphi', 'Golang', 'Python', 'c', 'c++', 'delphi', 'golang', 'python'] 降序: ['python', 'golang', 'delphi', 'c++', 'c', 'Python', 'Golang', 'Delphi', 'C++', 'C'] 2.数值型排序 def list_sort_number(): list=[30,40,10,50,50.1,80,60,100,90] list.sort() print("升序:",list) list.sort(reverse=True) print("降序:",list) 排序结果: 升序: [10, 30, 40, 50, 50.1, 60, 80, 90, 100] 降序: [100, 90, 80, 60, 50.1, 50, 40, 30, 10] 3.根据列表中类对象的属性排序 class element(object): def __init__(self,id="",name=""): self.id=id self.name=name def __lt__(self, other): # override <操作符 if self.id<other.id: return True return False def __str__(self): # override __str__ return "id={0},name={1}".format(self.id,self.name) def sort_by_attribute(): list=[element(id="130",name="json"), element(id="01",name="jack"),element(id="120",name="tom")] list.sort() for item in list: print(item) 由于 list.sort() 函数在排序时,使用的是小于号对比,所以自定义的数据类型需要 override __lt__(小于) 函数才能实现排序。 根据 element 的 id 属性排序 排序列的结果: id=01,name=jack id=120,name=tom id=130,name=json 4.根据列表中元素的长度排序 def list_sort_by_length(): list=["delphi","Delphi","python","Python","c++","C++","c","C","golang","Golang"] list.sort(key=lambda ele:len(ele)) #按元素长度顺序升序排列 print("升序:",list) list.sort(key=lambda ele:len(ele),reverse=True) #按降序排列 print("降序:",list) 借助于 lambda 表达式,计算 list 列表中的元素的长度,根据元素的长度进行排序。 排序的结果: 升序: ['c', 'C', 'c++', 'C++', 'delphi', 'Delphi', 'python', 'Python', 'golang', 'Golang'] 降序: ['delphi', 'Delphi', 'python', 'Python', 'golang', 'Golang', 'c++', 'C++', 'c', 'C'] 5.根据列表中元素的多个属性进行排序: def two_d_list_sort(): list=[ ["1","c++","demo"], ["1","c","test"], ["2","java",""], ["8","golang","google"], ["4","python","gil"], ["5","swift","apple"] ] list.sort(key=lambda ele:ele[0])# 根据第1个元素排序 print(list) list.sort(key=lambda ele:ele[1]) #先根据第2个元素排序 print(list) list.sort(key=lambda ele:ele[1]+ele[0]) #先根据第2个元素排序,再根据第1个元素排序 print(list) 同样借助于 lambda 表达式完成,当然也可以定义一个与 labmda 具有相同意义的函数实现排序。 排序结果: [['1', 'c++', 'demo'], ['1', 'c', 'test'], ['2', 'java', ''], ['4', 'python', 'gil'], ['5', 'swift', 'apple'], ['8', 'golang', 'google']] [['1', 'c', 'test'], ['1', 'c++', 'demo'], ['8', 'golang', 'google'], ['2', 'java', ''], ['4', 'python', 'gil'], ['5', 'swift', 'apple']] [['1', 'c++', 'demo'], ['1', 'c', 'test'], ['8', 'golang', 'google'], ['2', 'java', ''], ['4', 'python', 'gil'], ['5', 'swift', 'apple']] 6.动态的根据用户指定的索引进行排序: def two_d_list_sort2(sort_index="0,1,2"): # 动态的根据传入的元素索引进行排序 list=[ ["1","c++","demo"], ["1","c","test"], ["2","java",""], ["8","golang","google"], ["4","python","gil"], ["5","swift","apple"] ] key_set="" for item in sort_index.split(","): key_set+="ele["+item+"]+" key_set=key_set.rstrip("+") list.sort(key=lambda ele:eval(key_set)) print("排序索引:",sort_index,list) if __name__=="__main__": two_d_list_sort2("0") two_d_list_sort2("1") two_d_list_sort2("2") two_d_list_sort2("1,0") 有时候,在写代码之前,并不知道根据二维表的哪几列排序,而是在程序运行的时候根据输入或配置决定的,为了解决这个动态根据多个列或属性排序的问题,借助了 eval() 函数,eval() 函数能够把字符串编译成 python 代码并运行,从而达到动态根据多个列或属性排序的目的。 排序结果: 排序索引: 0 [['1', 'c++', 'demo'], ['1', 'c', 'test'], ['2', 'java', ''], ['4', 'python', 'gil'], ['5', 'swift', 'apple'], ['8', 'golang', 'google']] 排序索引: 1 [['1', 'c', 'test'], ['1', 'c++', 'demo'], ['8', 'golang', 'google'], ['2', 'java', ''], ['4', 'python', 'gil'], ['5', 'swift', 'apple']] 排序索引: 2 [['2', 'java', ''], ['5', 'swift', 'apple'], ['1', 'c++', 'demo'], ['4', 'python', 'gil'], ['8', 'golang', 'google'], ['1', 'c', 'test']] 排序索引: 1,0 [['1', 'c++', 'demo'], ['1', 'c', 'test'], ['8', 'golang', 'google'], ['2', 'java', ''], ['4', 'python', 'gil'], ['5', 'swift', 'apple']]
list.clear() #清空列表
list1 = ['Google', 'Runoob', 'Taobao', 'Baidu'] list1.clear() print ("列表清空后 : ", list1) 列表清空后 : []
list.copy() #复制列表
list1 = ['Google', 'Runoob', 'Taobao', 'Baidu'] list2 = list1.copy() print ("list2 列表: ", list2) list2 列表: ['Google', 'Runoob', 'Taobao', 'Baidu']
copy()和直接=赋值的区别: a=[0,1,2,3,4,5] b=a c=a.copy() del a[1] ''' 各变量值为: a=[0, 2, 3, 4, 5] b=[0, 2, 3, 4, 5] c=[0, 1, 2, 3, 4, 5] ''' b.remove(4) ''' 各变量值为: a=[0, 2, 3, 5] b=[0, 2, 3, 5] c=[0, 1, 2, 3, 4, 5] ''' c.append(9) ''' 各变量值为: a=[0, 2, 3, 5] b=[0, 2, 3, 5] c=[0, 1, 2, 3, 4, 5, 9] ''' 可以看出,使用=直接赋值,是引用赋值,更改一个,另一个同样会变, 例子中的a,b改变两次都影响到了对方 copy() 则顾名思义,复制一个副本,原值和新复制的变量互不影响 「a,c」
以上示例参考与(http://www.runoob.com/python3)
浙公网安备 33010602011771号