Learning python ten (tuples, dictionaries, collections)

Tuple (tuple) 
tuples are immutable sequence, and its operation is substantially the same list, in general, when we want to use the data without changing the rest of the case of using a list of tuples

created using the tuple ()
my_tuple = () # create an empty tuple
Print (of the type (my_tiple))
my_tuple = (1,2,3,4,5)
Print (my_tuple)
tuple is an immutable instinct to try to tuple elements reassigned
my_tuple = (1 , 2,3,4,5)
my_tuple [. 1] = error message # 10: TypeError: 'tuple' item assignment object does not support
the tuple sections may be used
Print (my_tuple [:: -. 1])
Print (my_tuple [0 : 2])
Print (my_tuple [0:. 3: 2])
....
when not empty tuples ten-membered, may be omitted ()
my_tuple = 1,2,3,4
Print (my_tuple, type (my_tuple))
If the tuple is not a null set, which it must have at least one number
my_tuple. 1 =
Print (my_tuple, type (my_tuple)) # is not a tuple
my_tuple = 1,
print (my_tuple, type (my_tuple) ) # tuple is a

solution tuple packet
unpack them tuple refers to each element is assigned to a variable
my_tuple = 1,2,3,4
A, B, C, D = my_tuple
Print (a)
Print (B)
Print (C)
Print (D)
when the tuple unpacking, the number of variables must be the number of tuples of elements consistent, it may be a variable preceded by an asterisk, and All variables obtained tuples remaining elements
can not both be a plurality of variable numbers *
a, B, C * = my_tuple
Print (a, B)
Print (C)
as long as a sequence can unpack

mutable objects
- each object are stored three data:
ID (labeled) type (type) value (value)

- a variable object list is
a = [l, 2,3]
- a [0] = 10 (subject to change)
- this operation is to modify the value of the object by a variable
- does not change the object variable points

- a = [4,5,6] (amount of change)
- This operation is reassigned to a variable
- the object change object points
example:
A = [l, 2,3]
Print (A, ID (A))
by modifying the object index
A [0] = 10
Print (A , id (a)) # id number does not change
the variable value reassignments
a = [4,5,6]
Print (a, id (a)) # id number will change the

dictionary (dict)
- belongs to a new dictionary data structure, called mapping
- dictionary and a list of similar effect, are subject to storage container
- the list of performance data stored horizontal well, but the poor performance query data
- in the dictionary each element has a unique name, you can quickly check to specify the elements by name
- at the time of the query elements, Dictionary efficiency is very fast

to use {} to create a dictionary
d = {} # empty dictionary
print (type (d))
syntax:
{Key: value , key: ....} value
D = { "a": "a", "B": "B", "C": "C"} # a key: value (key:value)
Value (value) dictionary can be any object, but the dictionary key (key) can be any immutable object
dictionary key is not repeated, if the query is repeated back will replace the foregoing
d = { "a" : "a", "b" : "B", "c": "C", "a": "Aa"} # is replaced with a preceding change a

dictionary used:
using dict () function to create Dictionary
d = dict (name = "Bob", age = "12") # each parameter is a key-value pair (dictionary created in this way, key are strings)
Print (D, type (D))

sequence may be the binary sequence is converted into a dictionary containing,
binary sequence, the sequence of only two values, [1,2], (a, 2), "ab &"
D = dict ([( "a" , "a"), ( "B", "B")])
Print (D, type (D))

len () may acquire the number of key-value pairs in the dictionary
len (D)

in checking whether the dictionary specified key
print ( "a"in D)

Not in check dictionary does not contain the specified key
print ( "c" not in d )

the need to get the value according to the key
print (d [ "a"] , d [ "c"], d [ "b"] )

get (key [, default]) method is used to obtain the modified character string according to the key, if the value is not acquired, a return None
D = dict (name = "Bob", Age = "12 is")
Print (d.get ( "a")) returns a # None
Print (d.get ( "a", "default value")) # can specify a default value, as a second parameter, the default value for this acquisition time is less than the value of

modified Dictionary
d = dict (name = "Bob", Age = "12 is")
D [ "name"] = "red" # key exists cover
Print (D)

D [ "address"] = "Shanghai" # key without Add exists
Print (D)

x.setdefault (, default) can be used to add to the key-value pairs in the dictionary, the dictionary if the dictionary will not be saved to make the operation, will be added if not present,
D = dict ( name = "Bob", Age = "12 is")
d.setdefault ( "address", "Shanghai")
Print (D)

x.update ([oTHER]) the other key-value dictionary of the dictionary is added to the current
d = { "a":"A","b":"B","c":"C"}
a = {"e":"E","f":"F","g":"G"}
d.update(a)
print (d)

remove, can be used to remove del keys of the dictionary of
D = { "A": "A", "B": "B", "C": "C"}
del D [ "B"]

x.pop (key [, default]) deleted according to the dictionary of key value pairs, will be key to remove and return if the default value is specified when you delete key does not exist, it does not complain, but directly return a default value
D = { "A": "A", "B": "B", "C": "C"}
d.pop ( "B")
Print ( "B")
d.pop ( "Z "" this is a default value ")
Print (D)

x.clear () to clear the key-value dictionary of
d = {" a ":" a "," b ":" B "," c " : "C"}
d.clear ()
traversing the dictionary
all x.keys () this method returns a dictionary Key
D = dict (name = "Bob ", age =" 12 is ")
for I in d.keys ():
Print (D [I])

x.values () This method returns all dictionary values
D = dict (name =" Bob ", age = "12")
I in d.values for ():
Print (I)

x.items () This method returns all key-value pairs in the dictionary, it will return a sequence, the sequence contains binary sequences, respectively dictionary binary and the key values
D = dict (name = "Bob", Age = "12 is")
for K, V in d.items ():
Print (K, "=", V)

set (sET)
- sets or lists very similar
- except that:
1. the set of immutable objects can only store
2. collection objects are stored unordered
3. duplicate set instinctive element
used to create a set of {}
s = {10,23,13, } -4
Print (S, type (S))

using the set () to create a set of
s = set () # Create an empty set s = {} is an empty dictionary
can be set () sequence conversion dictionary and a set of I
s = sET ({ "a":. 1, "V": 2})
Print (S, type (S)) using # set () is set when the conversion dictionary, the dictionary contains only the keys

used x.add () adding elements
= {10,23,13 S, -4}
s.add (1333) # the Add () passing only a single parameter
print (s)

using x.update () to add an element to the current collection set, update () sequences and can be passed as a parameter dictionary, Dictionary value uses Key
S = {10,23,12}
S2 = SET ( "Hello")
s.update (S2)
Print (S)

using x.pop () random remove elements in a collection
S = {10,23,12, "a", "DGF", "FG"}
Print (s.pop ())
Print (S)

using x.remove () to delete the specified set element
s = {10,23,12, "A", "DGF", "FG"}
s.remove ( "DGF")
Print (S)

using x.clear () empty set
s = {10,23,12 , "A", "DGF", "FG"}
s.clear ()
Print (S)

Guess you like

Origin www.cnblogs.com/wangwen022/p/11305763.html