简体   繁体   中英

Multiple the second element of the Tuple to Key value in dictionary

There is list of tuples as below:

list_tuple = [('A',1),('B',2),('C',3)] and 
dict_brand = {'A':25,'B':30,'C':20}

I have to calculate the equation where:

price = A*1 + B*2 + C*3 , where A,B,C values are taken from the dictionary correspondingly.

Try this

price = sum(dict_brand[x] * y for x, y in list_tuple)
print(price) #145

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