[英]How do I count amount of X mentioned?
我想计算提及某人的次数。
我试图用可视化方式将用户数最多的提及(这是一个Twitter excel文件)计数为GhostKumi,并带有以下代码
s = sheet.col(19)
c = s.count('GhostKumi')
print(c)
虽然返回
0
考虑到这不太正确;
mentions
GhostKumi
GhostKumi
MasryLando
CamUnivMuseums
GhostKumi
CamUnivMuseums
CamUnivMuseums
GhostKumi
GhostKumi
GhostKumi
CamUnivMuseums
这是提及专栏。
for c1, c2, c3 in zip(sheet.col(23), sheet.col(11), sheet.col(19)):
print("username:",c1.value)
print("has:",c2.value, "retweets")
print("and mentioned:",c3.value)
print("***********************")
s = sheet.col(19)
c = s.count('GhostKumi')
print(c)
这是完整的代码(显示提及的用户名和用户在Twitter提起的线程上进行的转发数量)
如上所述的结果是0,这是不正确的,我也想找到一种方法来找到匹配项并计数然后打印那些
假设数据是这样的;
mentions
GhostKumi
GhostKumi
MasryLando
CamUnivMuseums
GhostKumi
CamUnivMuseums
CamUnivMuseums
GhostKumi
GhostKumi
GhostKumi
CamUnivMuseums
例如:
GhostKumi: 6 mention(s)
CamUnivMuseums: 4 mention(s)
MasryLando: 1 mention(s)
并说明其中哪些用户最多:
GhostKumi: 6 mention(s) (Most)
MasryLando: 1 mention(s) (Least)
Sheet.col
返回Cell
对象的list
。
Python的list.count
方法将对传入的值进行简单比较,以尝试计算实例数。 但是, Cell
和str
永远不会相等。 实际上, xldr
的Cell
类甚至没有定义__eq__
方法 。
除了在list
上调用count
,您还可以过滤Cell
的列表并采用该列表的长度:
c = len([cell for cell in sheet.col(x) if cell.value == 'GhostKumi'])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.