Get values from a tuple in a list in Python

2024/7/8 6:43:20
x = Bookshop()
x.orders = [ [1, ("5464", 4, 9.99), ("8274",18,12.99), ("9744", 9, 44.95)],
[2, ("5464", 9, 9.99), ("9744", 9, 44.95)],
[3, ("5464", 9, 9.99), ("88112", 11, 24.99)],
[4, ("8732", 7, 11.99), ("7733", 11,18.99), ("88112", 5, 39.95)] ]r1 = x.prodcut_price()
print(r1)class Bookshop:def __init__(self):self.orders = 0def prodcut_price(self):result1  = list(map(lambda x:(x[1][0],x[1][1]*x[1][2]) if x[1][1]*x[1][2]>=100 else (x[1][0],x[1][1]*x[1][2]+10),self.orders))#print(result1)return result1

The list is basically the store number and each store sell some books with the code, quantity and the price.I am trying to create a method that will take each book quantity * price and print them in a tuple within list using lambda,filter and map only. what I got is that

[('5464', 39.96), ('5464', 89.91), ('5464', 89.91), ('8732', 83.93)]

but I need it for the whole list

Answer

To get those totals for a specific code you can do:

Code:

def get_value(orders_list, code):return {order[0]: o[1] * o[2] for order in orders_listfor o in order[1:] if o[0] == code}

Test Data:

orders = [[1, ("5464", 4, 9.99), ("8274", 18, 12.99), ("9744", 9, 44.95)],[2, ("5464", 9, 9.99), ("9744", 9, 44.95)],[3, ("5464", 9, 9.99), ("88112", 11, 24.99)],[4, ("8732", 7, 11.99), ("7733", 11, 18.99), ("88112", 5, 39.95)]
]print(get_value(orders, '5464'))

Results:

{1: 39.96, 2: 89.91, 3: 89.91}
https://en.xdnf.cn/q/120000.html

Related Q&A

Last Digit of the Sum of Fibonacci Numbers

I am trying to find the last digit of sum of Fibonacci Series. I calculate the sum as F(n+2) - 1. The below code is working fine but it is slow for large numbers (e.g 99999). How can I optimize this?n…

Django websites not loading

I have two Django websites on one server using Apache with mod_wsgi on Windows 10. For some reason the Django websites dont load, however, I have a normal website that does. Ive had it work in the past…

list manipulation and recursion

I have a mansory-grid in a pdf-page. The grid is choosen randomly, so i do not know how much upright cells or cross cells I have to fill. In my list I have all images that I want to proceed, each marke…

Getting TypeError: int object is not callable

Getting TypeError: int object is not callable. What am i doing wrong as i just want to add 10 to the z variableprint ("Hello World")x=int(input("Enter X")) y=int(input("Enter Y…

How to create a loop from 1-9 and from a-z?

I currently use:for i in range(1,10):print iWhich prints the digits 1 to 9. But I want to add a-z to the mix. How can I combine them?

Need Python to accept upper and lower case input

I want my Python code to accept both uppercase and lowercase input.Ive tried casefold, but with no luck. Any help?advice ="" while advice !=("Yes"):print("Would you like some…

What is [1] , in sock.getsockname()[1]? [duplicate]

This question already has answers here:How to access List elements(5 answers)Closed 7 years ago.I was going through socket programming in Python and I saw this: sock.getsockname()[1] Can anyone please …

How to use sys.exit() if input is equal to specific number

I am looking to correct this code so that when the user inputs 99999 then the code stops running, im also looking to make it so that if the user input is 999 it sets the total to 0 import sysdef money_…

python about linking sublist with same number together

I need to group sublists with the same elements together For example:list1 =[[1, 0], [2, 1], [30, 32]]would link [1, 0] and [2, 1] together since they both contain 1 and those two would combine into [0…

what to fix to solve the TypeError: an integer is required (got type str) [duplicate]

This question already has answers here:How can I read inputs as numbers?(10 answers)Closed 6 years ago.I have a problem with the choice of calculation years. python flux2nc.py ../data/output/fluxes/ …