对于简单的列表 - 你问什么是简单的搜索在列表中的下一个项目:
>>> 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')]
你想找到'B'出现的元素在'b'中比在'a'中更频繁? (请注意,出现一次的次数总是比没有出现次数多)。 –
如果偶数元素与奇数索引中的元素紧密关联,则应该更好地定义突出显示此关联的不同数据结构。 – trincot