python, list, tuple, dict, set summary

list (directory):

Variable content, may be repeated, for example:

>>> a=['wen','wen']
>>> a
['wen', 'wen']

By a [0] to access, use append, pop, insert operation.

tuple (component):

SUMMARY immutable, may be repeated, for example:

>>> c=(1,1,2)
>>> c
(1, 1, 2)

When only a tuple element of this will be necessary assignment:

>>> t=(1,)
>>> t
(1,)

"Variable" tuple:

>>> p=(1,a)
>>> p
(1, ['wen', 'wen'])
>>> a[1]='love'
>>> p
(1, ['wen', 'love'])

Tuple does not actually change, p (1) only a point rather than a content of the operation, but as a list is variable, looks tuple "variable" in

p [0] accessible through.

dict (dictionary):
(Key-value) assignment mode, can not be repeated, replacing duplicate content front the same content, for example:

>>> a={'wen':1,'wen':2}
>>> a
{'wen': 2}
>>> len(a)
1

Read the contents:

>>> d={'wen':1}
>>> d
{'wen': 1}
>>> d['wen']
1

Dict tuple can be used as a key (the contents of which can not be changed), re-assignment of key value will wash away the old value:

>>> d={'wen':1,c:2}
>>> d
{'wen': 1, (1, 1, 2): 2}
>>> d[c]
2
>>> d['wen']=4
>>> d
{'wen': 4, (1, 1, 2): 2}

At this time, if the c become a tuple from a list, the result would happen then?

>>> c=['wen']
>>> d
{'wen': 4, (1, 1, 2): 2}
>>> c
['wen']

Can be seen that d = { 'wen': 1, c: 2} while statement, d point to c, and c copied content is stored to itself, but changes the contents of c, c, although the content itself, but not point c, resulting in not visit d [c]

>>> d[c]
Traceback (most recent call last):
  File "<pyshell#128>", line 1, in <module>
    d[c]
TypeError: unhashable type: 'list'

Direct access to the value of the tuple or tuple operations:

>>> d[(1,1,2)]
2

So dict the key still is not repeatable, immutable.

Determine whether there is a key:

One kind through in

>>> '3' in d
False

Or use get (), if the key does not exist, you can return None (interactive environment in python does not display the results), or to specify their own value:

>>> d.get('3')
>>> d.get('3', -1)
-1

  

It may not be input value (but this time with no sense of dict, as with the set below):

Assignment:

>>> b={'wen'}
>>> b
{'wen'}

It will not be repeated:

>>> b={'wen','wen'}
>>> b
{'wen'}

Tuples can be used as key:

>>> a=(1,1,2)
>>> b={'wen',a}
>>> b
{'wen', (1, 1, 2)}

By D [key] to access, with d.pop (key) delete key and the corresponding value.

SET, a key group set, no repeat, only one set of parameters:

Assignment:

>>> s = set([1, 2, 3])
>>> s
{1, 2, 3}
>>> s = set([1, 1, 3])
>>> s
{1, 3}
>>> s = set([1, 2, 3],[2,3,4])
Traceback (most recent call last):
  File "<pyshell#188>", line 1, in <module>
    s = set([1, 2, 3],[2,3,4])
TypeError: set expected at most 1 arguments, got 2

Obviously when the time parameter is 2, the program will be given, the actual SET is a set of unordered, no duplicate collections. (Note that the order is disordered, but output element)

set can also be used to make the intersection or union, as detailed in the link: https: //www.cnblogs.com/xikl/p/12014317.html

Elements can be added by add (key) (tuples can also be added), remove (key) can remove elements

 

>>> c
(1, 2, 3)
>>> s.add(c)
>>> s
{1, 3, (1, 2, 3)}
>>> s.remove(c)
>>> s
{1, 3}

Guess you like

Origin www.cnblogs.com/xikl/p/12014421.html