简体   繁体   English

如何存储通过“ for statement”变量获得的打印结果?

[英]how can i store the printing result, obtained by “for statement” variable?

I am counting some specific bigram words frequency. 我正在计算一些特定的双字词频率。 and below are the part of my codes. 以下是我的代码的一部分。

sorted_bigrams = sorted(bigrams.items(), key = lambda pair:pair[1], reverse = True)

for bigram, count in sorted_bigrams:
    if bigram == ("interesting", "news"):
        print count

here, I want to store the counting number of the printing result "count", which is the counting number of bigram "interesting, news" 在这里,我要存储打印结果“ count”的计数,这是二元组“有趣,新闻”的计数

how can I do it.. 我该怎么做..

If you want to store the total count: 如果要存储总数:

sorted_bigrams = sorted(bigrams.items(), key = lambda pair:pair[1], reverse = True)

total_count = 0
for bigram, count in sorted_bigrams:
    if bigram == ("interesting", "news"):
        print count
        total_count += count
print total_count

If you want to keep track of the breakdown of the counts: 如果要跟踪计数明细,请执行以下操作:

sorted_bigrams = sorted(bigrams.items(), key = lambda pair:pair[1], reverse = True)

counts = []
for bigram, count in sorted_bigrams:
    if bigram == ("interesting", "news"):
        print count
        counts.append(count)
print counts

if bigrams is a dict storing bigram as key and count as value, then you don't need to sort it in order to get total count as the keys are unique. 如果bigrams是一个将bigram作为键存储并作为值计数的字典,则由于键是唯一的,因此无需对其进行排序即可获得总计数。

if bigrams is list of tuples, you can use collections.Counter to get the total count. 如果bigrams是元组列表,则可以使用collections.Counter获取总计数。

In [30]: bigrams
Out[30]: [(('a', 'b'), 10), (('d', 'f'), 3), (('a', 'c'), 15), (('a', 'b'), 2)]

In [31]: counter = Counter()

In [32]: for key, val in bigrams:
    counter[key] += val
   ....:     

In [33]: counter[('a', 'b')]
Out[33]: 12

暂无
暂无

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

相关问题 如何将方法的return语句存储在变量中? (蟒蛇) - How can I store a method's return statement in a variable? (python) 如何将存储在变量中的函数结果存储为字符串? - How can I store the result of a function stored in variable as a string? 如何将某些内容附加到函数的结果中并将其存储在python中的变量中 - How can I append something to a result of a function and store that in a Variable in python 如何将获取的数据包存储到pkt.show()命令的变量中? - How to Store Obtained Packet into a variable for pkt.show() command? 如何将读取文本文件的结果存储为变量? - How do I store the result from reading a text file as a variable? 所以,基本上我的代码在打印我希望它打印的语句后打印 None 。 我怎样才能阻止这个 None 打印 - So, basically my code is printing None after printing the statement I want it to print. How can I stop this None from printing 您是否可以将if语句分配为变量,以及在计算时如何打印if语句的结果 - Can you assign an if statement as a variable and how do you print the result from an if statement when it is a calculation 如何检查函数是否在“IF Else”语句中返回结果 - How can I check if a function returns a result in an "IF Else" statement 如何将结果存储在 pyhton 的元组中 - How can I store my result in tuple in pyhton 如何在每个 ping 结果中提取平均值并存储在文件中 - how can I extract Average in this each ping result and store in file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM