删除列表 Python 中的重复项(使用 For 循环)
Remove Duplicates in a List Python (Using For loop)
list = [1,1,3,4,5,5,5,6]
count = 0
for i in range(len(list)):
for k in range(len(list)):
if i == k :
continue
elif list[i] == list[k]:
del list[k]
list.append("0")
count = count + 1
print(count)
print(list)
当我执行此操作时,“0”值添加到此列表以填充已删除的重复值。
我用计数变量来计数,
Count 变量应该打印 3,因为当我 运行 程序时,“0”中的 3 个添加到列表中。
为什么打印“9”?
This is the output
ls = [1,1,3,4,5,5,5,6]
result = []
for item in ls[:]:
if item not in result:
result.append(item)
print(result)
要使用 for 循环,请尝试以下代码:
# old list
mylist = [1,1,3,4,5,5,5,6]
# create new list
newlist = []
# Loop for each item inside the old list
for x in mylist:
#check if this item is not duplicate
if x not in newlist:
# add not duplicate item to new list
newlist.append(x)
print (newlist)
输出:-
[1, 3, 4, 5, 6]
当您删除一个数字并在列表末尾添加一个零时,您的主循环(for i ...)最终将到达末尾的零。那时您将删除一个“1”和两个“5”,因此计数将为 3,并且将有 3 个零要处理。这些零中的每一个都会在列表中找到另外两个零(用另一个零替换不会有什么不同)。因此,总共 1+2+2+2+2 = 9 计数 1(对于 1)2(对于 5)和 3 次 2(对于每个零)。
解决此问题的一种方法(无需过多更改代码)是在达到零时中断主循环(假设零不是初始列表中的合法值)
如果我有这个错误,您将不得不原谅我,但从您的示例来看,您为每个重复的数字添加了一个“0”,然后试图计算列表中添加了多少个“0”。
根据列表创建集合似乎更容易,然后将两者与我的思维方式进行比较。
mylist = [1,1,3,4,5,5,5,6]
myset = set(mylist)
print(len(mylist) - len(myset))
这将为您提供您正在寻找的第 3 个答案。
逻辑错误。
- 如果您打算用
0
替换重复值,则执行 del
+ list.append()
是不正确的,因为 del
会删除值,而 [=21] =] 将在列表 末尾 添加一个新值,而不是内联。请参阅此示例。
my_list = [100, 100, 200]
print(my_list, "Original list")
del my_list[1]
print(my_list, "List after del")
my_list.append(0)
print(my_list, "List after append")
输出:
[100, 100, 200] Original list
[100, 200] List after del
[100, 200, 0] List after append
您可以使用 del
+ list.insert()
,但仍然效率低下。相反,您只需直接重新分配新值即可。
my_list[1] = 0
print(my_list, "List after re-assignment")
输出:
[100, 0, 200] List after re-assignment
- 对于每个元素,您都在迭代所有其他元素,但会跳过当前元素本身。这意味着,如果您将下一个元素中的值更改为
0
,一旦您到达该元素,它就会将其他零计为重复项,即使这些零不计入在内。所以伪代码是:
[100, 100, 200, 200] -> Original list
loop index=0
Check duplicates for item-0 which is 100
[100, 0, 200, 200] -> Sees item-1 as duplicate.
count = 1
loop index=1
Check duplicates for item-1 which is 0
No duplicates
count = 1
loop index=2
Check duplicates for item-2 which is 200
[100, 0, 200, 0] -> Sees item-3 as duplicate.
count = 2
loop index=3
Check duplicates for item-3 which is 0
[100, 0, 200, 0] -> Sees item-1 as duplicate.
count = 3
如您所见,它错误地计算了 0
的重复值。快速修复您的代码是跳过 0
元素
...
for i in range(len(list)):
if list[i] == 0:
continue
...
更好的是,重新设计逻辑。不是迭代所有元素,而是只迭代到当前元素。所以想法是遍历每个元素,并检查它是否已经出现在过去的元素中。需要强调的是,过去的元素,不要迭代未来的元素。
- 不要使用内置名称
list
。如果您稍后再写 list(some_iterable)
,这将导致失败。
示例解决方案
my_list = [1,1,3,4,5,5,5,6]
count = 0
for i in range(len(my_list)):
for k in range(0, i):
if my_list[i] == my_list[k]:
my_list[i] = 0 # If you want the result to be [1, 0, 3, 4, 5, 0, 0, 6]
# my_list[k] = 0 # If you want the result to be [0, 1, 3, 4, 0, 0, 5, 6]
count = count + 1
break
print(count)
print(my_list)
输出
3
[1, 0, 3, 4, 5, 0, 0, 6]
改进
请注意,您的算法的时间复杂度为 O(n^2)
。您可以通过使用额外的 O(n)
space 复杂性将其降低到 O(n)
,这将跟踪已经存在的元素。
my_list = [1,1,3,4,5,5,5,6]
existing = set()
count = 0
for i in range(len(my_list)):
if my_list[i] in existing:
my_list[i] = 0
count += 1
else:
existing.add(my_list[i])
print(count)
print(my_list)
list = [1,1,3,4,5,5,5,6]
count = 0
for i in range(len(list)):
for k in range(len(list)):
if i == k :
continue
elif list[i] == list[k]:
del list[k]
list.append("0")
count = count + 1
print(count)
print(list)
当我执行此操作时,“0”值添加到此列表以填充已删除的重复值。 我用计数变量来计数, Count 变量应该打印 3,因为当我 运行 程序时,“0”中的 3 个添加到列表中。 为什么打印“9”?
This is the output
ls = [1,1,3,4,5,5,5,6]
result = []
for item in ls[:]:
if item not in result:
result.append(item)
print(result)
要使用 for 循环,请尝试以下代码:
# old list
mylist = [1,1,3,4,5,5,5,6]
# create new list
newlist = []
# Loop for each item inside the old list
for x in mylist:
#check if this item is not duplicate
if x not in newlist:
# add not duplicate item to new list
newlist.append(x)
print (newlist)
输出:-
[1, 3, 4, 5, 6]
当您删除一个数字并在列表末尾添加一个零时,您的主循环(for i ...)最终将到达末尾的零。那时您将删除一个“1”和两个“5”,因此计数将为 3,并且将有 3 个零要处理。这些零中的每一个都会在列表中找到另外两个零(用另一个零替换不会有什么不同)。因此,总共 1+2+2+2+2 = 9 计数 1(对于 1)2(对于 5)和 3 次 2(对于每个零)。
解决此问题的一种方法(无需过多更改代码)是在达到零时中断主循环(假设零不是初始列表中的合法值)
如果我有这个错误,您将不得不原谅我,但从您的示例来看,您为每个重复的数字添加了一个“0”,然后试图计算列表中添加了多少个“0”。
根据列表创建集合似乎更容易,然后将两者与我的思维方式进行比较。
mylist = [1,1,3,4,5,5,5,6]
myset = set(mylist)
print(len(mylist) - len(myset))
这将为您提供您正在寻找的第 3 个答案。
逻辑错误。
- 如果您打算用
0
替换重复值,则执行del
+list.append()
是不正确的,因为del
会删除值,而 [=21] =] 将在列表 末尾 添加一个新值,而不是内联。请参阅此示例。
my_list = [100, 100, 200]
print(my_list, "Original list")
del my_list[1]
print(my_list, "List after del")
my_list.append(0)
print(my_list, "List after append")
输出:
[100, 100, 200] Original list
[100, 200] List after del
[100, 200, 0] List after append
您可以使用 del
+ list.insert()
,但仍然效率低下。相反,您只需直接重新分配新值即可。
my_list[1] = 0
print(my_list, "List after re-assignment")
输出:
[100, 0, 200] List after re-assignment
- 对于每个元素,您都在迭代所有其他元素,但会跳过当前元素本身。这意味着,如果您将下一个元素中的值更改为
0
,一旦您到达该元素,它就会将其他零计为重复项,即使这些零不计入在内。所以伪代码是:
[100, 100, 200, 200] -> Original list
loop index=0
Check duplicates for item-0 which is 100
[100, 0, 200, 200] -> Sees item-1 as duplicate.
count = 1
loop index=1
Check duplicates for item-1 which is 0
No duplicates
count = 1
loop index=2
Check duplicates for item-2 which is 200
[100, 0, 200, 0] -> Sees item-3 as duplicate.
count = 2
loop index=3
Check duplicates for item-3 which is 0
[100, 0, 200, 0] -> Sees item-1 as duplicate.
count = 3
如您所见,它错误地计算了 0
的重复值。快速修复您的代码是跳过 0
元素
...
for i in range(len(list)):
if list[i] == 0:
continue
...
更好的是,重新设计逻辑。不是迭代所有元素,而是只迭代到当前元素。所以想法是遍历每个元素,并检查它是否已经出现在过去的元素中。需要强调的是,过去的元素,不要迭代未来的元素。
- 不要使用内置名称
list
。如果您稍后再写list(some_iterable)
,这将导致失败。
示例解决方案
my_list = [1,1,3,4,5,5,5,6]
count = 0
for i in range(len(my_list)):
for k in range(0, i):
if my_list[i] == my_list[k]:
my_list[i] = 0 # If you want the result to be [1, 0, 3, 4, 5, 0, 0, 6]
# my_list[k] = 0 # If you want the result to be [0, 1, 3, 4, 0, 0, 5, 6]
count = count + 1
break
print(count)
print(my_list)
输出
3
[1, 0, 3, 4, 5, 0, 0, 6]
改进
请注意,您的算法的时间复杂度为 O(n^2)
。您可以通过使用额外的 O(n)
space 复杂性将其降低到 O(n)
,这将跟踪已经存在的元素。
my_list = [1,1,3,4,5,5,5,6]
existing = set()
count = 0
for i in range(len(my_list)):
if my_list[i] in existing:
my_list[i] = 0
count += 1
else:
existing.add(my_list[i])
print(count)
print(my_list)