python list相关的一些操作

python list相关的一些操作

统计list中出现次数最多的值

#  统计list出现次数最多的值
test= ['a','a','a','a','a',2,3,10,10,10,4]
print(max(set(test),key=test.count))

统计list中出现的值个数

#  统计list出现次数最多的值
from collections import Counter
c = Counter(['a','a','a','a','a',2,3,10,10,10,4])

结果为

Counter({
     'a': 5, 10: 3, 2: 1, 3: 1, 4: 1})

同时迭代多个list

a = ["1", "11"]
b = ["2", "22"]
c = ["3", "33"]
for A, B, C in zip(a, b, c):
     print(A + "|" + B + "|" + C)

注意,在list长度不相同的情况下,只会循环长度最短的list

排列组合

from itertools import combinations
teams = ["a", "b", "c"]
for game in combinations(teams, 2):
    print(game)

结果为

('a', 'b')
('a', 'c')
('b', 'c')

你可能感兴趣的:(python)