Python List-Dictionary

Asked 2 years ago, Updated 2 years ago, 26 views

Attempting to recall dictionaries after saving them to the list

li=[]

dic={}

afterwards Run the code below in the repeat statement.

dic['digit']=int(input('digit number'))


dic['price' = int('select menu'))

li.append(dic)

What should I do if I want to save dictionaries in the list and enter a seat number to get the price corresponding to the place?

li=[{'digit':'price'}{'digit':'price'}{'digit':'price'}...] I just want to save it like this and get a price that matches the place I entered.

python

2022-09-22 17:56

1 Answers

Is it necessary to do that with the data structure?

I think it's a structure that consists of one dictionary.

The digit number will be unique, so you can use the digit key to set the price to the value.

In [1]: D = {}                                                                  

In [2]: D['1'] = 1000                                                           

In [3]: D['2'] = 2000 

In [4]: D                                                                       
Out[4]: {'1': 1000, '2': 2000}

But if I have to do it like a question...There are also the following methods.

In [15]: li=[{'1':'1000'},{'2':'2000'},{'3':'3000'}] 

In [16]: next(d for d in li if '2' in d)                                        
Out[16]: {'2': '2000'}


2022-09-22 17:56

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.