day02 python learning

Today, to summarize from the list of tuples, dictionaries three aspects
a, list
1, the role of: storing a plurality of values according to the position
2, the definition of
L = [1, 1.2, 'A']
Print (type (L))
. 3, the type of conversion : whenever loop can be traversed for the type of parameters are passed as a list () turn into a listing
RES = list ( 'Hello')
Print (RES)
RES = list ({ 'K1': 111, 'K2': 222, 'K3'})
Print (L)
, whether or value assignment operation: absence of the error index
L [. 3] = 333
2, the slice (care regardless of the end, step)
L = [111, 'Egon', 'Hello', 'A', 'B', 'C', 'D', [. 1, 2,. 3]]
Print (L [0:. 3])
Print (L [0:. 5: 2]) # 0 . 4 2
new_l L = [:] # slice being identical copy behavior, and corresponds to the shallow copy
3, the length of
Print (len ([. 1, 2, 3]))
. 4, the members in operation in and Not
Print ( 'AAA' in [ 'AAA',. 1, 2])
#Print (. 1 in [ 'AAA',. 1, 2])
#. 5, the value is added to the list
# 5.1 added
l = [111, 'egon' , 'hello']
l.append (3333)
5.2, interpolated values
L = [111, 'Egon', 'Hello']
l.insert (0, 'Alex')
Print (L)
5.3, adding Extend value
new_l = [1,2,3 ]
l = [111, 'egon', 'Hello']
l.extend (new_l)
Print (L)
. 7, delete
way: common deletion method, simply delete no return value
l = [111, 'egon ',' the Hello ']
del L [1]
the X-del = L [1] # throws an exception, does not support the assignment syntax
print (l)
Second way: l.pop () to delete the index, returns the value to delete
l = [111, 'Egon', 'Hello']
l.pop () # index is not specified by default remove the last
l.pop ()
Print (L)
RES = l.pop (. 1)
Print (L)
Print (RES)
mode three: l.remove () The element removal, return None
L = [111, 'Egon', [l, 2,3], 'Hello']
l.remove ([l, 2,3])
Print (L)
l.remove = RES ( 'Egon')
Print (RES) # None
. 8, the cycle
L = [. 1, 'AAA', 'BBB']
for X in L:
l.pop (. 1)
Print (X)
need to have the operation
= L [. 1, 'AAA', 'BBB', 'AAA', 'AAA']
. 1, l.count ()
Print (l.count ( 'AAA'))
2, l.index ()
Print (L. index ( 'aaa'))
Print (l.index ( 'aaaaaaaaa')) # can not find the error
3, l.clear ()
Print (L)
4, l.reverse (): not the sort that list upside down
= L [. 1, 'Egon', 'Alex', 'LXX']
l.reverse ()
Print (L)
. 5, l.sort (): the list of elements must be of the same type can sort
l = [11, -3,9,2,3.1]
l.sort () # default from small to large, called ascending
l.sort (reverse = True) # descending row, set descending
Print (L)
L = [ . 11, 'A', 12 is]
l.sort ()
L = [ 'C', 'E', 'A']
l.sort ()
Print (L)
is understood: the size of the string can be compared, according to the character position corresponding sequence pk
size of the string is in the order to be distinguished ASCI code table, the table behind the front row of characters is greater than
Learn: list may be different than the size of the same string, the principle, but the element corresponding to the position of the same type to be
L1 = [. 1, 'ABC', 'Zaa']
L2 = [. 1, 'ABC', 'ZB']
print (l1 <l2)
added
1, queue: FIFO, FIFO
l = []
enqueue operation
l.append ( 'First')
l.append ( 'SECOND')
l.append ( 'THIRD')
Print (L )
dequeue
Print (l.pop (0))
Print (l.pop (0))
Print (l.pop (0))
2, the stack: LIFO, Last In first out
l = []
stack operation
l.append ( 'First')
l.append ( 'SECOND')
l.append ( 'THIRD')
Print (L)
dequeue
Print (l.pop ())
Print (l.pop ())
print (l.pop ())
Second, the tuple
of tuples is "a list of immutable"
# 1, action: according to the index / position storing a plurality of values, for reading not only for upgrading

