Ganso & Dictionary

# What is Ganso: Ganso is an immutable list (no need to change)

# ====================================== ========= Basic use =============================
# 1, uses: for storing a plurality of values, when a plurality of values stored in the read only when using the most appropriate neuron progenitor demand does not need to change

# 2, is defined by: in () with, any type of a plurality of divided value
T = (1,3.1, 'AAA', (l, 2,3), [ 'A', 'B'])
T tuple = (...)
RES = tuple ( 'Hello')
RES = tuple ([ 'X':. 1, 'Y': 2])
Print (RES)

# 3, + common operations built-in method
# priority control operation:
# 1, according to the index value (Forward + Reverse take take): only take
T = ( 'A', 'B', 1)
T [0 ] = 111


# 2, a slice (care regardless of the end, step)
T = ( 'H', 'E', 'L', 'L', 'O')
RES = [. 1:. 3] EL
Print (RES)
Print ( t) can not be changed without changing the original trimming
# 3, the length
T = ( 'H', 'E', 'L', 'L', 'O')
Print (len (t))
#. 4, operation members and in in Not
T = ( 'H', 'E', 'L', 'L', 'O')
Print ( 'H' in T) True

#5、循环
t=('h','e','l','l','o')
for item in t:
print(item)

 

The type of summary ======== # ====================================== ============================
# store multiple values

# ordered

# Immutable (1, Variable: variable value, id == not constant variable hash 2, immutable: variable value, id == becomes immutable can hash)
tuple variable list of immutable refers to the memory address may become
T = (. 1, 'a', [ 'X', 'Y', 'Z'])
Print (ID (T))
T [2] [0] = 'X-'
Print ( T)
Print (ID (T))

# Master Method
T = ( 'A', 'B', 'A')
Print (t.index ( 'A')) is the subscript 0
t.index ( 'XXX')
Print (t.count (A)) A total of two

Dictionary type
# ====================================== Basic use ======= ===============================
# 1, Usage: used to store a plurality of values (of different kinds)

# 2, is defined by: {} in the plurality of separated by a comma key: value element, wherein the value may be a hot data type, and the key function is generally used to describe the value of the key it is usually a string type, but In fact, key must be immutable type (int \ a float \ STR \ tuple)
D = {0: 'X', 1.3: 'Y', 'K1': 'Z', (l, 2,3): 'AAA' }
Print (D [(2,3)]) # key is the dictionary output (1,2,3) value
D = dict (...)
Print (D [(2,3)])
d1 = {[1,2,3]: ' aaa'} so defined may not

d = dict ([( 'k1 ', 1), ( 'k2', 2), ( 'k3', 3)]) # converts this data into a dictionary type of
print (d) output of the entire dictionary

dict = D ((( 'K1',. 1), ( 'K2', 2), ( 'K3',. 3)))
Print (D) may be replaced by ancestral first value corresponds to a second key value equivalent value

L = [( 'K1',. 1), ( 'K2', 2), ( 'K3',. 3)]
D = {}
for T in L:
Print (T) # extracted three small neuron progenitor
d [t [ 0]]

l=[('k1',1),('k2',2),('k3',3)]
d={}
for t in l:
k,v=t
d[k]=v
print(d)
d=dict(l)
print(d)

d=dict(x=1,y=2,z=3)
print(d)


# 3, + common operations built-in method
# priority control operation:
# 1, the access key press value: may be stored preferably
D = { 'X': 1}
Print (D [ 'X']) # 1
Print (ID (D))
D [ 'X'] = 1111
Print (D)
Print (id (D))
invariant description id where the dictionary is present key variable type

the key does not exist where
D [ 'Y'] = 2222
Print (D)
may be the value which
L = [ 'A', 'B']
L [2] = 'C' can not be given assignment #

Index does not exist can not be entered, a list of bonus
dictionaries can be added value in the absence of key inside


# 2, the length len
D = { 'X':. 1, 'Y': 2, 'Z': 3}
Print (len (D))
length is 3
# 3, membership operator in and Not in
D = { 'X ':. 1,' Y ': 2,' Z ':. 3}
Print (in. 1 D) to false
Print (' X 'in D) to true
dictionary key according to the operation member

# 4, deleted
D = { 'X':. 1, 'Y': 2, 'Z':. 3}
del D [ 'Y']
Print (D)

= {D 'X':. 1, 'y': 2, 'Z':}. 3
RES = d.pop ( 'y') y # Returns the value corresponding to the
print (res) #res of 2
Print (D) #y be deleted

d.popitem = RES ()
Print (RES) to remove the returned # Key: value
Print (d) d # deleted after the output value

A dictionary is deleted randomly disordered popitem
# 5, key Keys (), the value of values (), on the key-value items ()
D = { 'X':. 1, 'Y': 2, 'Z':}. 3
D .keys () output Key
Print (List (d.keys ()))
d.values () output value
Print (List (d.values ()))
d.item () output dictionary
print (list (d.items ( )))

 

# 6, circulation


in d.keys K for ():
Print (K)

for K in D:
Print (K) is the direct output cycle # default Key

Print (List (d.keys ()))
for V in d.values ():
Print (v)
Print (List (d.values ()))
for k, v in d.items ():
Print (k, v)
Print (List (d.items ()))

The type of summary ======== # ====================================== ============================
# save a value or store multiple values

# ordered or unordered

# Variable or non-variable (a variable: variable value, id == not constant variable hash 2, immutable: variable value, id == becomes immutable may be the hash.)


# Need to know the operation
D = { 'name': 'Egon', 'Age': 18 is, 'Sex': 'MALE', 'hobblies': [l, 2,3]}
V = d.get (' namexxxx ') does not exist # key returns None given not
print (v)

v1 = d.get ( 'name') # return value value
Print (V1)

update discharge method
D = { 'name': 'Egon', 'Age': 18 is, 'Sex': 'MALE', 'hobblies': [l, 2,3]}
d.update ({ 'X':. 1 , 'name': "EGON"})
Print (d) d # update key does not have the key added modified (updated)

#fromkeys: create a new demand for rapid dictionary, the initial value of all values of None, and the key from a list of
Keys = [ 'name', 'Age', 'Sex']
D = {} fromkeys (Keys. , None) #keys d is the key value are None
Print (d)
corresponds to the following codes:
Keys = [ 'name', 'Age', 'Sex']
d = {}
for K in Keys:
d [K ] = None
Print (D)


#setdefault

According to the default operation form
D = { "X":. 1, "Y":}. 3
D [ 'X'] = 1111
D [ 'Z'] = 12345
present value on the change value does not exist

According to the situation setdefault
D = { "X":. 1, "Y":}. 3
RES = d.setdefault ( 'X': 11111) # key without modifications presence value is returned the original value
Print (D)
Print (res)

res = d.setdefault ( 'z', 11111) # add a new value is returned in the case where the added value of the key does not exist
Print (D)
Print (RES)

#练习
s='hello alex alex say hello sb sb'
#d={'hello':2,'alex':2,'say':1,'sb':2}

} = {D
words s.split = ()
Print (words)
for words in Word:
#d [Word] = words.count (Word) #d [ 'Hello'] = word.count ( 'Hello') each of # repeats the operation to add the following code optimization
d.setdefault (Word, words.count (Word))


Print (D)
# [ 'Hello', 'Alex', 'Alex', 'say', 'Hello', 'SB', 'SB']
# { 'Hello': 2, 'Alex': 2, 'say':. 1, 'SB': 2}

 

for word in words:
d[word]=words.count(word)
print(d)

print(words)
d['hello']=words.count('hello')
d['alex']=words.count('alex')
print(d)


Supplementary ancestral symbol
T = (. 1,)
Print (T, type (T))
means back to the ancestral element must be in a comma without the comma () is included


name=input('>>:').strip()
res='my name is %s' %name
print(res.center(50,'*'))

Two-step and do the step
() is a usage that he has included the meaning of
name = the INPUT ( '>>:'). Strip ()
Print (( 'My name IS% S'% name) .center (50, '* '))

 

Guess you like

Origin www.cnblogs.com/liugangjiayou/p/11601053.html