gpt4 book ai didi

解析元组的 Pythonic 方式

转载 作者:行者123 更新时间:2023-12-05 08:50:13 30 4
gpt4 key购买 nike

我正在处理一个元组列表,如下所示:

res = [('stori', 'JJ'), ('man', 'NN'), ('unnatur', 'JJ'), ('feel', 'NN'), ('pig', 'JJ'), ('start', 'NN'), ('open', 'JJ'), ('scene', 'NN'), ('terrif', 'NN'), ('exampl', 'NN'), ('absurd', 'JJ'), ('comedi', 'NN'), ('formal', 'JJ'), ('orchestra', 'NN'), ('audienc', 'NN'), ('turn', 'VBP'), ('insan', 'JJ'), ('violent', 'JJ'), ('mob', 'NN'), ('crazi', 'NN'), ('chant', 'JJ'), ('singer', 'NN'), ('unfortun', 'JJ'), ('stay', 'NN'), ('absurd', 'IN'), ('whole', 'JJ'), ('time', 'NN'), ('general', 'JJ'), ('narrat', 'NN'), ('eventu', 'VBP'), ('make', 'VBP'), ('put', 'VB'), ('even', 'RB'), ('era', 'NN'), ('turn', 'NN'), ('cryptic', 'JJ'), ('dialogu', 'NN'), ('would', 'MD'), ('make', 'VB'), ('shakespear', 'JJ'), ('seem', 'JJ'), ('easi', 'JJ'), ('third', 'JJ'), ('grader', 'NN'), ('technic', 'JJ'), ('level', 'NN'), ('better', 'RBR'), ('might', 'MD'), ('think', 'VB'), ('good', 'JJ'), ('cinematographi', 'NN'), ('futur', 'NN'), ('great', 'JJ'), ('vilmo', 'JJ'), ('zsigmond', 'NN'), ('futur', 'NN'), ('star', 'NN'), ('salli', 'NN'), ('kirkland', 'NN'), ('freder', 'NN'), ('forrest', 'JJS'), ('seen', 'VBN'), ('briefli', 'NN')]

我正在寻找一个输出,该输出将显示此元组列表中的名词、动词、形容词和其他单词的数量,符合以下条件:

  • 以NN开头的名词
  • 动词以 VB 或 VP 开头
  • 形容词以JJ开头
  • 其他不符合该条件的

到目前为止我有:

# Create a set of all values that appear
appears = set([x[1] for x in res])

cnt_noun = 0
cnt_adj = 0
cnt_vb = 0
cnt_other = 0

for tpl in res:
if('NN' in tpl[1]):
cnt_noun += 1
elif('JJ' in tpl[1]):
cnt_adj += 1
elif('VB' in tpl[1] or 'VP' in tpl[1]):
cnt_vb += 1
else:
cnt_other += 1

正确显示计数:

cnts = [cnt_noun, cnt_vb, cnt_adj, cnt_other]
for x in cnts:
print(x)

产量

29
7
22
5

重要的是实际返回每个单词标签的总数,因为这将用作大型数据构建序列的一部分。

但是,是否有更 pythonic 的方式来完成同样的事情,代码更少,效率更高?

最佳答案

我可能会使用 collections.Counter:

res = [('stori', 'JJ'), ('man', 'NN'), ...]
counter = Counter([b for a, b in res])
# Counter acts like a dictionary containing e.g. {'JJ': 3, ...}

下面来看一个完整的解决方案:

from collections import Counter

res = [('stori', 'JJ'), ('man', 'NN'), ('unnatur', 'JJ'), ('feel', 'NN'), ('pig', 'JJ'), ('start', 'NN'), ('open', 'JJ'), ('scene', 'NN'), ('terrif', 'NN'), ('exampl', 'NN'), ('absurd', 'JJ'), ('comedi', 'NN'), ('formal', 'JJ'), ('orchestra', 'NN'), ('audienc', 'NN'), ('turn', 'VBP'), ('insan', 'JJ'), ('violent', 'JJ'), ('mob', 'NN'), ('crazi', 'NN'), ('chant', 'JJ'), ('singer', 'NN'), ('unfortun', 'JJ'), ('stay', 'NN'), ('absurd', 'IN'), ('whole', 'JJ'), ('time', 'NN'), ('general', 'JJ'), ('narrat', 'NN'), ('eventu', 'VBP'), ('make', 'VBP'), ('put', 'VB'), ('even', 'RB'), ('era', 'NN'), ('turn', 'NN'), ('cryptic', 'JJ'), ('dialogu', 'NN'), ('would', 'MD'), ('make', 'VB'), ('shakespear', 'JJ'), ('seem', 'JJ'), ('easi', 'JJ'), ('third', 'JJ'), ('grader', 'NN'), ('technic', 'JJ'), ('level', 'NN'), ('better', 'RBR'), ('might', 'MD'), ('think', 'VB'), ('good', 'JJ'), ('cinematographi', 'NN'), ('futur', 'NN'), ('great', 'JJ'), ('vilmo', 'JJ'), ('zsigmond', 'NN'), ('futur', 'NN'), ('star', 'NN'), ('salli', 'NN'), ('kirkland', 'NN'), ('freder', 'NN'), ('forrest', 'JJS'), ('seen', 'VBN'), ('briefli', 'NN')]
mapping = {'NN':'noun', 'JJ':'adjective','VB':'verb','VP':'verb'}
counter = Counter([mapping.get(b[:2], "other") for a, b in res])
print(counter) # Counter({'noun': 29, 'adjective': 22, 'verb': 7, 'other': 5})

关于解析元组的 Pythonic 方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62567325/

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