2016-11-08 18 views
0

2組のfollowing_idとfollower_idを比較すると、結果がすべて分裂しているように見えます。2組のデータをPythonのIntersectionと比較する

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 

これは、私は結果が2つのセット間で一致する2つのIDである['233323490','54321']になりたい[' ', ',', '1', '0', '3', '2', '5', '4', '7', '6', '9', '8']

で生じます。

私のために、次の作品は:

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_idのために、データ型で行い、follower_idする必要がありますか?

+1

'。 '。join()'は単一の文字列を返します。おそらく 'following_id'と' follower_id'の定義でそれを削除すれば、結局、これらの2つが交差点を見つけるためにリストになる必要がありますか? 're.findall()'はすでにリストを返しています – TuanDT

+0

@ Tuan333意味があります。 – New

答えて

1

あなたがlists.joinstringsをない作っているので、これは次のとおりです。

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' 

あなただけを使用する必要があります:re.findallとして

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

すでに試合のlistを返します。

+0

クイックレスポンス!それは理にかなっている。ありがとうございました。 – New

0

およびfollower_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)) 
+0

すばらしい説明!! – New

関連する問題