Home > other >  Python problem dictionary structure
Python problem dictionary structure

Time:09-19

To achieve the following dictionary:
{id: 4, name: "t1"}, {5, id: name: "t2"}

Functions are defined as follows
Def (clustername, * args)
* args of input is the name of the value in the dictionary, id is other function called by the name of the value obtained, the name and id corresponding
Def XXXXX (clustername, * args) :
Data_Dic={}
For I in args:
Name=I
Id=STR (ZZZZZZ (clustername, name))
Data_Dic (" id ")=id
Data_Dic/" name "=I
Print (data_Dic)
Find it wrote after can output a dictionary
{' id ':' 5 ', 'name' : 't2'}
There is a problem is not clear whether the for here

CodePudding user response:

Data_lst=[]
For I in [' a ', 'b', 'c'] :
For j in [1, 2, 3] :
Data_Dic={}
Data_Dic (" id ")=j
Data_Dic/" name "=I
Data_lst. Append (data_Dic)
Print (data_lst)

To establish a list of the for loop generated by the dictionary, in turn, added to the list

CodePudding user response:

Young man, the feature of the dictionary first figuring out ha!
First you say want to achieve the following dictionary:
{id: 4, name: "t1"}, {5, id: name: "t2"}
The key is the only dictionary,
If you are in a dictionary, behind the same key value is sure to cover the front!
You this 2 sets of data have id and name, so certain will be overwritten!
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- unless it's like this:
{' key1: {id: 4, name: "t1}", "key2" : {id: 5, name: "t2"}}
This is a big dictionary contains a small dictionary! Thus increased the complexity of the problem, so also do not recommend you to construct!

In fact, you can put your every dictionary in list
[{id: 4, name: "t1"}, {5, id: name: "t2"},... {}, {}.
However, in the list of the dictionary, in the search efficiency instead, reduce a lot of

=============================================
So, if your id is the only can be determined, then you can be so regular way to construct a dictionary!
{' id4 ':' t1 ', 'id5' : 't2'... }

Look at your actual demand to choose!



CodePudding user response:

The problem of assembling double, dictionary is included in the list, or in a dictionary, if you want to know, a single dictionary, can only store a set of data

CodePudding user response:

With the array of the dictionary, so has good points
  • Related