[英]Need to find the lowest number in sublists, where the number is in string format
我在列表中有列表,在里面,我有一個十進制數,我希望我的代碼遍歷列表列表並找到最小的十進制數。 我也想跳過列表的第一個元素。
Input :
[[['7', '3', 'SELL', 'chair_1', '30.00'], ['17', '4', 'BID', 'chair_1', '34.00']]]
[[['14', '5', 'SELL', 'toaster_1', '14.50', '30']]]
[[['10', '1', 'SELL', 'radio_1', '10.00', '30'], ['13', '5', 'BID', 'radio_1', '12.50'], ['16', '6', 'BID', 'radio_1', '12.50']]]
[[['12', '4', 'SELL', 'TV_1', '200.00', '35'], ['15', '8', 'BID', 'TV_1', '250.00'], ['18', '1', 'BID', 'TV_1', '150.00'], ['19', '3', 'BID', 'TV_1', '200.00'], ['21', '3', 'BID', 'TV_1', '300.00']]]
[[['11', '3', 'SELL', 'transistor_1', '15.00', '30'], ['20', '7', 'BID', 'transistor_1', '20.00']]]
【0.2s完成】
我需要跳過第一個嵌套列表,並從第二個嵌套列表中迭代,並在每個嵌套列表中找到最低的出價數。
對於拍賣元素 toaster_1,最低出價是 34.00 對於拍賣元素 toaster_1,沒有最低出價。 對於拍賣元素 radio_1,最低出價是 16.50 對於拍賣元素 TV_1,最低出價是 150.00 對於拍賣元素 Transistor_1,最低出價是 20.00
對我來說問題是這些元素是字符串類型的,所以當我運行代碼找到最小數字時,output 就像下面一樣。
. . . . .
Expected output is
34.00
0
16.50
150.00
20.00
您可以像這樣將最低出價投給 int int(value_as_string)
。 在這里查看更多信息。
正如我在您的帖子中看到的,輸入似乎是一個單獨的列表 list of lists 。
假設您正在尋找一個 function 來單獨操作它們中的每一個,試試這個方法 -
a = [[['7', '3', 'SELL', 'chair_1', '30.00'], ['17', '4', 'BID', 'chair_1', '34.00']]]
b = [[['14', '5', 'SELL', 'toaster_1', '14.50', '30']]]
c = [[['10', '1', 'SELL', 'radio_1', '10.00', '30'], ['13', '5', 'BID', 'radio_1', '12.50'], ['16', '6', 'BID', 'radio_1', '12.50']]]
d = [[['12', '4', 'SELL', 'TV_1', '200.00', '35'], ['15', '8', 'BID', 'TV_1', '250.00'], ['18', '1', 'BID', 'TV_1', '150.00'], ['19', '3', 'BID', 'TV_1', '200.00'], ['21', '3', 'BID', 'TV_1', '300.00']]]
e = [[['11', '3', 'SELL', 'transistor_1', '15.00', '30'], ['20', '7', 'BID', 'transistor_1', '20.00']]]
def get_min_auc(auc):
d = [j for i in auc[0][1:] for j in i if len(j.split('.'))>1]
try: return min(d)
except: return 0
print(get_min_auc(a))
print(get_min_auc(b))
print(get_min_auc(c))
print(get_min_auc(d))
print(get_min_auc(e))
34.00
0
12.50
150.00
20.00
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.