当比较两个集合时,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的数据类型有关吗?你知道吗
following_id
和follower_id
是字符串。当您将一个字符串转换为一个集合时,您将得到每个字符的集合:在创建集合时,Python不关心字符串中的逗号或空格。。。它只是遍历字符,将每个字符视为新集合中的一个项。你知道吗
你在找一组弦。所以你需要传递一些包含字符串的东西,然后转换成一个集合。
re.findall
应该给你一个字符串列表。如果你不把他们连在一起,你应该可以走十字路口,得到你想要的东西:这是因为你用的是
strings
,而不是lists
:您只需使用:
因为
re.findall
已经返回了list
个匹配项。你知道吗相关问题 更多 >
编程相关推荐