Python技巧

两个list合并为dict

keys = ['a', 'b', 'c']
values = [1, 2, 3]
z = zip(keys, values)
zipped = dict(z)
print(zipped)

结果为

{
     'a': 1, 'b': 2, 'c': 3}

过滤list

def is_odd(n):
    return n % 2 == 1
newlist = filter(is_odd, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
print(list(newlist))

结果为

[1, 3, 5, 7, 9]

提取list中出现过的值

from collections import Counter
a = [1, 2, 3, 1, 1, 2, 'text', 'rwerw', 'werewre', 'tsdvsdvext', 'vsdvsdtext', 'text', 'text', 'text']
result = list(dict(Counter(a)).keys())
print(result)

结果为

[1, 2, 3, 'text', 'rwerw', 'werewre', 'tsdvsdvext', 'vsdvsdtext']

你可能感兴趣的:(python)