2つのリストを取り、両方に表示される値を見つけたいと思います。
a = [1, 2, 3, 4, 5]
b = [9, 8, 7, 6, 5]
returnMatches(a, b)
[5]
たとえばを返します。
2つのリストを取り、両方に表示される値を見つけたいと思います。
a = [1, 2, 3, 4, 5]
b = [9, 8, 7, 6, 5]
returnMatches(a, b)
[5]
たとえばを返します。
回答:
最も効率的な方法ではありませんが、これを行う最も明白な方法は次のとおりです。
>>> a = [1, 2, 3, 4, 5]
>>> b = [9, 8, 7, 6, 5]
>>> set(a) & set(b)
{5}
順序が重要な場合は、次のようなリスト内包表記を使用できます。
>>> [i for i, j in zip(a, b) if i == j]
[5]
(同じサイズのリストでのみ機能します。これは順序の意味が意味します)。
&
)またはset(a).intersection(b)
リストの理解と同じかそれより速くなります。
set(a) & set(b)
か?
set.intersection()を使用すると、高速で読みやすくなります。
>>> set(a).intersection(b)
set([5])
bool(set(a).intersection(b))
以下のためTrue
かFalse
difference
またはを必要とする可能性があるため、この答えはより柔軟で読みやすくなっていunion
ます。
.intersection()
vsのパフォーマンスに違いはあります&
か?
Lutzのソリューションを示す簡単なパフォーマンステストが最適です。
import time
def speed_test(func):
def wrapper(*args, **kwargs):
t1 = time.time()
for x in xrange(5000):
results = func(*args, **kwargs)
t2 = time.time()
print '%s took %0.3f ms' % (func.func_name, (t2-t1)*1000.0)
return results
return wrapper
@speed_test
def compare_bitwise(x, y):
set_x = frozenset(x)
set_y = frozenset(y)
return set_x & set_y
@speed_test
def compare_listcomp(x, y):
return [i for i, j in zip(x, y) if i == j]
@speed_test
def compare_intersect(x, y):
return frozenset(x).intersection(y)
# Comparing short lists
a = [1, 2, 3, 4, 5]
b = [9, 8, 7, 6, 5]
compare_bitwise(a, b)
compare_listcomp(a, b)
compare_intersect(a, b)
# Comparing longer lists
import random
a = random.sample(xrange(100000), 10000)
b = random.sample(xrange(100000), 10000)
compare_bitwise(a, b)
compare_listcomp(a, b)
compare_intersect(a, b)
これらは私のマシンでの結果です:
# Short list:
compare_bitwise took 10.145 ms
compare_listcomp took 11.157 ms
compare_intersect took 7.461 ms
# Long list:
compare_bitwise took 11203.709 ms
compare_listcomp took 17361.736 ms
compare_intersect took 6833.768 ms
言うまでもなく、人工的なパフォーマンステストは1粒ずつ行う必要set().intersection()
がありますが、答えは少なくとも他のソリューションと同じくらい速く、最も読みやすいので、この一般的な問題の標準的なソリューションになるはずです。
set
既存のlist
ものから新規作成しても、元のものから何も削除されませんlist
。リスト内の重複を処理する特別なロジックが必要な場合は、重複をどのように処理するかを具体的に回答する必要があるため、新しい質問をする必要があると思います。
私は集合ベースの答えを好みますが、これはとにかくうまくいくものです
[x for x in a if x in b]
これを行う最も簡単な方法は、セットを使用することです。
>>> a = [1, 2, 3, 4, 5]
>>> b = [9, 8, 7, 6, 5]
>>> set(a) & set(b)
set([5])
簡単な方法:
list(set(a).intersection(set(b)))
>>> s = ['a','b','c']
>>> f = ['a','b','d','c']
>>> ss= set(s)
>>> fs =set(f)
>>> print ss.intersection(fs)
**set(['a', 'c', 'b'])**
>>> print ss.union(fs)
**set(['a', 'c', 'b', 'd'])**
>>> print ss.union(fs) - ss.intersection(fs)
**set(['d'])**
重複しますか?そうでない場合は、代わりにセットを使用する必要があります。
>>> set([1, 2, 3, 4, 5]).intersection(set([9, 8, 7, 6, 5]))
set([5])
a = [1, 2, 3, 4, 5]
b = [9, 8, 7, 6, 5]
lista =set(a)
listb =set(b)
print listb.intersection(lista)
returnMatches = set(['5']) #output
print " ".join(str(return) for return in returnMatches ) # remove the set()
5 #final output
以下を使用できます。
a = [1, 3, 4, 5, 9, 6, 7, 8]
b = [1, 7, 0, 9]
same_values = set(a) & set(b)
print same_values
出力:
set([1, 7, 9])
ブール値が必要な場合:
>>> a = [1, 2, 3, 4, 5]
>>> b = [9, 8, 7, 6, 5]
>>> set(b) == set(a) & set(b) and set(a) == set(a) & set(b)
False
>>> a = [3,1,2]
>>> b = [1,2,3]
>>> set(b) == set(a) & set(b) and set(a) == set(a) & set(b)
True
次のソリューションは、リストアイテムの任意の順序で機能し、長さが異なる両方のリストをサポートしています。
import numpy as np
def getMatches(a, b):
matches = []
unique_a = np.unique(a)
unique_b = np.unique(b)
for a in unique_a:
for b in unique_b:
if a == b:
matches.append(a)
return matches
print(getMatches([1, 2, 3, 4, 5], [9, 8, 7, 6, 5, 9])) # displays [5]
print(getMatches([1, 2, 3], [3, 4, 5, 1])) # displays [1, 3]
np.intersect1d(list1, list2)
you can | for set union and & for set intersection.
for example:
set1={1,2,3}
set2={3,4,5}
print(set1&set2)
output=3
set1={1,2,3}
set2={3,4,5}
print(set1|set2)
output=1,2,3,4,5
curly braces in the answer.
&
セットでのオペレーターの使用は、SilentGhostによる回答で既に回答されています