在Python中比较两组具有交集的数据

2024-10-03 13:23:39 发布

您现在位置:Python中文网/ 问答频道 /正文

当比较两个集合时,follower\u id和follower\u id,返回的结果似乎是将所有的东西分开。你知道吗

import re
id1 = '[User(ID=1234567890, ScreenName=RandomNameHere), User(ID=233323490,      ScreenName=AnotherRandomName), User(ID=4459284, ScreenName=YetAnotherName)]'
id2 = '[User(ID=1234467890, ScreenName=sdf), User(ID=233323490,  ScreenName=AnotherRandomName), User(ID=342, ScreenName=443)]'

following_id = ', '.join( re.findall(r'ID=(\d+)', id1) )
follower_id = ', '.join( re.findall(r'ID=(\d+)', id2) )

a = list(set(following_id).intersection(follower_id))
print a

结果是[' ', ',', '1', '0', '3', '2', '5', '4', '7', '6', '9', '8']

我希望结果是['233323490','54321'],这是两个集之间匹配的两个id。你知道吗

以下是我的作品:

list1 = [1234567890, 233323490, 4459284, 230, 200, 234, 200, 0002]
list2 = [1234467890, 233323490, 342, 101, 234]
a = list(set(list1).intersection(list2))
print a

结果是[233323490, 234]

这与following\u id和following\u id的数据类型有关吗?你知道吗


Tags: reidlistfollowingjoinid2setuser
2条回答

following_idfollower_id是字符串。当您将一个字符串转换为一个集合时,您将得到每个字符的集合:

>>> set('hello, there')
{' ', 'o', 't', 'e', 'r', 'h', ',', 'l'}

在创建集合时,Python不关心字符串中的逗号或空格。。。它只是遍历字符,将每个字符视为新集合中的一个项。你知道吗

你在找一组弦。所以你需要传递一些包含字符串的东西,然后转换成一个集合。re.findall应该给你一个字符串列表。如果你不把他们连在一起,你应该可以走十字路口,得到你想要的东西:

following_id = re.findall(r'ID=(\d+)', id1)
follower_id = re.findall(r'ID=(\d+)', id2)

a = list(set(following_id).intersection(follower_id))

这是因为你用的是strings,而不是lists

following_id = ', '.join( re.findall(r'ID=(\d+)', id1) )
follower_id = ', '.join( re.findall(r'ID=(\d+)', id2) )
print(following_id) # '1234567890, 233323490, 4459284'
print(follower_id) # '1234467890, 233323490, 342'

您只需使用:

following_id = re.findall(r'ID=(\d+)', id1)
follower_id = re.findall(r'ID=(\d+)', id2)

因为re.findall已经返回了list个匹配项。你知道吗

相关问题 更多 >