Data Types - dictionaries and collections

First, Dictionary

1, the dictionary representation

Dictionary definition: by {} are represented Dictionary

Each data dictionary is composed of a key-value pair  Key: value , each key-value pairs separated by a comma

Dictionary key is unique (not repeated)

key only immutable data type (recommended key entire string)

key corresponding value value can be any data type

2, the dictionary of additions and deletions to change search

(1) add an element

By direct assignment key, you can increase the element

dic={"name":'xiaoming',"age":12}
dic["height"]=1.70

update method

Add multiple elements

(2) modify the element

 Modified by a value corresponding to the specified key

dic["height"]=1.8

(3) Find the elements

Find the key corresponding to the key values, if there is no error will

print(dic[‘aa’])

get method 

Find the key values corresponding to the key, if the key does not exist, returns None

dic={"aa":11,"bb":22,"cc":33}
print(dic.get("aa"))

keys method

Gets a dictionary of all the key

print (list (dic.keys ()) ) # to get into a list of key

value method

Get all the values ​​in the dictionary 

print(list(dic.values()))

items Methods

Get all the key-value pairs

print(list(dic.items()))

(4) remove elements

pop method

Delete the specified key by key pair

dic={"aa":11,"bb":22,"cc":33}
dic.pop("aa")
print(dic)

popitem method

Delete the last key-value dictionary of ( 3.5 before randomly delete key-value pairs contained 3.5 )

dic.popitem ()

clear method

Empty dictionary

Second, the collection

1, the representation of the set

Defining a set of: by {} expressed

Define an empty set: The set keywords

set1=set()
print(type(set1))

Data set is a variable type

2, a collection of properties

(1) the set of non-duplicate element (for a list of fast de-emphasis)

(2) only the set of data stored immutable type (variable type and hard type for distinguishing data)

The definition of a collection, put the data collection will not be seen in error

 

 

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/python-squirrel/p/11863000.html