因此,我关注的是2个用户。我已经收集了100名他们的twitter追随者,并抓取了追随者提到的人的用户ids。我现在有两个twitter 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
我总是得到一个空白的列表,有人能帮我吗?我对编码是相当陌生的。
发布于 2021-05-05 23:42:40
您使用的方法是正确的,并且将从两个列表中为您提供相似的元素。
可以从两个列表中找到类似的元素,如下所示:
你的方法,以列表理解的形式-
ELmentions = [user for user in elonusermentions if user in loganusermentions]
另一种是将列表转换为集合,并找到它们的交集-
a = set(elonusermentions)
b = set(loganusermentions)
ELmentions = a.intersection(b)
然后,如果您愿意,可以将ELmentions转换回list。
如果生成的列表已经是空的,您应该检查它们。还要检查if语句,并将括号放在需要的地方。
https://stackoverflow.com/questions/67376920
复制相似问题