標題:
排序 - sort() & sorted()
[打印本頁]
作者:
tonyh
時間:
2022-7-14 17:04
標題:
排序 - sort() & sorted()
data=input("請輸入多個整數(以空白分隔): ").split(" ")
for i in range(len(data)): #將串列內容轉換為整數型態
data[i]=int(data[i])
print("遞增排序: ",end="")
for i in sorted(data): #sorted()不會真的改變串列的內容
print(i,end=" ")
print("\n遞減排序: ",end="")
for i in sorted(data,reverse=True):
print(i,end=" ")
print("\n原始資料: ",end="")
#data.sort() #sort()會改變串列的內容
for i in data:
print(i,end=" ")
複製代碼
作者:
李沛昂
時間:
2022-7-14 17:30
data=input("請輸入多個整數(以空白分隔): ").split(" ")
for i in range(len(data)):
data[i]=int(data[i])
print("遞增排序: ",end="")
for i in sorted(data):
print(i,end=" ")
print("\n遞減排序: ",end="")
for i in sorted(data,reverse=True):
print(i,end=" ")
print("\n原始資料: ",end="")
for i in data:
print(i,end=" ")
複製代碼
作者:
王捷恩
時間:
2022-7-16 13:26
data = input("enter a list of numbers (seperate with space in between): ").split(" ")
for i in range(len(data)):
data[i] = int(data[i])
print("ascending sort: ", end = " ")
for i in sorted(data):
print(i, end = " ")
print("\ndescending sort: ", end = " ")
for i in sorted(data, reverse = True):
print(i, end = " ")
print("\noriginal data: ", end = " ")
for i in data:
print(i, end = " ")
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/)
Powered by Discuz! 7.2