Nested lists and dictionaries

zd = {
    'shen': ['python','java','ruby'],
    'yuan': ['java'],
    'chen': ['c++'],
    'long': ['delphi'],
    'huge': ['ruby'],
  }

for K, V in zd.items ():      # simultaneous traversal key and a value item method, simultaneous traversal keys and values 
    Print ( ' \ n- ' + K)
     for I in V:
         Print (I)

print('------------------------------------------------')

for k in zd.keys ():             # traversing all of the dictionary keys 
    Print (k)


print('------------------------------------------------')

for i in zd.values ():            # traversing the dictionary value 
    Print (i)

print('+++++++++++')

for I in ZD:                    # traversing the dictionary values 
    Print (ZD [I])

print('------------------------------------------------')

print (zd.items ())

ssx = list(zd.items())

print(ssx[0])
print(ssx[1])

print('+++++++++++++++++++++++++')

for i in ssx:
    print(i)


print('------------------------------------------------')

Print (zd.values ())          # print values of the dictionary, a return value is a list of the list does not contain any key 

SX = List (zd.values ())       # List making a list of

print(sx)

print('+++++++++++++++')

print(sx[0])

print('------------------------------------------------')

Print (zd.keys ())     # Returns a list of all the keys, i.e., is returned a list containing all keys 

CP = []

cp = zd.keys ()

for i in cp:
    print(i)


==========================================================================
==========================================================================
==========================================================================
==========================================================================



Results of the:


shen
python
java
ruby

yuan
java

chen
c++

long
delphi

huge
ruby

------------------------------------------------

shen
yuan
chen
long
huge

------------------------------------------------

['python', 'java', 'ruby']
['java']
['c++']
['delphi']
['ruby']

+++++++++++

['python', 'java', 'ruby']
['java']
['c++']
['delphi']
['ruby']

------------------------------------------------

dict_items([('shen', ['python', 'java', 'ruby']), ('yuan', ['java']), ('chen', ['c++']), ('long', ['delphi']), ('huge', ['ruby'])])
('shen', ['python', 'java', 'ruby'])
( ' Yuan ' , [ ' java ' ])

+++++++++++++++++++++++++

('shen', ['python', 'java', 'ruby'])
( ' Yuan ' , [ ' java ' ])
('chen', ['c++'])
('long', ['delphi'])
('huge', ['ruby'])

------------------------------------------------

dict_values([['python', 'java', 'ruby'], ['java'], ['c++'], ['delphi'], ['ruby']])
[['python', 'java', 'ruby'], ['java'], ['c++'], ['delphi'], ['ruby']]

+++++++++++++++

['python', 'java', 'ruby']

------------------------------------------------

dict_keys(['shen', 'yuan', 'chen', 'long', 'huge'])
shen
yuan
chen
long
huge

==========================================================================
==========================================================================
==========================================================================
==========================================================================



List1 = [ ' key1 ' , ' key2 ' , ' key3 ' ]

List2 = [ ' . 1 ' , ' 2 ' , ' . 3 ' ]                 # in zip merge function


LB = List (ZIP (List1, list2))                 # returns a list

print(lb)

# ------------------------------------------------- --- 

d = {}

ZD = dict (ZIP (List1, list2))         # returns the dictionary

print (d)

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Results of the:

[( ' Key1 ' , ' . 1 ' ), ( ' key2 ' , ' 2 ' ), ( ' key3 ' , ' . 3 ' )]


{ ' Key1 ' : ' . 1 ' , ' key2 ' : ' 2 ' , ' key3 ' : ' . 3 ' }


============================================================================
==============================================================================

===============================================================================


The list of transformations to the dictionary:


new_list= [['key1','value1'],['key2','value2'],['key3','value3']]

d = {}

zd  = dict(new_list)

print (d)


Results of the:


{'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}


=================================================================================

Method Two:


new_list= [['key1','value1'],['key2','value2'],['key3','value3']]

new_dict = {}

for I in new_list The:
     Print (I)                #   I is: [ 'key1', 'VALUE1'] 
    new_dict [I [0]] = I [. 1]    # I [. 1] as follows: left right side value1 key value
    
                          # I [0] is: key1 
# new_dict [key1] # = VALUE1 increase k, v values

print(new_dict)


Results of the:


['key1', 'value1']
['key2', 'value2']
['key3', 'value3']
{'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}

Guess you like

Origin www.cnblogs.com/xiaobaibailongma/p/12064729.html