简体   繁体   中英

Need to find the lowest number in sublists, where the number is in string format

I have lists inside the list, Inside that, I have a decimal number, I want my code to iterate through the list of lists and find the lowest decimal number. Also I want to skip the first element of the list.

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']]]

[Finished in 0.2s]

I need to skip the first nested list, and iterate from the second nested list, and find the lowest bidding number in each nested list.

For the Auction element toaster_1, the lowest bid number is 34.00 For the Auction element toaster_1, there is no lowest bidding number. For the Auction element radio_1 the lowest bidding number is 16.50 For the Auction element TV_1 the lowest bidding number is 150.00 For the Auction element Transistor_1 the lowest bidding number is 20.00

The problem for me is these elements are in String type, so when I run the code to find the minimum number, the output is just like below.

. . . . .

Expected output is
34.00
0
16.50
150.00
20.00

You can cast the lowest biddings to int like this int(value_as_string) . Look here for more information.

As I can see in your post, the inputs seem to be a separate list of list of lists .

Assuming that you are looking for a function to operate on each of them individually, try this method -

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM