Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.5k views
in Technique[技术] by (71.8m points)

python - How to add a value to a a list?

I have the following list.

import datetime

my_list = [
    ['Morocco', 'Fish', datetime.datetime(2020, 11, 17, 0, 0),'0,012'], 
    ['Morocco', 'Fish', datetime.datetime(2020, 11, 17, 0, 0),'0,153'], 
    ['Morocco', 'Fish', datetime.datetime(2020, 11, 17, 0, 0), '0,114'],
    ['Morocco', 'Fish', datetime.datetime(2020, 11, 17, 0, 0), '0,109'],
    ['Morocco', 'Fish', datetime.datetime(2020, 11, 17, 0, 0), '0,252'],
    ['Spain', 'Fish', datetime.datetime(2020, 11, 17, 0, 0), '0,012'], 
    ['Spain', 'Fish', datetime.datetime(2020, 11, 17, 0, 0), '0,113'], 
    ['Spain', 'Fish', datetime.datetime(2020, 11, 17, 0, 0), '0,116'],
    ['Spain', 'Fish', datetime.datetime(2020, 11, 17, 0, 0), '0,250'],
    ['Spain', 'Fish', datetime.datetime(2020, 11, 17, 0, 0), '0,266']]

With below code im grabbing the highest value per country. That works correct, however. I would also like to add the country to max_price_per_list

max_prices_by_country = {}
for item in my_list:
    price = max_prices_by_country.setdefault(item[0], 0)
    max_prices_by_country[item[0]] = max(price, float(item[-1].replace(',', '.')))
    country = item[0]

max_price_per_list = [[country] + [str(price).replace('.', ',') for price in max_prices_by_country.values()]]

print(max_price_per_list)

This is my current output:

[['Spain', '0,252', '0,266']]

This is my preferred output:

[[Morocco, '0,252'], 
['Spain', '0,266']]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

maybe you can do it this way

import datetime
my_list = [
    ['Morocco', 'Fish', datetime.datetime(2020, 11, 17, 0, 0),'0,012'], 
    ['Morocco', 'Fish', datetime.datetime(2020, 11, 17, 0, 0),'0,153'], 
    ['Morocco', 'Fish', datetime.datetime(2020, 11, 17, 0, 0), '0,114'],
    ['Morocco', 'Fish', datetime.datetime(2020, 11, 17, 0, 0), '0,109'],
    ['Morocco', 'Fish', datetime.datetime(2020, 11, 17, 0, 0), '0,252'],
    ['Spain', 'Fish', datetime.datetime(2020, 11, 17, 0, 0), '0,012'], 
    ['Spain', 'Fish', datetime.datetime(2020, 11, 17, 0, 0), '0,113'], 
    ['Spain', 'Fish', datetime.datetime(2020, 11, 17, 0, 0), '0,116'],
    ['Spain', 'Fish', datetime.datetime(2020, 11, 17, 0, 0), '0,250'],
    ['Spain', 'Fish', datetime.datetime(2020, 11, 17, 0, 0), '0,266']]
    
max_prices_by_country = {}
for item in my_list:
    country = item[0]

    # if country is not in dict add it with a value, 0
    if country not in max_prices_by_country:
        max_prices_by_country[country] = 0

    # get the max value of existing and incoming value (which is item[0])
    max_prices_by_country[country] = max(max_prices_by_country[country], float(item[-1].replace(',', '.')))

# convert the dict to a list of array.
max_price_per_list = [[k, str(max_prices_by_country[k]).replace('.', ',')] for k in max_prices_by_country]

print(max_price_per_list)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...