我有一个由子列表组成的列表。我需要在子列表的特定位置找到最大的元素。我已经将主列表放在 for 循环中,所以我可以直接操作子列表。但是列表采用字符串类型,所以当我使用 Max 函数时它无法给我最大的数字。each_entry :[['10', '1', 'SELL', 'toaster_1', '10.00', '20'], ['12', '8', 'BID', 'toaster_1', '7.50'], ['13', '5', 'BID', 'toaster_1', '12.50'], ['17', '8', 'BID', 'toaster_1', '20.00']][['15', '8', 'SELL', 'tv_1', '250.00', '20'], ['18', '1', 'BID', 'tv_1', '150.00'], ['19', '3', 'BID', 'tv_1', '200.00'], ['21', '3', 'BID', 'tv_1', '300.00']]for each_bid_item in dictlist: for each_entry in each_bid_item: initial_time = (each_entry[0][0]) max_time = (each_entry[0][5]) reserve_price = (each_entry[0][4]) sell_item = (each_entry[0][3])当我打字时print(max(each_entry[0][4]))Output : 1 5简而言之,我需要找到第一个和第二个子列表的最高出价。
1 回答

12345678_0001
TA贡献1802条经验 获得超5个赞
您可以轻松遍历子列表以找到最大值。
each_entry = [['10', '1', 'SELL', 'toaster_1', '10.00', '20'],
['12', '8', 'BID', 'toaster_1', '7.50'],
['13', '5', 'BID', 'toaster_1', '12.50'],
['17', '8', 'BID', 'toaster_1', '20.00']]
max_bid = 0
for entry in each_entry:
sell_price = float(entry[4])
if sell_price > max_bid:
max_bid = sell_price
print(max_bid)
输出:
20.0
添加回答
举报
0/150
提交
取消