【python】列表list

列表排序,listB = sorted(listA, key=None, reverse=False) 

listA = ['b','aaa','0','ac','cf','9tt','cdghg','','zyfttt'] # 字符串列表,[str]
print(sorted(listA)) #默认升序排序
print(sorted(listA,reverse=True)) #reverse=True,降序排序
print(sorted(listA,key=lambda x:len(x))) #按字符长度升序排序

listA = [4,6,1,2,-8,6,7.1,7.2,-9,0,12,3.1,-3,5,9] # 数字列表,[number]
print(sorted(listA)) #默认升序排序
print(sorted(listA,reverse=True)) #reverse=True,降序排序
print(sorted(listA,key=abs)) #按数值绝对值升序排序
print(sorted(listA,key=lambda x:x**2)) #按表达式计算值升序排序

listA = [('a',3),('b',1),('c',5),('d',0)] #元组列表,[tuple]
listB = sorted(listA,key=lambda x:x[1]) #元组第二个值排序
print(listB)

listA = [{'Name': 'Runoob', 'Age': 7},{'Name': 'Runoob', 'Age': 19},{'Name': 'Runoob', 'Age': 58},{'Name': 'Runoob', 'Age': 2}]#字典列表
listB = sorted(listA,key=lambda x:x['Age']) #按dict[key]值升序排序
print(listB)


>>> listA = [(0,1),(0,0),(1,2),(2,3),(3,3),(3,1)]
# 先以元组第一个值排序,再以第二个值排序
>>> listB = sorted(listA,key=lambda x:(x[0],x[1]))
>>> listC = sorted(listA,key=lambda x:(x[1],x[0]))
>>> print(listB)
[(0, 0), (0, 1), (1, 2), (2, 3), (3, 1), (3, 3)]
>>> print(listC)
[(0, 0), (0, 1), (3, 1), (1, 2), (2, 3), (3, 3)]
>>> 

统计,统计元素在listA中个数,{obj:listA.count(obj)}

text = 'qazqaznnn844999'
listA = list(text)
count_obj = {obj:listA.count(obj) for obj in listA}
print(count_obj)

 listB=listA,listB=listA.copy()的区别,copy()复制

>>> listA = ['r','t','g','h','j','k','p']
>>> listB = listA
>>> listC = listA.copy()
>>> print(listA)
['r', 't', 'g', 'h', 'j', 'k', 'p']
>>> print(listB)
['r', 't', 'g', 'h', 'j', 'k', 'p']
>>> print(listC)
['r', 't', 'g', 'h', 'j', 'k', 'p']
>>> listA.remove('h')
>>> print(listA)
['r', 't', 'g', 'j', 'k', 'p']
>>> print(listB)
['r', 't', 'g', 'j', 'k', 'p']
>>> print(listC)
['r', 't', 'g', 'h', 'j', 'k', 'p']
>>> listA.append('mn')
>>> print(listA)
['r', 't', 'g', 'j', 'k', 'p', 'mn']
>>> print(listB)
['r', 't', 'g', 'j', 'k', 'p', 'mn']
>>> print(listC)
['r', 't', 'g', 'h', 'j', 'k', 'p']
>>> 

列表,从列表中取N个值,输出所有组合

>>> listA = [21,2,3,5,34,19,53]
>>> from itertools import combinations
>>> listB = list(combinations(listA,3))
>>> print(listB)
[(21, 2, 3), (21, 2, 5), (21, 2, 34), (21, 2, 19), (21, 2, 53), (21, 3, 5), (21, 3, 34), (21, 3, 19), (21, 3, 53), (21, 5, 34), (21, 5, 19), (21, 5, 53), (21, 34, 19), (21, 34, 53), (21, 19, 53), (2, 3, 5), (2, 3, 34), (2, 3, 19), (2, 3, 53), (2, 5, 34), (2, 5, 19), (2, 5, 53), (2, 34, 19), (2, 34, 53), (2, 19, 53), (3, 5, 34), (3, 5, 19), (3, 5, 53), (3, 34, 19), (3, 34, 53), (3, 19, 53), (5, 34, 19), (5, 34, 53), (5, 19, 53), (34, 19, 53)]
>>> 

列表,求列表中所有数值的最大公约数

def gcd(a, b):
    """求最大公约数函数"""
    if a < b:
        a, b = b, a
    while b != 0:
        temp = a % b
        a = b
        b = temp
    return a

listA = [32,16,88,44,256,64]
# 求N个数最大公约数
while len(listA) != 1:
    a = listA.pop()
    b = listA.pop()
    c = gcd(a, b)
    listA.append(c)

print(listA[0])

两个list合并为键值对:

# !/usr/bin/env python
# -*-coding:utf-8 -*-
list1 = ["ID","name","labels","result","remark"]
list2 = ["T-8956","AS_20220118.json","漏检","PASS","全程漏检"]
print(list1)
print(list2)

my_dict = dict(zip(list1, map(str, list2)))
print(my_dict)
my_dict = {k: str(v) for k, v in zip(list1, list2)}
print(my_dict)

for z in zip('key','value'):
    print(z)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值