删除列表中重复项之间的所有实例,Python
Remove all instances between repeated items in list, Python
我需要编写一个函数,它接受一个列表,并从该列表中删除两个相同项目之间的所有项目(并保留相同项目之一)。
例如
list1 = ['a', 'b', 1, 'c', 'd', 'e', 'f', 'g', 'h', 1, 'i', 'j']
list2 = ['a', 'b', 1, 'c', '2', 'd', '2', 'e', 'f', 1, 'g', 'h']
list3 = ['a', 1, 'b', 'c', 1, 'd', 2, 'e', 'f', 2, 'g']
print(funct(list1))
print(funct(list2))
print(funct(list3))
将导致:
['a', 'b', 1, 'i', 'j']
['a', 'b', 1, 'g', 'h']
['a', 1, 'd', 2, 'g']
您可以假设列表中重复块的任何第二个实例总是完全独立的,或者完全在另一个块中。
例如
['a', 1, 'b', 2, 'c', 2, 'd', 1, 'e']
或
['a', 1, 'b', 1, 'c', 2, 'd', 2, 'e']
但从来没有
['a', 1, 'b', 2, 'c', 1, 'd', 2, 'e']
我已经编写了一个代码来执行此操作,但不喜欢创建单独的列表并将项目附加到该列表的需要,而宁愿只从原始列表中删除项目。
这就是我所拥有的,任何帮助将不胜感激:)
def funct(list):
unlooped = []
appending = True
list_index = 1
for item in list:
if appending:
unlooped.append(item)
elif item == looper:
appending = True
if item in list[list_index:] and appending:
appending = False
looper = item
list_index += 1
return unlooped
这个怎么样:
def unloop(ls):
return [x for i,x in enumerate(ls) if not set(ls[:i]).intersection(set(ls[i:]))]
解释:如果项目 [0..i-1]
和 [i..n]
的交集为空,即没有元素同时出现,则从 ls
的位置 i
获取项目 x
x
.
前后
它适用于您提供的 3 个示例,可能需要针对边缘情况对其进行测试
你写了 "I've written a code to do this, but do not like the need of creating a separate list, and appending items to that list, and would rather just remove items from the original list." 我假设这意味着从原始列表中删除项目,所以我编写了一个解决方案,从列表中删除整个切片。使用列表理解只是创建另一个列表并将项目附加到它的另一种方式,所以我想避免这种方法。
def unloop(lst):
for i, v in enumerate(lst):
try:
j = lst.index(v, i+1)
lst[i:j] = []
except ValueError:
pass
return lst
我认为有一些方法可以让这个算法更有效率。我会考虑的。附带说明一下,最好养成避免名称与内置对象冲突的习惯。例如,如果您有一个名为 list
的变量,您会发现很难使用内置的 list
名称将其他类型的可迭代对象转换为列表。
我需要编写一个函数,它接受一个列表,并从该列表中删除两个相同项目之间的所有项目(并保留相同项目之一)。
例如
list1 = ['a', 'b', 1, 'c', 'd', 'e', 'f', 'g', 'h', 1, 'i', 'j']
list2 = ['a', 'b', 1, 'c', '2', 'd', '2', 'e', 'f', 1, 'g', 'h']
list3 = ['a', 1, 'b', 'c', 1, 'd', 2, 'e', 'f', 2, 'g']
print(funct(list1))
print(funct(list2))
print(funct(list3))
将导致:
['a', 'b', 1, 'i', 'j']
['a', 'b', 1, 'g', 'h']
['a', 1, 'd', 2, 'g']
您可以假设列表中重复块的任何第二个实例总是完全独立的,或者完全在另一个块中。
例如
['a', 1, 'b', 2, 'c', 2, 'd', 1, 'e']
或
['a', 1, 'b', 1, 'c', 2, 'd', 2, 'e']
但从来没有
['a', 1, 'b', 2, 'c', 1, 'd', 2, 'e']
我已经编写了一个代码来执行此操作,但不喜欢创建单独的列表并将项目附加到该列表的需要,而宁愿只从原始列表中删除项目。
这就是我所拥有的,任何帮助将不胜感激:)
def funct(list):
unlooped = []
appending = True
list_index = 1
for item in list:
if appending:
unlooped.append(item)
elif item == looper:
appending = True
if item in list[list_index:] and appending:
appending = False
looper = item
list_index += 1
return unlooped
这个怎么样:
def unloop(ls):
return [x for i,x in enumerate(ls) if not set(ls[:i]).intersection(set(ls[i:]))]
解释:如果项目 [0..i-1]
和 [i..n]
的交集为空,即没有元素同时出现,则从 ls
的位置 i
获取项目 x
x
.
它适用于您提供的 3 个示例,可能需要针对边缘情况对其进行测试
你写了 "I've written a code to do this, but do not like the need of creating a separate list, and appending items to that list, and would rather just remove items from the original list." 我假设这意味着从原始列表中删除项目,所以我编写了一个解决方案,从列表中删除整个切片。使用列表理解只是创建另一个列表并将项目附加到它的另一种方式,所以我想避免这种方法。
def unloop(lst):
for i, v in enumerate(lst):
try:
j = lst.index(v, i+1)
lst[i:j] = []
except ValueError:
pass
return lst
我认为有一些方法可以让这个算法更有效率。我会考虑的。附带说明一下,最好养成避免名称与内置对象冲突的习惯。例如,如果您有一个名为 list
的变量,您会发现很难使用内置的 list
名称将其他类型的可迭代对象转换为列表。