3

如果我有这个字符串列表:

['fsuy3,fsddj4,fsdg3,hfdh6,gfdgd6,gfdf5',
'fsuy3,fsuy3,fdfs4,sdgsdj4,fhfh4,sds22,hhgj6,xfsd4a,asr3'] 

(大名单)

如何删除出现在少于 1% 和超过 60% 的字符串中的所有单词?

4

3 回答 3

8

您可以使用collections.Counter

counts = Counter(mylist)

接着:

newlist = [s for s in mylist if 0.01 < counts[s]/len(mylist) < 0.60]

(在 Python 2.x 中使用float(counts[s])/len(mylist)


如果您在谈论逗号分隔的单词,那么您可以使用类似的方法:

words = [l.split(',') for l in mylist]

counts = Counter(word for l in words for word in l)

newlist = [[s for s in l if 0.01 < counts[s]/len(mylist) < 0.60] for l in words]
于 2013-08-08T15:36:59.557 回答
1

直截了当的解决方案

occurrences = dict()
for word in words:
  if word not in occurrences:
     occurrences[word] = 1
  else:
     occurrences[word] += 1

result = [word for word in words 0.01 <= occurrences[word] /len(words) <= 0.6]
于 2013-08-08T15:37:46.937 回答
0

我猜你想要这个:

    from collections import Counter,Set

# break up by ',' and remove duplicate words on each line
    st = [set(s.split(',')) for s in mylist]

# Count all the words
    count = Counter([word for line in st for word in line])

# Work out which words are allowed
    allowed = [s for s in count if 0.01 < counts[s]/len(mylist) < 0.60]

#For each row in the original list. If the word is allowed then keep it
    result = [[w for w in s.split(',') if w in allowed] for s in mylist]

    print result
于 2013-08-08T16:06:44.300 回答