簡體   English   中英

我在 python 中的列表排序代碼中的問題

[英]problems in my list sorting code in python

我想出了這段代碼來整理隨機排列的數字列表中的重復項。

counter = 0
randomDup_list = [0, 3, 0, 1, 9, 8, 2, 3, 4, 2, 4, 3, 5, 6, 0, 6, 5, 2, 6, 6, 7, 8, 9, 4, 4]
dup_sorted = []
for x in randomDup_list:
    if len(randomDup_list) == 0:
        dup_sorted.append(x)
        counter +=1
    elif x != randomDup_list[counter]:
        for y in dup_sorted:
            if y != x:
                dup_sorted.append(x)
print(dup_sorted)

當我運行代碼時沒有錯誤,但數字似乎沒有附加到我的新列表中,並且它像這樣顯示為空白: []

最pythonic的方法是使用列表理解,如下所示:

dup_sorted = [el for index, el in enumerate(randomDup_list) if el not in randomDup_list[:index]]

Enumerate 將創建一個元組列表,其中第一個元組元素作為列表中的索引, [(0,0), (1,3), (2,0), ...]是您案例中的前 3 個元素.

然后它基本上檢查el是否是列表中el的第一次出現,如果是,它將el添加到dup_sorted列表中。

列表推導可能很難理解,但互聯網上有很多關於它們的信息。 祝你學習 Python 好運!

我不明白你想要什么,但你基本上可以像這樣對列表進行排序

print(sorted(randomDup_list))

你可以像這樣創建你的新列表

dup_sorted = sorted(randomDup_list)
print(dup_sorted)

大家好,歡迎學習python。 我已經編碼了幾年,但我仍然使用print來測試我的邏輯。

讓我們分解您的代碼並將print添加到每個邏輯測試

counter = 0
randomDup_list = [0, 3, 0, 1, 9, 8, 2, 3, 4, 2, 4, 3, 5, 6, 0, 6, 5, 2, 6, 6, 7, 8, 9, 4, 4]
dup_sorted = []
for x in randomDup_list:
    print("start iteration")
    if len(randomDup_list) == 0:
        print("1 true")
        dup_sorted.append(x)
        counter +=1
    elif x != randomDup_list[counter]:
        print("2 true")
        print(dup_sorted)
        for y in dup_sorted:
            if y != x:
                print("3 true")
                dup_sorted.append(x)
    print("stop iteration")
print(dup_sorted)

如果您運行此代碼,您將看到第一個邏輯測試在您的示例中永遠不會成立。 因此它將 go 進行第二次邏輯測試。 這最終將評估為 true,但將跳過一些迭代。 start iteration stop iteration 對於最后一個邏輯測試,這永遠不會是真的,因為 dup_sorted 將始終為空。 所以for y in dup_sorted將一無所獲。

我也認為sort out是模棱兩可的。 是排序嗎? 篩選?

您可以執行set(randomDup_list)過濾重復項
你可以做sorted(randomDup_list)來整理列表
您可以對排序的過濾列表執行sorted(list(set(randomDup_list))

看到你的新評論

randomDup_list = [0, 3, 0, 1, 9, 8, 2, 3, 4, 2, 4, 3, 5, 6, 0, 6, 5, 2, 6, 6, 7, 8, 9, 4, 4]
dup_sorted = []
for x in randomDup_list:
    if x in dup_sorted:
        continue
    else:
        dup_sorted.append(x)
print(dup_sorted)

這會初始化一個空列表。 循環瀏覽您的列表。 檢查它是否存在,如果不存在則追加。 由於 List 保持順序,您可以期望順序不會改變。

從列表中刪除任何重復項而不更改順序

代碼

dup_sorted = []
for x in randomDup_list:
  if not x in dup_sorted:  # check if x is in output yet
    dup_sorted.append(x)   # add x to output
  
print(dup_sorted)
# Output: [0, 3, 1, 9, 8, 2, 4, 5, 6, 7]

你可以使用這個:

counter = 0
randomDup_list = [0, 3, 0, 1, 9, 8, 2, 3, 4, 2, 4, 3, 5, 6, 0, 6, 5, 2, 6, 6, 7, 8, 9, 4, 4]
dup_sorted = []


dup_sorted.append(randomDup_list[0])
for x in range(len(randomDup_list)):
    temp = 0
    for y in range(len(dup_sorted)):
        if randomDup_list[x] != dup_sorted[y]:
            temp = temp + 1
    if temp == len(dup_sorted):
        dup_sorted.append(randomDup_list[x])
        
print(dup_sorted)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM