Python_Note_6–dictionary 基本操作

Here are some usage for dict in python. 1.fotmat cities = {‘BJ’: ‘Beijing’, ‘SH’: ‘Shanghai’, ‘TJ’: ‘Tianjing’, ‘XA’: “Xi’an”, ‘SZ’: ‘Shenzhen’} 2.add items cities[‘NJ’] = ‘Najing’ 3.del items del cities[‘TJ’] 4..pop() items cities.pop(‘TJ’) 5..copy(), cities_2 is a copied dict of cities. cities_2 = cities.copy() 6..clear() print cities.clear() # result is {} 7..has_key() print cities.has_key(‘BJ’) # … Read morePython_Note_6–dictionary 基本操作