gpt4 book ai didi

python - 删除出现在列表中小于 1% 和大于 60% 的所有元素

转载 作者:太空宇宙 更新时间:2023-11-03 12:48:59 26 4
gpt4 key购买 nike

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

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

(大名单)

我怎样才能删除出现在少于 1% 和多于 60% 的字符串中的所有单词?

最佳答案

您可以使用 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]

关于python - 删除出现在列表中小于 1% 和大于 60% 的所有元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18130165/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com