python class finishing 4 --- list of magic

A, list category, a list of

li = [1, 12, 9, "age", [ "white", "black"], "Alex"]

1. In brackets

2. comma separated each element

3. The elements in the list can be numbers, strings, lists, and other Boolean

4. Index Value

5. Slice the value ----> or get a list of

6. for loop

7. while loop

8. Support in operation

List in memory is stored as a linked list can be modified

Second, the list of gray magic

1. Modify

 

li = [1, 12, 9 , "alex", [ " white", "black"]] 
Li [. 1] = 120 
Print (Li)

 

li = [1, 12, 9 , "alex", [ " white", "black"]] 
Li [. 1:. 3] = [120, 90] 
Print (Li)

 

2. Delete

li = [1, 12, 9 , "alex", [ " white", "black"]] 
del Li [. 1] 
Print (Li)

li = [1, 12, 9 , "alex", [ " white", "black"]] 
del Li [0:. 3] 
Print (Li)

3. Operation

li = [1, 12, 9, "alex", ["大白", [7, 19]]]
v = li[4][1][0]
print(v

String to the list

s = "dabai"
new_li = list(s)
print(new_li)

for i in 123:
    print(i)
    

Digital (int) can not be performed for loop, can not be changed to list the list, because the string into a list, is carried out inside the for loop.

The list into a string

1. When both numbers strings, wrote for circulation

li = [11,22,33,"123", "alex"]
s = ""
for i in li:
    s = s + str(i)
print(s)

2. If the list is only a string, use the join method of the string

li = ["123", "alex"]
v = "".join(li)
print(v)

Third, a list of commonly used magic

1. The input parameters, and finally added to the original value, None expressed in python in empty, nothing. Because the list can be edited directly, there is no need to re-acceptance by v, directly li.appebd (5) to

li = [11, 22, 33, 44] 
v = li.append (5)
print (li)
print (v)

li object calls the append method

to = [11, 22, 33, 44] 
li.append ([1234.2323]) 
print (to)

2. Clear List

li = [11, 22, 33, 44] 
li.clear () 
print (li)

3. Copy, shallow copy

li = [11, 22, 33, 44] 
v = li.copy () 
print (v)

4. Calculate the number of elements appearing

li = [11, 22, 33, 22, 44] 
v = li.count (22) 
print (v)

The original extended list of parameters iterables ( Iterable as iterables)

li = [11, 22, 33, 22, 44]
li.extend(["大白", "521"])
print(li)

extend for loop corresponding to internal, then li.append ()

li = [11, 22, 33, 22, 44]
li.extend("大白")
print(li)

6. The value of the current index value acquisition position according to the left priority, you can specify the starting position

li = [11, 22, 33, 22, 44] 
v = li.index (22) 
print (v)

7. Insert the specified index

li = [11, 22, 33, 22, 44]
li.insert(0, 99)
print(li)

8.默认删除列表最后一个,并获取被删除的值

li = [11, 22, 33, 22, 44]
v = li.pop()
print(li)
print(v)

指定索引删除

li = [11, 22, 33, 22, 44]
v = li.pop(1)
print(li)
print(v)

9. 删除列表中的指定值,左边优先

li = [11, 22, 33, 22, 44]
li.remove(22)
print(li)

10. 将当前列表进行翻转

li = [11, 22, 33, 22, 44]
li.reverse()
print(li)

11.列表的排序,默认是从小到大排

li = [11, 22, 33, 22, 44]
li.sort()
print(li)

也可以从大到小排

li = [11, 22, 33, 22, 44]
li.sort(reverse = True)
print(li)

也可以根据函数方面排序,目前未学。

 

Guess you like

Origin www.cnblogs.com/dabai123/p/10960419.html