Met python -------- str string int shaping bool Boolean data tuples tu set () supplementary dictionary set dict

int and str

a = [ "Jiang Yang", "Houxiao Qing", "dragon pool", "Cheng Li", "Feng Xia"]

print (a.capitalize ()) first letter capitalized

print (a.title ()) of each word capitalized

print (a.swappcase) case conversion

print (a.center (20, "*")) centrally filling

print (a.find ( "c")) Search Search index by element, or -1 when not found

print (a.index ()) Search Search index through the elements, can not find the return on error

print ( "_". join ([ "1", "2", "2"])) spliced ​​to the list into a string

+ str str
str * 5
string plus operations, multiplication operations are opening up new space

li = [ "Jiang Yang", "Houxiao Qing", "dragon pool", "Cheng Li", "Feng Xia"]

s = "_".join(li)

print (s) -------- _ Houxiao Qing Yang Jiang Cheng Li _ _ _ longchi Feng Xia

li = "I like to spend regardless of cool."

s = "_".join(li)

print (s) ------------ Hi I __ ___ _ Huan spend __ __ Kou cool

List

lst = [1,23,4,5,7,8,9]

print (lst.index (4)) # lookup indexed by the elements

lst.sort () # default sort ascending
lst.sort (reverse = True) # DESC
print (lst)

= LST [1,23,4,5,7,8,9]
lst.sort ()
lst.reverse () # artificial descending
print (lst)

= LST [1,23,4,5,7,8,9]
lst.reverse ()
Print (LST) inverts the source data #

= LST [1,23,4,5,7,8,9]
LST1 LST = [:: -. 1]
Print (LST)
Print (LST1) # inverted without modifying the data

Interview questions:
LST = [. 1, []]. 5 *
Print (LST)
LST [. 1] .append (. 6)
Print (LST) -------- [. 1, [],. 1, [],. 1 , [], 1, [], 1, []]
[1, [6], 1, [6], 1, [6], 1, [6], 1, [6]]

Circulation delete the list each element

li = [ "Jiang Yang", "Houxiao Qing", "dragon pool", "Cheng Li", "Feng Xia"]

for i in li :

​ Li.remove(i)

print (li) ------- The result is: [ "After Little Green", "Cheng Li"] ----- during operation for the record will have a pointer to the current element which loop. a, a pointer to the beginning of zero. then get the first zero elements. this time turned out the first element will automatically become the first zero. then move the pointer back once, point 1 elements. this is the original 1 has become 0, it will not be deleted.

Look with a pop deleted

li = [ "Jiang Yang", "Houxiao Qing", "dragon pool", "Cheng Li", "Feng Xia"]

for i in range(0,len(li)):

li [i]

Print (li) ---- [ 'Houxiao Qing', 'Cheng Li'] ---- Results: error

i = 0, 1, 2 deleted when li [0] after being deleted. ⾯ rear surface becomes a first 0.

So when i = 2 time. List only one element. But this is the first time to remove two certainly being given ah

The analysis found. Cycle will not delete. Whether using del or with remove. Can not be realized.

pop ------ delete the last

li = [ "Jiang Yang", "Houxiao Qing", "dragon pool", "Cheng Li", "Feng Xia"]

EL in li for:
li.pop () # POP does not work

print (li) Results: [ "Cheng Li" "Little Green waiting"]

Only in this way can be deleted

for i in range (0, len (li)) ---------------- cycle len (li) times, then remove it from back to front

li.pop ()

print (li) ------ Results: []

Or use a list to record what you want to delete. Delete honest cycle

li = [ "Jiang Yang", "Houxiao Qing", "dragon pool", "Cheng Li", "Feng Xia"]

LI1 = []

for i in li :

​ li1 .append(i)

for i in li1 :

​ li .remove(i)

print (li)

Note that due to deletion of the elements will lead element index change, so prone to problems. Try not to recycle directly delete elements can be added to the element you want to delete and then another set of bulk deletion

.tuple Ganso

Interview:
TU = (1)
TU1 = ( "alex")
TU2 = (1,) # tuple

you = (12,3,4) + (4,5,3,4)
print (you)

tu = (1, []) * 3
print (tu)
you [-1] .append (10)
print (tu)

dict

dict in frome (), you can help us to create a dict by list

dic = dict.fromkeys ([ "jay" , "JJ"], [ " Jay", "twist vine"])
Print (DIC) ------- { 'Jay': [ 'Jay', 'cannabis vine '],' JJ ': [ ' Jay ',' twist vine ']}

Each of the foregoing list of contents as a return key, behind the list as value. Dict generated

dic = dict.fromkeys ([ "jay", "JJ"], [ "Jay", "twist vine"])

print(dic)

dic.get("jay").append("胡⼤大")

print (dic) ------ because the two key values ​​corresponding to the same one another thus changing also changes

Results:
{ 'Jay': [ 'Jay', 'twist vine', 'large zoomed Hu'], 'JJ': [ ' Jay', 'twist vine', 'Great zoomed Hu']}

--- code just changed the jay more that list. However, due to jay and JJ Use ⼀ use the same column list. So. front of that changed. Behind the change to follow

dict elements in an iterative process is not allowed to be deleted

dic = { 'k1': ' alex', 'k2': 'wusir', 's1': ' ⾦ boss' #} with the delete key 'k' of the element
for k in dic:

IF 'K' in K:
del DIC [K]
Generation not allowed when trekking into the delete operation ⾏

print (dic) --------- error

Want to delete an element temporarily stored in a first list, and then delete circulation list, then deleted

dic = { 'k1': 'alex', 'k2': 'wusir', 's1': '⾦ boss'}

= dic_del_list []
# deleted with the key 'k' elements
for i in dic:

​ if 'k' in i :
​ dic_del_list.append(i)
for el in dic_del_list:
​ del dic[el]

print(dic)

Type conversion:

Neuron progenitor => list list (tuple)

List => tuples tuple (list)

list=>str str.join(list)

str=>list str.split()

False data is converted into

0,"",None,[],(),{},set()==>False

1. Variable:
List
dict
SET
2. immutable:
int
STR
BOOL
tuple
3. Ordered:
List
tuple
STR
4. disorder:
dict
SET

Value ways:

1. Index
List
tuple
str

2. Key
dict

3. Direct
int
BOOL
the SET

Endless loop
LST = [l, 2,3]
for I in LST:
lst.append (. 4)
Print (LST)

lst = [11,22,33,44]
for i in lst:
lst.remove(i)
print(lst)

for i in lst:
del lst[-1]
print(lst)

lst = [11,22,33,44]
for i in lst:

lst = [11,22,33,44]
for i in range(len(lst)):
del lst[-1]
print(lst)

for i in range(len(lst)):
lst.pop()
print(lst)

lst = [11,22,33,44]
lst1 = lst[:]
for i in lst1:
lst.remove(i)
print(lst)

Use a for loop to empty the contents of the list element
1. Remove from back to front, 2. Create a new container, new container circulation to remove the old contents of the container

Interview questions:

= LST [1, [2]]
LST [1] = LST
Print (LST)
Answer: [1, [...]]

Dictionary and set:
DIC = { "Key":. 1, "key1": 2}

for i in dic:
if i == "key1":
dic[i] = dic[i] + 8 # dic[i] = 10
else:
print("没有这个键!")
print(dic)

And a set of dictionaries can not modify the original size (length dictionary) while traversing (cycle), can be modified value
S = {1,2,3,4,5,6}
for I in S:
s.pop ()
Print (s)

Guess you like

Origin www.cnblogs.com/x-h-15029451788/p/11329676.html