A plurality of spaced apart # 2 of any type, defined :() the comma elements
T = (1, 1.3, 'AA')
T = tuple ((1, 1.3, 'AA'))
Print (T, type ( T))
X = (10) # single brackets denote comprising means
Print (X, type (X))
T = (10,) # if the tuple is only one element, must be a comma
print (t, type (t ))
T = (1, 1.3, 'AA') # T = (0-> 1 memory address value, 1-> memory address value 1.3, 2> value 'aaa' memory address)
3, built the method of
the priority control operations:
by index values (forward + reverse take take): only take
T = ( 'AA', 'BBB', 'CC')
Print (T [0])
#Print (T [- 1])
sections (care regardless of the end, step)
T = ( 'AA', 'BBB', 'CC', 'dd', 'Eee')
Print (T [0:. 3])
Print (T [: : 1])
length of
T = ( 'AA', 'BBB', 'CC', 'dd', 'Eee')
Print (len (T))
members in operation in and Not
Print ( 'AA' in T)
Third, Dictionary
1, is defined: {} key within a plurality of spaced apart by commas: value, which value can be any type, but the key must be immutable and can not be repeated
make a dictionary manner:
D = { 'K1': 111 , (l, 2,3): 222}
D} = {# out of the defined default empty dictionary
print (d, type (d) )
embodiment made of two dictionaries:
D = dict (X =. 1, Y = 2, Z =. 3)
Print (D, type (D))
2, data type conversion
info = [[ 'name', 'Egon'], ( 'Age', 18 is), [ 'Gender', 'MALE']]
D = {}
for K, V in info: # K, V = [ 'name', 'Egon'],
D [K] = V
Print (D)
mode made dictionary three:
RES = dict (info) # line of code get above for loop operation print (res)
made dictionary four ways: fast initialization a dictionary
Keys = [ 'name', 'Age', 'Gender']
D = {}
for K in Keys:
D [K] = None
Print (D)
D = {}. fromkeys (Keys, None) # line of code to get the work cycle described above for

3, the built-in method of
the priority control operations:
1, by the access key values: can be kept desirable
for assignment: key is present, is modified
for the assignment operator: key does not exist, create a new value
2, the length len
D = { 'K1 ': 111,' K2 ': 2222,' K1 ': 3333,' K1 ': 4444}
Print (D)
Print (len (D))
. 3, member operator in and not in: The Key
D = {' K1 ' : 111, 'K2': 2222}
Print ( 'K1' in D)
Print (111 in D)
. 4, deleted
D = { 'K1': 111, 'K2': 2222}
4.1 universal remove
del d [ 'k1' ]
Print (D)
4.2 POP delete: delete key element according returns the delete key value corresponding to the value of
RES = d.pop ( 'K2')
Print (D)
Print (RES)
4.3 popitem delete: delete random, returns a tuple (key deletion, the deleted value)
RES = d.popitem ()
Print (D)
Print (RES)
. 5, key keys (), the value of values (), on the key-value items () => is obtained in python3 old hen
= {d 'K1': 111, 'K2': 2222}
'' '
. 6, for cycles
for K in d.keys ():
Print (K)
for K in d:
Print (K)
need to have a built-in method
d {= 'K1': 111}
. 1, d.clear ()
2, d.update ()
d.update ({ 'K2': 222, 'K3': 333, 'K1': 111111111111111})
Print (D)
3, d.get (): the key value, good fault tolerance
print (d [ 'k2'] ) # key does not exist, an error
Print (d.get ( 'K1')) # 111
Print (d.get ( 'k2')) # key is not present is not given, returns None
. 4, d.setdefault ()
4.1 if the key has not added, the dictionary returns a value corresponding key
info = { 'name': 'Egon'}
RES = info .setdefault ( 'name', 'Egon')
Print (info)
Print (RES)
4.2 If the key is not added, the dictionary returns a value corresponding key
info {} =
RES = info.setdefault ( 'name', 'Egon' )
print(info)
print(res)

Released five original articles · won praise 2 · views 65

Guess you like

Origin blog.csdn.net/weixin_43138641/article/details/104806298