我知道我的嘗試非常基礎,我對 python 的了解也是如此。該問題要求從用戶那里獲取多個數字,并從其左側洗掉可能的 0、 91 或 91,使其成為 10 位數字,然后對其進行排序和列印。
我試過了num_list[1] = num_list[1][len(num_list[1])-10:]。它有效,所以我嘗試將它放在我正在學習的串列理解格式中,但它當時不起作用。我需要有關如何做到這一點的幫助,希望能更好地理解何時應該使用理解格式。
n = int(input()) # get number of phone numbers from user
num_list = [] # an empty list to store phone numbers in
num_list = [input() for _ in range(n)] # store phone numbers in num_list
##################################
num_list = [num_list[num] = num_list[num][len(num_list[num])-10:] for num in num_list] #remove possible 0, 91, 91 from beginning of numbers
########################################
num_list = sorted(num_list)
num_list = [" 91 " num[:5] " " num[5:] for num in num_list]
print(*num_list , sep="\n")
uj5u.com熱心網友回復:
您不應該直接在串列理解中分配。
您正在尋找的語法是:
num_list = [num[-10:] for num in num_list]
這在邏輯上等同于:
cleaned_list = []
for num in num_list:
cleaned_list.append(num[-10:])
num_list = cleaned_list
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/409638.html
標籤:
上一篇:這是一個數字猜謎游戲,num_check函式第一次運行良好,但不會運行任何第二次嘗試。有誰知道為什么?
下一篇:我不知道我的功能有什么問題
