【问题标题】:Auction program question, need help in checking a string - number from sub list in python拍卖程序问题,需要帮助检查 python 子列表中的字符串 - 数字
【发布时间】:2020-08-14 10:53:45
【问题描述】:

我有一个由子列表组成的列表。我需要在子列表中的特定位置找到最大的元素。我已经将主列表放在 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

简而言之,我需要找到第一个和第二个子列表的最高出价。

【问题讨论】:

标签: python list


【解决方案1】:

您可以轻松遍历子列表以找到最大值。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-01-30
    • 1970-01-01
    • 2017-08-02
    • 2012-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-05
    相关资源
    最近更新 更多