s = ‘i am very very like you and like you‘ dict( [(i, s.split().count(i)) for i in s.split()] ) Out[2]: {‘i‘: 1, ‘am‘: 1, ‘very‘: 2, ‘like‘: 2, ‘you‘: 2, ‘and‘: 1}
set( map(lambda x:(x, s.split().count(x)), s.split()) ) Out[6]: {(‘am‘, 1), (‘and‘, 1), (‘i‘, 1), (‘like‘, 2), (‘very‘, 2), (‘you‘, 2)}
dict( map(lambda x:(x, s.split().count(x)), s.split()) ) Out[7]: {‘i‘: 1, ‘am‘: 1, ‘very‘: 2, ‘like‘: 2, ‘you‘: 2, ‘and‘: 1}
原文地址:https://www.cnblogs.com/douzujun/p/12368652.html
时间: 2024-10-11 16:05:37