正则表达式:从字符串列表创建字典

2024-10-01 00:33:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个字符串的大列表,我想创建一个字典了。你知道吗

每一个不同的单词都是一个键,值是单词出现在各种字符串的整个列表中的次数。你知道吗

我对Python还是个新手,有点迷路了。我确信我必须做循环,在循环中我必须:

  1. 检查下一个单词是否重复
  2. 维护迭代器以计算每个单词在字典中存在的次数

如果我先使用set()获取所有唯一的单词,然后循环遍历它们并计算频率,会怎么样?你知道吗

如有任何建议,我将不胜感激

 [u'retw', u'folivi_jochan', u':', u'rt', u'newsycombinator', u':', u'uber',  u'is', u'taking', u'millions', u'of', u'manhattan', u'rides', u'away', u'from',  u'taxis', u'http', u':', u'//t.co/zluyq3f6cc'] [u'retw', u'chr1sa', u':', u'rt',  u'newsycombinator', u':', u'uber', u'is', u'taking', u'millions', u'of',  u'manhattan', u'rides', u'away', u'from', u'taxis', u'http', u':',  u'//t.co/zluyq3f6cc'] [u'retw', u'olutosinfashusi', u':', u'rt', u'newsycombinator', u':', u'uber', u'is', u'taking', u'millions', u'of', u'manhattan', u'rides', u'away', u'from', u'taxis', u'http', u':', u'//t.co/zluyq3f6cc'] [u'retw', u'shakycode', u':', u'rt', u'newsycombinator', u':', u'uber', u'is', u'taking', u'millions', u'of', u'manhattan', u'rides', u'away', u'from', u'taxis', u'http', u':', u'//t.co/zluyq3f6cc'] [u'an', u'interesting', u'read', u'manhattan', u'is', u'the', u'best', u'tv', u'show', u'that', u'hardly', u'anybody', u'is', u'watching', u'http', u':', u'//t.co/psfmauuwfg'] [u'tmr', u'am', u':', u'lunch', u'at', u'the', u'arts', u'!', u'from', u'11-2pm', u'at', u'1935', u'manhattan', u'beach', u'blvd', u'in', u'redondo', u'beach', u'!', u'map', u':', u'http', u':', u'//t.co/x6x2eeijbh'] [u's1', u'was', u'superb', u'.', u'``', u'manhattan', u'is', u'the', u'best', u'tv', u'show', u'that', u'hardly', u'anybody', u'is', u'watching', u"''", u'http', u':', u'//t.co/q6iazmtaam'] [u'taylor', u'swift', u'seen', u'leaving', u'msr', u'studios', u'in', u'manhattan', u'on', u'october', u'07', u',', u'2015', u'in', u'new', u'york', u',', u'new', u'york', u'.', u'http', u':', u'//t.co/3cwxrapr38'] [u'viva', u'a1054665', u'manhattan', u'acc', u'estimated', u'to', u'be', u'7', u'yrs', u'old', u'american', u'staff', u'mix', u',', u'white', u'/', u'brown', u',', u'spayed', u'female', u'...', u'http', u':', u'//t.co/sloopljyxq'] [u'#', u'3d', u'taevision', u"'showroom", u'in', u'the', u'night', u'#', u'porsche', u'996', u"'", u'#', u'automotive', u'#', u'fashion', u'#', u'makeup', u'#', u'ny', u'#', u'nyc', u'#', u'manhattan', u'http', u':', u'//t.co/eftvytqedk']

谢谢


Tags: offromhttpis单词rtcorides
2条回答

另一种方法是使用for循环:

for word in strings:
if word not in dict.keys():
    dict[word]=1
else:
    dict[word] += 1

上面假设string是要迭代的单词列表。你知道吗

对于Python2.7及更高版本,请使用collectionsmodule中的Counter

from collections import Counter
mylist = [u'retw', u'folivi_jochan', u':', u'rt', u'newsycombinator', u':', u'uber', u'is', u'taking', u'millions', u'of', u'manhattan', u'rides', u'away', u'from', u'taxis', u'http', u':', u'//t.co/zluyq3f6cc', u'retw', u'chr1sa', u':', u'rt', u'newsycombinator', u':', u'uber', u'is', u'taking', u'millions', u'of', u'manhattan', u'rides', u'away', u'from', u'taxis', u'http', u':', u'//t.co/zluyq3f6cc', u'retw', u'olutosinfashusi', u':', u'rt', u'newsycombinator', u':', u'uber', u'is', u'taking', u'millions', u'of']
c = Counter(mylist)
print dict(c)
[(u':', 8),
 (u'rt', 3), 
 (u'uber', 3), 
 (u'newsycombinator', 3), 
 (u'of', 3), 
 (u'is', 3), 
 (u'retw', 3), 
 (u'taking', 3), 
 (u'millions', 3), 
 (u'from', 2), 
 (u'//t.co/zluyq3f6cc', 2), 
 (u'manhattan', 2), 
 (u'away', 2),
 (u'http', 2),
 (u'taxis', 2), 
 (u'rides', 2),
 (u'olutosinfashusi', 1),
 (u'chr1sa', 1), 
 (u'folivi_jochan', 1)]

如果有三个单独的列表,请尝试使用来自itertoolschain

one,two,three = [u'retw', u'folivi_jochan', u':', u'rt', u'newsycombinator', u':', u'uber',   u'is', u'taking', u'millions', u'of', u'manhattan', u'rides', u'away', u'from', u'taxis', u'http', u':', u'//t.co/zluyq3f6cc'],[u'retw', u'chr1sa', u':', u'rt', u'newsycombinator', u':', u'uber', u'is', u'taking', u'millions', u'of', u'manhattan', u'rides', u'away', u'from', u'taxis', u'http', u':', u'//t.co/zluyq3f6cc'], [u'retw', u'olutosinfashusi', u':', u'rt', u'newsycombinator', u':', u'uber', u'is', u'taking', u'millions', u'of']
from itertools import chain
from collections import Counter
c=Counter(chain(one,two,three))

Counter是一个高性能类,用于计算迭代表中元素的出现次数。它最常用的()方法返回tuples(element,count)的列表。 这个元组列表可以用来构造dict

相关问题 更多 >