リスト[68,31,93,35,10]
(すべての数字は異なります) とリスト(すべての数字は異なりますが、他のリストと重複する可能性があります) がある場合、2 番目のリストが追加される[93,0,22,10,99,33,21,9]
正確な を取得できる必要があります。[68,31,93,35,10,0,22,99,33,21,9]
重複のない最初のリスト。[68,31,35]
また、最初のリストで 2 番目のリストのすべての重複が削除された場所を正確に取得できるようにする必要もあります。出力は常に入力と同じ順序にする必要があります。どうすればいいですか?(シンプルであればワンライナーがいいでしょう。)
6 に答える
4
l1 = [68, 31, 93, 35,10]
l2 = [93, 0, 22, 10, 99, 33, 21,9]
l1 + [x for x in l2 if not x in l1]
# [68, 31, 93, 35, 10, 0, 22, 99, 33, 21, 9]
[x for x in l1 if not x in l2]
# [68, 31, 35]
編集: 長いリストの場合、これらすべてのリスト ルックアップを実行する必要はありません。他の2つのレシピは次のとおりです。
連合:
from collections import OrderedDict
OrderedDict().fromkeys(l1+l2).keys()
# [68, 31, 93, 35, 10, 0, 22, 99, 33, 21, 9]
違い:
s = set(l2)
[x for x in l1 if not x in s]
# [68, 31, 35]
于 2011-07-31T13:29:59.373 に答える
2
l1
入力とを仮定するとl2
、それらの順序付き和集合を次のように計算できます。
l1 + filter(lambda x: x not in l1, l2)
順序差 l1 - l2 を取得するには、次のように記述します。
filter(lambda x: x not in l2, l1)
または、リスト内包表記を使用します。
>>> l1 = [68,31,93,35,10]
>>> l2 = [93,0,22,10,99,33,21,9]
>>> l1 + [el2 for el2 in l2 if el2 not in l1]
[68, 31, 93, 35, 10, 0, 22, 99, 33, 21, 9]
>>> [el1 for el1 in l1 if el1 not in l2]
[68, 31, 35]
非常に大きなリスト (パフォーマンスが問題になる場合) でこれを行う場合は、set
検索を高速化するために を作成します。
>>> sl1 = set(s1)
>>> l1 + [el2 for el2 in l2 if el2 not in sl1]
[68, 31, 93, 35, 10, 0, 22, 99, 33, 21, 9]
>>> sl2 = set(s2)
>>> [el1 for el1 in l1 if el1 not in sl2]
[68, 31, 35]
于 2011-07-31T13:29:09.010 に答える
0
def unique_chain(*iters):
seen = set()
for it in iters:
for item in it:
if item not in seen:
yield item
seen.add(item)
print list(unique_chain([68, 31, 93, 35,10], [93, 0, 22, 10, 99, 33, 21,9]))
于 2011-07-31T13:33:50.090 に答える
0
最初の2つのリストをそのように定義した後、
a = [68,31,93,35,10]
b = [93,0,22,10,99,33,21,9]
これが最初の問題の1行の解決策です。
c = [x for x in a+b if x not in set(a).intersection(set(b))]
そして、2番目の問題へのワンライナー、
d = [x for x in a+b if x not in b]
于 2011-08-01T03:37:51.427 に答える
0
>>> a = [68,31,93,35,10]
>>> b = [93,0,22,10,99,33,21,9]
>>> result= []
>>> temp = a + b
>>> [result.append(x) for x in temp if x not in result]
>>> result
[68, 31, 93, 35, 10, 0, 22, 99, 33, 21, 9]
>>> a = set(a)
>>> b = set(b)
>>> a - b
set([35, 68, 31])
于 2011-07-31T13:42:44.697 に答える
0
多分あなたはOrderedSet
import collections
class OrderedSet(collections.MutableSet):
def __init__(self, iterable, *args, **kwargs):
super(OrderedSet, self).__init__(*args, **kwargs)
self._data = collections.OrderedDict()
self.update(iterable)
def update(self, iterable):
self._data.update((x, None) for x in iterable)
def __iter__(self):
return iter(self._data)
def __contains__(self, value):
return value in self._data
def __len__(self):
return len(self._data)
def __le__(self, other):
if isinstance(other, OrderedSet):
return self._data <= other._data
return super(OrderedSet, self).__le__(other)
def __and__(self, other):
# Overrided by make the order of self the preferred one
if isinstance(other, collections.Set):
return self._from_iterable(value for value in self
if value in other)
return self & set(other)
def __ior__(self, other):
self.update(other)
return self
def add(self, value):
self._data[value] = None
def discard(self, value):
self._data.pop(value, None)
def __repr__(self):
return "%s(%r)" % (type(self).__name__, self._data.keys())
于 2011-07-31T15:36:31.440 に答える