简体   繁体   中英

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

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)

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

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 .

Python's list.count method will do a simple comparison of the passed in value to try to count the number of instances. However, a Cell and a str won't ever be equal. In fact, xldr 's Cell class doesn't even define an __eq__ method .

Instead of calling count on the list , you might instead filter the list of Cell s and take the length of that list:

c = len([cell for cell in sheet.col(x) if cell.value == 'GhostKumi'])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM