Advanced python - Lesson 7

Posted by sadaf on Wed, 02 Mar 2022 01:05:40 +0100

Advanced python has been greatly optimized on the basis of the first phase, making the whole more beautiful and easy to understand

7, List

7.1 common operations of list

7.1.1 search

a,-----.statswith()

. statswitch(): judge whether the string starts with a substring, put the substring in parentheses, and the results are Boolean types True and False

str5 = "hello my friend"
print(str5.startswith("hello"))  # Result: True
print(str5.startswith("he"))     # Result: True

b,-----.endswith()

. endswitch(): judge whether the string ends with a substring, put the substring in parentheses, and the results are Boolean types True and False

 str5 = "hello my friend"
print(str5.endswith("end"))  #Result: True

c,-----.isalpha()

. isalpha(): judge whether there are letters in the string

str5 = "hello my friend"
print(str5.isalpha())  # Result: False (interspersed with spaces, which are not letters)

d,-----.isdigit()
. isdigit(): judge whether there are all numbers in the string

str6 = "678"
print(str6.isdigit())  # Result: True

e,-----.isalnum()
. isalnum: () judge whether it is all letters, or numbers, or a combination of letters and numbers

str9a = "123abc"
str9b = "123456"
str9c = "abcdef"
print(str9a.isalnum())
print(str9b.isalnum())
print(str9c.isalnum())
# result:
>>> True
>>> True
>>> True

f,-----.isspace()

. isspace(): judge whether there are spaces in the string

str10 = "     "
print(str10.isspace())  # True

7.1.2 search

a. List name [subscript] function description

name1 = ["xwa", "xla", "xza", "xwa"]
print(name1[2])
# result
>>> xza

b,. Index (data) returns a subscript if it exists. If it does not exist, an error will be reported

name1 = ["xwa", "xla", "xza", "xwa"]
print(name1.index("xwa", 1, 4))  # You can limit the search range. Otherwise, the first data subscript of the column will be found by default
# result:
>>> 3

c,. Count (data) returns the number of times the specified data is in the list

name1 = ["xwa", "xla", "xza", "xwa"]
print(name1.count("xwa"))
# result
>>> 2

d. Len (list name) returns the length of the list. Length refers to the number of data in the list

name1 = ["xwa", "xla", "xza", "xwa"]
print(len(name1))
# result:
>>> 4

7.1.3 judgment and addition

a. In: data in list, which determines whether the data is in the list. The two results are Boolean True or False

name1 = ["xwa", "xla", "xza", "xwa"]
print("xza" in name1)  # Result True
print("xza1" in name1)  # Result False

b. Not in: data not in list to judge whether the data is not in the list

name1 = ["xwa", "xla", "xza", "xwa"]
print("xla" not in name1)  # False
print("xla1" not in name1)  # True

c. append(): list name Append data to the end of the append list

name1 = ["xwa", "xla", "xza", "xwa"]
name1.append("lie")
print(name1)
# result:
>>> ['xwa', 'xla', 'xza', 'xwa', 'lie']  # More than thought, added at the end of the list

d. extend(): list name If the data is a sequence, the end of the list is appended one by one

name1 = ["xwa", "xla", "xza", "xwa"]
name1.extend("lie")  # The string is split into one and then added at the end
print(name1)
# result:
>>> ['xwa', 'xla', 'xza', 'xwa', 'l', 'i', 'e']

e. insert(): list name Insert (subscript, data) adds data at the specified location

name1 = ["xwa", "xla", "xza", "xwa"]
name1.insert(2, "xxa")  # Add xxa to the position with subscript 2
print(name1)
# result:
>>> ['xwa', 'xla', 'xxa', 'xza', 'xwa']

7.1.4 delete

a. Del target: del list name or del list name [subscript], delete the list or data in the list

name2 = ["lw", "ll", "lw", "lh"]
del name2  # Delete list
 print(name2)
#result
>>> List deletion, error report

Example 2

name2 = ["lw", "ll", "lw", "lh"]
del name2[2]
print(name2)
# result:
>>> ['lw', 'll', 'lh']

b. pop(): list name (subscript), delete the data of the specified subscript, and return the data (deleted data)

name2 = ["lw", "ll", "lw", "lh"]
del_name = name2.pop(1)
print(del_name)  # Return deleted data
print(name2)
# result:
>>> ll  
>>> ['lw', 'lw', 'lh']
name2 = ["lw", "ll", "lw", "lh"]
del_name2 = name2.pop()
print(del_name2)  # Deleted data
print(name2)  # Remaining data after deletion
# result:
>>> lh
>>> ['lw', 'll', 'lw']

c. remove(): list name Remove (data), delete the first configuration item of a data in the list (the first data when two data are the same)

name2 = ["lw", "ll", "lw", "lh"]
name2.remove("lw")  # Delete first lw
print(name2)
# result
>>> ['ll', 'lw', 'lh']

d,. clesr(): list name clear(), clear the list (delete all data in the list)

name2 = ["lw", "ll", "lw", "lh"]
name2.clear()
print(name2)
# result
>>> []

7.1.5 modification

a. List name [subscript] = new data: list name [subscript] = new data, modify the specified data according to the subscript

name3 = ["yul", "kun", "yud", "yul"]
name3[2] = "lll"
print(name3)
# result
>>> ['yul', 'kun', 'lll', 'yul']

b. reverse(): list name reverse(), reverse the number in the list

name3 = ["yul", "kun", "yud", "yul"]
name3.reverse()  # Look at the result, the data in the list is reversed
print(name3)
# result:
>>> ['yul', 'yud', 'kun', 'yul']

c. Sort: list name sort() to sort the list in ascending order. The numbers are from small to large, and the letters are from a to z. the Chinese characters are irregular, which may be according to Pinyin or strokes

name3 = ["yul", "kun", "yud", "yul"]
name4 = [3, 5, 2, 9, 1, 8, 0]
name3.sort()
name4.sort()
print(name3)
print(name4)
# result:
>>> ['kun', 'yud', 'yul', 'yul']
>>> [0, 1, 2, 3, 5, 8, 9]

d. sort: list name sort(reverse = False), reverse = True, descending, reverse = False, ascending (default) reverse indicates the sorting rule

name4 = [3, 5, 2, 9, 1, 8, 0]
name4.sort(reverse=True)  # Descending order
print(name4)
# result;
>>> [9, 8, 5, 3, 2, 1, 0]
name4 = [3, 5, 2, 9, 1, 8, 0]
name4.sort(reverse=False)  # Ascending order
print(name4)
# result:
>>> [0, 1, 2, 3, 5, 8, 9]

7.1.6 copying

Copy (): list name copy(), copy and store the original data

name4 = [3, 5, 2, 9, 1, 8, 0]
name3 = name4.copy()
print(name3)
print(name4)
# result;
>>> [3, 5, 2, 9, 1, 8, 0]
>>> [3, 5, 2, 9, 1, 8, 0]

7.2 nested use of lists

List nesting: refers to a list that contains other sub lists.

name5 = [[["xl", "yul", ["yy"]], "xw", "xz"], ["pleasure", "2", "3"], ["a", "b", "c"]]  # List nesting, [subscript] [subscript] It all starts from scratch
print(name5[1][2])
print(name5[0][0][2][0])
# result:
>>> 3
>>> yy

Circular traversal of the list: print each data of the list in turn

list1 = ["wang", "li", "zang"]
# for loop
for i in list1:
    print(i)
# result:
>>> wang
>>> li
>>> zang

7.3. while loop traversal

list1 = ["wang", "li", "zang"]
i = 0  # Subscript starts from zero
while i < len(list1):  # When you don't know how much data is in the list, use the len function. Len function: returns the length
    print(list1[i])
    i += 1
# result:
>>> wang
>>> li
>>> zang

7.4 small exercises

"""
10 Three jewels were randomly assigned to three members of Zhang San gang
 Pearl:["a", "b", "c", ...]
Three members of Zhang San Gang[[][][]]
"""
# Import random modules and functions
import random  # Import random module
# num = random.randint(0, 2)  # Returns any integer between 0 and 2
bead = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]
san = [[], [], []]
# Where are the jewels assigned to Zhang San
for z in bead:
    # Add Baozhu z to the sub columns [[], [], []] of Zhang San. Use the append function to add, append (data), list name [subscript] = new data, and modify the specified data according to the subscript
    # Random assignment: importing random functions
    num = random.randint(0, 2)  # Then import the random function through the random module, 0 and 1 before and after the package, and give the random number to num,
    san[num].append(z)  # append() append function at the end of the list
print(san)
# Result: three runs
# [['a', 'b', 'c', 'g', 'i', 'j'], ['d', 'h'], ['e', 'f']]
# [['c', 'e', 'g', 'h', 'i'], ['d', 'j'], ['a', 'b', 'f']]
# [['a', 'd', 'e', 'i'], ['b', 'g', 'j'], ['c', 'f', 'h']]

Topics: Python Back-end