本文目錄一覽:
求助python中一個排序問題!
lines = [x for x in open(‘/tmp/test.txt’,r)]
lines.sort()
print lines
讓程序自動排序即可
python 一個排序的問題
題目是不是:
有ABCD四個列表,每個列表有不同的元素(理解為字母);
每次從四個列表裡面pop第一個元素組成一個單詞作為新列表(輸出的列表)的元素;
pop完之後要判斷:
D列表長度可以為0
C列表長度可以為0,當D列表長度為0
B列表長度可以為0,當C列表長度為0
A列表長度可以為0,當B列表長度為0
如果有異常(不符合上述條件),組成的單詞要追回(remove)。
輸出包含新生成單詞的列表。
另外要注意異常:
當輸入的四個列表已經有長度為0的情況
當第一次pop之後,有列表長度為0的情況
def GenerateRndList():
“Call ListFactory and check whether the list is empty. If it is, repeat ListFactory.”
while 1:
list = ListFactory()
if len(list) 0:
return list
def ListFactory():
“Generate list”
from random import randint
List = [‘A’,’B’,’C’,’D’,’E’,’F’,’G’,’H’,’I’,’J’,’K’,’L’,’M’,’N’,’O’,’P’,’Q’,’R’,’S’,’T’,’U’,’V’,’W’,’X’,’Y’,’Z’]
tmpID = randint(0,len(List)) # how many elements require to be removed
for i in range(tmpID):
List.pop(randint(0, (len(List) – 1)))
return List
def AddWord(character_list, debug = False):
“Add word to output list”
global WORD_LIST
word = ”
for i in character_list:
word += i
if debug is True:
print ‘Combined – ‘, word
WORD_LIST.append(word)
def RemoveWord(debug = False):
“Remove final word to output list”
global WORD_LIST
if debug is True:
print ‘Remove – ‘, WORD_LIST[-1]
WORD_LIST.pop()
def Outputter():
print WORD_LIST
def WordVerifier(word):
“Send out word to one web site to verify whether word is meaningful.”
pass
def ListVerifier(input_list):
“Check whether length of list items matches the requirement”
global ProcessFlag
# Prepare a flag list, whose element is 1 or 0.
original_length_list = []
for i in range(len(input_list)):
if len(input_list[i]) == 0:
original_length_list.append(0)
if len(input_list[i]) = 1:
original_length_list.append(1)
”’
Compare whether the list is same after sorted,
– if same, rule is matched;
– if not, rule is NOT matched.
”’
compare_length_list = original_length_list[:]
compare_length_list.sort()
compare_length_list.reverse()
if str(compare_length_list) != str(original_length_list):
RemoveWord() # at this time, state maybe [01][01][01]1
ProcessFlag = False
elif original_length_list.count(0) 0:
ProcessFlag = False
def Callback(input_list, Callback_Support_Function):
“Generate new word”
while 1:
new_word = []
for i in range(len(input_list)):
new_word.append(input_list[i].pop(0))
AddWord(new_word)
Callback_Support_Function(input_list)
if ProcessFlag is False:
break
def main(list_number = 4):
INPUT = []
for i in range(list_number):
INPUT.append(GenerateRndList())
print ‘Generated lists are,’
for i in INPUT:
print ‘\t’, i
Callback(INPUT, ListVerifier)
Outputter()
# WordVerifier(word)
if __name__ == ‘__main__’:
global WORD_LIST, ProcessFlag
WORD_LIST = []
ProcessFlag = True
main(list_number = 4)
Python學習小技巧之列表項的排序
Python學習小技巧之列表項的排序
本文介紹的是關於Python列表項排序的相關內容,分享出來供大家參考學習,下面來看看詳細的介紹:
典型代碼1:
data_list = [6, 9, 1, 3, 0, 10, 100, -100]
data_list.sort()
print(data_list)
輸出1:
[-100, 0, 1, 3, 6, 9, 10, 100]
典型代碼2:
data_list = [6, 9, 1, 3, 0, 10, 100, -100]
data_list_copy = sorted(data_list)
print(data_list)
print(data_list_copy)
輸出2:
[6, 9, 1, 3, 0, 10, 100, -100]
[-100, 0, 1, 3, 6, 9, 10, 100]
應用場景
需要對列表中的項進行排序時使用。其中典型代碼1是使用的列表自身的一個排序方法sort,這個方法自動按照升序排序,並且是原地排序,被排序的列表本身會被修改;典型代碼2是調用的內置函數sort,會產生一個新的經過排序後的列表對象,原列表不受影響。這兩種方式接受的參數幾乎是一樣的,他們都接受一個key參數,這個參數用來指定用對象的哪一部分為排序的依據:
data_list = [(0, 100), (77, 34), (55, 97)]
data_list.sort(key=lambda x: x[1]) # 我們想要基於列表項的第二個數進行排序
print(data_list)
[(77, 34), (55, 97), (0, 100)]
另外一個經常使用的參數是reverse,用來指定是否按照倒序排序,默認為False:
data_list = [(0, 100), (77, 34), (55, 97)]
data_list.sort(key=lambda x: x[1], reverse=True) # 我們想要基於列表項的第二個數進行排序,並倒序
print(data_list)
[(0, 100), (55, 97), (77, 34)]
帶來的好處
1. 內置的排序方法,執行效率高,表達能力強,使代碼更加緊湊,已讀
2. 靈活的參數,用於指定排序的基準,比在類似於Java的語言中需要寫一個comparator要方便很多
其它說明
1. sorted內置函數比列表的sort方法要適用範圍更廣泛,它可以對除列表之外的可迭代數據結構進行排序;
2. list內置的sort方法,屬於原地排序,理論上能夠節省內存的消耗;
總結
好了,以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作能帶來一定的幫助
python 中 set 的排序問題?
set 本身根據定義就是無序的,具體的輸出順序跟實現相關。
方法1 為什麼是有序的你可以認為這是一個實現的巧合,實際代碼中不應該依賴這個特性(因為別的實現可能不一致,甚至 Python 官方的時候隨著版本都有可能變化)。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/230762.html