[英]Python list: how to make it ordered and get rid of duplicates
I'm a novice in Python and my homework is to take a list with a bunch of numbers in it and我是 Python 新手,我的作业是列出一个包含一堆数字的列表,然后
This must be accomplished with loops这必须用循环来完成
My code so far:到目前为止我的代码:
numbers = [84, 79, 66, 69, 79, 82, 78, 79, 84, 84, 79, 66, 69, 84,
72, 65, 84, 73, 83, 84, 72, 69, 81, 85, 69, 83, 84, 73, 79, 78]
ordered = []
while numbers != []:
min = numbers[0]
for i in range(0, len(numbers)):
if numbers[i] < min:
min = numbers[i]
ordered.append(min)
j = 0
while j < len(numbers):
if numbers[j] == min:
numbers.pop(j)
j += 1
print(ordered)
And the output:和输出:
[65, 66, 69, 72, 73, 78, 79, 79, 81, 82, 83, 84, 84, 84, 85]
So task1 is okay but task2 is only accomplished at some points not all.所以 task1 没问题,但 task2 只在某些时候完成,而不是全部。 I can't figure out why?我想不通为什么? Thank you in advance for any help!预先感谢您的任何帮助! PS: I already solved the problem in another way but it just keeps bugging me why this posted idea of mine didn't work. PS:我已经以另一种方式解决了这个问题,但它一直困扰着我,为什么我发布的这个想法不起作用。
Others have provided shorter and more efficient ways of doing this, but you asked specifically what goes wrong with your approach.其他人提供了更短、更有效的方法,但您具体询问了您的方法出了什么问题。 The problem is in this bit:问题出在这一点上:
j = 0
while j < len(numbers):
if numbers[j] == min:
numbers.pop(j)
j += 1
What happens if min
occurs twice back to back?如果min
背靠背出现两次会发生什么? Let's say for example that min == 3
, and the list is [1, 3, 3, 7]
.例如,假设min == 3
,并且列表是[1, 3, 3, 7]
。
j == 0
, numbers[j] == 1
so we don't pop it, and increment j
.对于j == 0
, numbers[j] == 1
所以我们不会弹出它,并增加j
。j == 1
, numbers[j] == 3
so we remove the element 1
and increment j
.对于j == 1
, numbers[j] == 3
所以我们删除元素1
并增加j
。 The list is now [1, 3, 7]
.列表现在是[1, 3, 7]
。j == 2
, numbers[j] == 7
so we don't pop it, and we're done.对于j == 2
, numbers[j] == 7
所以我们不会弹出它,我们就完成了。 Whoops!哎呀! We have skipped over the second 3
because it moved back one position when we popped its predecessor, while j
moved forwards one position at the same time.我们跳过了第二个3
因为当我们弹出它的前一个时它向后移动了一个位置,而j
同时向前移动了一个位置。
The solution is to only increment j
if we didn't remove anything, because we need to re-check the element at position j
after we removed its predecessor:解决方案是仅在我们没有删除任何内容的情况下才增加j
,因为我们需要在删除其前任后重新检查位置j
的元素:
j = 0
while j < len(numbers):
if numbers[j] == min:
numbers.pop(j)
else:
j += 1
Let's verify that the loop is still guaranteed to terminate.让我们验证循环是否仍然保证终止。 Each iteration, either j
becomes larger, or len(numbers)
becomes smaller, so eventually they will meet and j < len(numbers)
becomes false.每次迭代,要么j
变大,要么len(numbers)
变小,所以最终它们会相遇并且j < len(numbers)
变为 false。 So we're good.所以我们很好。
You can use bubble sort.(If you need another sorting method, just replace it)你可以使用冒泡排序。(如果你需要其他排序方法,只需替换它)
numbers = [84, 79, 66, 69, 79, 82, 78, 79, 84, 84, 79, 66, 69, 84,
72, 65, 84, 73, 83, 84, 72, 69, 81, 85, 69, 83, 84, 73, 79, 78]
def bubble_sort(arr):
n = len(arr)
for i in range(n-1):
for j in range(0, n-i-1):
if arr[j] > arr[j+1] :
arr[j], arr[j+1] = arr[j+1], arr[j]
no_dub = []
for i in arr:
if i not in no_dub:
no_dub.append(i)
return no_dub
bubble_sort(numbers)
The result:结果:
[65, 66, 69, 72, 73, 78, 79, 81, 82, 83, 84, 85]
You need a small addition:你需要一个小的补充:
while j < len(numbers):
if numbers[j] == min:
numbers.pop(j)
j -= 1 #increment should not advance if you pop a number
j += 1
Then it will work然后它会工作
set
will remove duplicates set
将删除重复项
sorted
will sort it sorted
将对其进行排序
sorted(list(set([65, 66, 69, 72, 73, 78, 79, 79, 81, 82, 83, 84, 84, 84, 85])))
[65, 66, 69, 72, 73, 78, 79, 81, 82, 83, 84, 85]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.