简体   繁体   English

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

[英]Regex: creating a dictionary from a list of strings

I have a large list of strings and I would like to create a dictionary out of this. 我有很多字符串,我想以此创建一个字典。

Every different word is a key and the value is the number of times the word is present in the whole list of various strings. 每个不同的词都是一个键,值是该词在各种字符串的整个列表中出现的次数。

I am new to Python still and am bit lost. 我还是Python的新手,迷路了。 I am sure I have to do the loop, in which I would have to: 我确定我必须执行循环,在该循环中,我必须:

  1. Check if the next word is not a duplicate 检查下一个单词是否重复
  2. maintain iterator to calculate the number of times each word exists in a dictionary 维护迭代器以计算每个单词在词典中存在的次数

What if I use set() first to get all unique words and than loop through them and count the frequency? 如果我先使用set()来获取所有唯一单词,然后遍历它们并计算频率怎么办?

Would be greatly appreciative of any advice 任何建议都将不胜感激

 [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']

Thank you 谢谢

For python 2.7 and above use Counter from the collections module : 对于python 2.7及更高版本,请使用collections 模块中的 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)]

If you have three separate lists try using chain from itertools : 如果您有三个单独的列表,请尝试使用itertools chain

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 is a high performance class for counting occurences of elements in iteratables. Counter是一种高性能类,用于计数可迭代对象中元素的出现。 Its most_common() method returns a list of tuple s (element,count) . 它的most_common()方法返回一个tuple s (element,count)的列表。 This list of tuples can be used to construct a dict 该元组列表可用于构建dict

Alternative approach, using your for loop: 替代方法,使用for循环:

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

Above assumes that string is your list of words that you want to iterate. 上面假设string是您要迭代的单词列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM