Home > other >  Excuse me how python to extract certain key/value pair from the dictionary?
Excuse me how python to extract certain key/value pair from the dictionary?

Time:09-19

Rt, small white for help is how to extract a particular key name of the key/value pair

CodePudding user response:

KEY in the dictionary is a unique, specific KEY name refers to what?

CodePudding user response:

 li=[1, 3, 4, 5, 6, 6, 5, 4] 
Dict_a={
"Zhang SAN" : 'male',
"Bill" : 'female',
"Two" : 'confidential'
}
Dict_li={
'a' : 1,
'b' : 'hello',
'c' : li,
'd' : {" ABC ", 123, "hello", 123, 'ABC'},
'e' : dict_a,
'f' : {30, "bill" : "* *" : 20, "two" : 18,}

}
Print (dict_li [' a '])
Print (dict_li [' b '])
Print (dict_li [' c '])
Print (dict_li [' c '] [2])
Print (dict_li [' d '])
Print (dict_li [' e '])
Print (dict_li [' e '] [' zhang '])
Print (dict_li [' f '])
Print (dict_li [' f '] [' zhang '])


Output:
1
Hello
[1, 3, 4, 5, 6, 6, 5, 4]
4
{' ABC '123,' hello '}
{' ABC '123,' hello '}
{' zhang ':' male ', 'bill' : 'female' and 'two' : 'confidential'}
Male
{' bill ': 30,' zhang SAN: 20, 'the king 2:18}
20


CodePudding user response:

 dict1={1: 'Joe', 2: "bill"} 
Print (dict1 [1])

For the key and the value in dict1. The items () :
Print (key, value)

For the key in dict1. Keys () :
Print (dict1 [key])

CodePudding user response:

reference 1st floor paullbm response:
dictionary of KEY is unique and specific KEY name refers to what?
, for example, some key names the same dictionary, want to press a key value to the size of the corresponding value (such as a score) will be ordered these dictionary from big to small, how can you achieve

CodePudding user response:

M0_46502506
reference 4 floor response:
Quote: refer to 1st floor paullbm response:
keys of the dictionary is unique, specific KEY name refers to what?
, for example, some key names the same dictionary, want to press a key value to the size of the corresponding value (such as a score) will be ordered these dictionary from big to small, how to achieve


 dict1={1: 'Joe Smith, 6: "li si, 3:" bill, "7:" bill ", 4: "bill"} 
Print (dict1 [1])


Print (list (set ([_ for _, dictv1 dict1 in items (a) if dictv1=='bill'])))

CodePudding user response:

 # this mean? 
D=[{34, 'A' : 'C' : 55, 'd' : 23}, {' A ': 66,' B ': 33}, {' C' : 99, 'A' : 1, 'd' : 22}, {' B ': 12,' A ': 65}]

R=sorted (d, key=lambda x: x [' A '])
Print (r)
  • Related