twiterAPI/tweepy比较了我收集的两个用户的列表,其中提到了用户ID



所以我看到的是两个用户。我已经收集了他们的100名推特粉丝,并获取了粉丝提到的人的用户ID。我现在有两个推特Id列表,如果其中任何一个与重叠,我想进行比较

elonusermentions=[]
for user in elonFirst100FullUsers:
if not user.protected and user.statuses_count>0:
Elonmentioneduser=user.status.entities["user_mentions"]
for Euser in Elonmentioneduser:
elonusermentions.append(Euser['id'])
elonusermentions
## first 100 elon followers mentioned these people(ID's)

loganusermentions=[]
for user in loganFirst100FullUsers:
if not user.protected and user.statuses_count>0:
loganmentioneduser=user.status.entities["user_mentions"]
for Luser in loganmentioneduser:
loganusermentions.append(Luser['id'])
loganusermentions
## first 100 logan followers mentioned these people(ID's)

我想比较一下这些清单,但不确定该怎么做。到目前为止,我已经尝试过这样的东西。

ELmentions=[]
for user in elonusermentions:
if user in loganusermentions:
ELmentions.append(user)
ELmentions

我总是收到一张空白名单,有人能帮我吗?我对编码还很陌生。

您使用的方法是正确的,并且将为您提供两个列表中相似的元素。

您可以从以下两个列表中找到类似的元素:

你的方法,在列表理解形式-

ELmentions = [user for user in elonusermentions if user in loganusermentions]

另一种是将列表转换为集合并找到它们的交集-

a = set(elonusermentions)
b = set(loganusermentions)
ELmentions = a.intersection(b)

然后,如果您愿意,您可以将ELcontractions转换回列表。

如果生成的列表已经为空,则应该检查它们。还要检查你的if语句,并将菊花放在需要的地方。

相关内容

最新更新