Python 在具有相同值的列表中查找差异
我需要能够找到列表中的差异,除了两个添加的元素之外,这些差异可能彼此具有相同的值 范例Python 在具有相同值的列表中查找差异,python,Python,我需要能够找到列表中的差异,除了两个添加的元素之外,这些差异可能彼此具有相同的值 范例 a = ['cool task', 'b', 'another task', 'j', 'better task', 'y'] b = ['cool task', 'b', 'a task', 'j', 'another task', 'j', 'better task', 'y'] 我的问题是,'a task'和'a task'后面都跟着一个'j' [x for x in b if x not in a]
a = ['cool task', 'b', 'another task', 'j', 'better task', 'y']
b = ['cool task', 'b', 'a task', 'j', 'another task', 'j', 'better task', 'y']
我的问题是,'a task'
和'a task'
后面都跟着一个'j'
[x for x in b if x not in a]
['a task']
由于a
和b
都包含'j'
,因此它将从列表中删除
我该怎么做才能让我最终
['a task', 'j']
对于简单列表-您需要的只是搜索列表中的下一项:
>>> a = ['cool task', 'b', 'another task', 'j', 'better task', 'y']
>>> b = ['cool task', 'b', 'a task', 'j', 'another task', 'j', 'better task', 'y']
>>> c = [[x, b[b.index(x) + 1]] for x in b if x not in a]
>>> c
[['a task', 'j']]
但我认为你实际上是在使用字典或元组
元组:
>>> a = [('cool task', 'b'), ('another task', 'j'), ('better task', 'y')]
>>> b = [('cool task', 'b'), ('a task', 'j'), ('another task', 'j'), ('better task', 'y')]
>>> c = [x for x in b if x not in a]
>>> c
[('a task', 'j')]
>>> a = {'cool task': 'b', 'another task': 'j', 'better task': 'y'}
>>> b = {'cool task': 'b', 'a task': 'j', 'another task': 'j', 'better task': 'y'}
>>> c = [(x, b[x]) for x in b if x not in a]
>>> c
[('a task', 'j')]
字典:
>>> a = [('cool task', 'b'), ('another task', 'j'), ('better task', 'y')]
>>> b = [('cool task', 'b'), ('a task', 'j'), ('another task', 'j'), ('better task', 'y')]
>>> c = [x for x in b if x not in a]
>>> c
[('a task', 'j')]
>>> a = {'cool task': 'b', 'another task': 'j', 'better task': 'y'}
>>> b = {'cool task': 'b', 'a task': 'j', 'another task': 'j', 'better task': 'y'}
>>> c = [(x, b[x]) for x in b if x not in a]
>>> c
[('a task', 'j')]
对于简单列表-您需要的只是搜索列表中的下一项:
>>> a = ['cool task', 'b', 'another task', 'j', 'better task', 'y']
>>> b = ['cool task', 'b', 'a task', 'j', 'another task', 'j', 'better task', 'y']
>>> c = [[x, b[b.index(x) + 1]] for x in b if x not in a]
>>> c
[['a task', 'j']]
但我认为你实际上是在使用字典或元组
元组:
>>> a = [('cool task', 'b'), ('another task', 'j'), ('better task', 'y')]
>>> b = [('cool task', 'b'), ('a task', 'j'), ('another task', 'j'), ('better task', 'y')]
>>> c = [x for x in b if x not in a]
>>> c
[('a task', 'j')]
>>> a = {'cool task': 'b', 'another task': 'j', 'better task': 'y'}
>>> b = {'cool task': 'b', 'a task': 'j', 'another task': 'j', 'better task': 'y'}
>>> c = [(x, b[x]) for x in b if x not in a]
>>> c
[('a task', 'j')]
字典:
>>> a = [('cool task', 'b'), ('another task', 'j'), ('better task', 'y')]
>>> b = [('cool task', 'b'), ('a task', 'j'), ('another task', 'j'), ('better task', 'y')]
>>> c = [x for x in b if x not in a]
>>> c
[('a task', 'j')]
>>> a = {'cool task': 'b', 'another task': 'j', 'better task': 'y'}
>>> b = {'cool task': 'b', 'a task': 'j', 'another task': 'j', 'better task': 'y'}
>>> c = [(x, b[x]) for x in b if x not in a]
>>> c
[('a task', 'j')]
根据您的用途,您可以从以下位置使用
计数器
:
根据您的用途,您可以从以下位置使用
计数器
:
您可以使用枚举添加、删除和更改的条目:
>>> from difflib import SequenceMatcher
>>> matcher = SequenceMatcher(a=a, b=b)
>>> added = []
>>> for tag, i1, i2, j1, j2 in matcher.get_opcodes():
... if tag == 'insert':
... added += b[j1:j2]
...
>>> added
['a task', 'j']
以上内容仅针对新增条目;如果您需要了解已删除或更改的条目,那么这些事件也有操作码,请参阅
但是,如果您的条目总是成对的,那么只需使用元组生成集合(使用);然后,您可以对以下各项执行任何设置操作:
aset = set(zip(*([iter(a)] * 2)))
bset = set(zip(*([iter(b)] * 2)))
difference = bset - aset
演示:
您可以使用枚举添加、删除和更改的条目:
>>> from difflib import SequenceMatcher
>>> matcher = SequenceMatcher(a=a, b=b)
>>> added = []
>>> for tag, i1, i2, j1, j2 in matcher.get_opcodes():
... if tag == 'insert':
... added += b[j1:j2]
...
>>> added
['a task', 'j']
以上内容仅针对新增条目;如果您需要了解已删除或更改的条目,那么这些事件也有操作码,请参阅
但是,如果您的条目总是成对的,那么只需使用元组生成集合(使用);然后,您可以对以下各项执行任何设置操作:
aset = set(zip(*([iter(a)] * 2)))
bset = set(zip(*([iter(b)] * 2)))
difference = bset - aset
演示:
它可以根据您的需要工作:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
def difference(a, b):
a, b = (lambda x, y: (y, x) if len(set(x)) > len(set(y)) else (x, y)) (a, b)
a_result = list(a)
b_result = list(b)
for z in range(len(a)):
if a[z] in b:
a_result.remove(a[z])
b_result.remove(a[z])
return a_result, b_result
# or
# return a_result if len(set(a_result)) > len(set(b_result)) else b_result
def main():
a = ['cool task', 'b', 'another task', 'j', 'better task', 'y']
b = ['cool task', 'b', 'a task', 'j', 'another task', 'j', 'better task', 'y']
print(difference(a, b))
if __name__ == "__main__":
main()
它可以根据您的需要工作:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
def difference(a, b):
a, b = (lambda x, y: (y, x) if len(set(x)) > len(set(y)) else (x, y)) (a, b)
a_result = list(a)
b_result = list(b)
for z in range(len(a)):
if a[z] in b:
a_result.remove(a[z])
b_result.remove(a[z])
return a_result, b_result
# or
# return a_result if len(set(a_result)) > len(set(b_result)) else b_result
def main():
a = ['cool task', 'b', 'another task', 'j', 'better task', 'y']
b = ['cool task', 'b', 'a task', 'j', 'another task', 'j', 'better task', 'y']
print(difference(a, b))
if __name__ == "__main__":
main()
您想在
b
中查找在b
中比在a
中更频繁出现的元素吗?(请注意,出现一次视为出现的次数多于完全不出现)。如果偶数元素与奇数索引后面的元素紧密相连,您最好定义一个不同的数据结构来突出显示此关联。您想在b
中查找在b
中比在a
中更频繁出现的元素吗?(请注意,出现一次就意味着出现的次数更多)。如果偶数元素与奇数索引后面的元素紧密相连,则最好定义一个不同的数据结构来突出此关联。感谢编辑,我能够使用c[0]使用第一个答案,然后使用我想要的部分]感谢编辑,我能够使用c[0][然后是我想要的部分]使用第一个答案,这看起来像是一个有用的模块。我不知道。这看起来像是一个有用的模块。我没有意识到。