[英]How do I count amount of X mentioned?
I want to count how many times someone has been mentioned if they've been mentioned. 我想计算提及某人的次数。
I've tried to count the user with visually the most amount of mentions (this is a twitter excel file) being GhostKumi with the code below 我试图用可视化方式将用户数最多的提及(这是一个Twitter excel文件)计数为GhostKumi,并带有以下代码
s = sheet.col(19)
c = s.count('GhostKumi')
print(c)
Although it returns with 虽然返回
0
Which isn't quite right considering; 考虑到这不太正确;
mentions
GhostKumi
GhostKumi
MasryLando
CamUnivMuseums
GhostKumi
CamUnivMuseums
CamUnivMuseums
GhostKumi
GhostKumi
GhostKumi
CamUnivMuseums
This is the mentions column. 这是提及专栏。
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)
This is the whole code (which shows mentions usernames and the amount of retweets the user has on a pulled thread from twitter) 这是完整的代码(显示提及的用户名和用户在Twitter提起的线程上进行的转发数量)
The outcome as stated above was 0 which isn't right, and I'd also like to find a way to find matches and count then print those 如上所述的结果是0,这是不正确的,我也想找到一种方法来找到匹配项并计数然后打印那些
Assuming the data is this; 假设数据是这样的;
mentions
GhostKumi
GhostKumi
MasryLando
CamUnivMuseums
GhostKumi
CamUnivMuseums
CamUnivMuseums
GhostKumi
GhostKumi
GhostKumi
CamUnivMuseums
For example: 例如:
GhostKumi: 6 mention(s)
CamUnivMuseums: 4 mention(s)
MasryLando: 1 mention(s)
And to state which of those users have the most: 并说明其中哪些用户最多:
GhostKumi: 6 mention(s) (Most)
MasryLando: 1 mention(s) (Least)
Sheet.col
returns a list
of Cell
objects . Sheet.col
返回Cell
对象的list
。
Python's list.count
method will do a simple comparison of the passed in value to try to count the number of instances. Python的list.count
方法将对传入的值进行简单比较,以尝试计算实例数。 However, a Cell
and a str
won't ever be equal. 但是, Cell
和str
永远不会相等。 In fact, xldr
's Cell
class doesn't even define an __eq__
method . 实际上, xldr
的Cell
类甚至没有定义__eq__
方法 。
Instead of calling count
on the list
, you might instead filter the list of Cell
s and take the length of that list: 除了在list
上调用count
,您还可以过滤Cell
的列表并采用该列表的长度:
c = len([cell for cell in sheet.col(x) if cell.value == 'GhostKumi'])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.