List of common methods for Python learning:

Posted by DJH on Fri, 04 Feb 2022 04:05:07 +0100

catalogue

1. Add:

(1) Method append (): you can add an element (the end) to the list

(2) Method insert (): insert the element object before the index at the specified position

(3) Method extend(): you can add elements from another collection to the list one by one

2. Change: modify according to the index

3. Check:

(1) In: judge whether the element exists in the list and return Bool value

(2) not in: opposite to in

(3) Method index (): query the location of the element (only find the first matching content, then output the index location of the first element, and ignore it after finding it)

(4) Method count(): query the number of elements

4. Delete

(1) del statement: delete according to index

(2) Method pop (): delete the last element

(3) Method remove (): delete according to the value of the element and delete the first matching element (an error will be reported if the element does not exist)

5. Exhaust

(1) Method sort(): rearrange the list in a specific order. The default is from small to large. The parameter reverse=True can be changed to reverse order, from large to small.

(2) Method reverse (): reverse the list.

6. Copy method: copy a new list.

1. Add:

(1) Method append (): you can add an element (the end) to the list

(2) Method insert (): insert the element object before the index at the specified position

(3) Method extend(): you can add elements from another collection to the list one by one

book = ['Journey to the West', 'Water Margin']
book.append('Romance of the Three Kingdoms') # append appends to the end of the list
print(book)
# insert method
book.insert(0, 'The Dream of Red Mansion')
print(book)
# extend method
kongfu = ['Tianlong Babu', 'Xiaoao Jianghu']
book.extend(kongfu)
print(book)
print(kongfu)

Operation results:

 

2. Change: modify according to the index

Note: the index should not be out of bounds

# modify
book = ['The Dream of Red Mansion', 'Journey to the West', 'Water Margin', 'Romance of the Three Kingdoms', 'Tianlong Babu', 'Xiaoao Jianghu']
book[4] = 'Legend of Shooting Heroes'
print(book)

Operation results:

3. Check:

(1) In: judge whether the element exists in the list and return Bool value

(2) not in: opposite to in

(3) Method index (): query the location of the element (only find the first matching content, then output the index location of the first element, and ignore it after finding it)

(4) Method count(): query the number of elements

# In and not in
book = ['The Dream of Red Mansion', 'Journey to the West', 'Water Margin', 'Romance of the Three Kingdoms', 'Tianlong Babu', 'Xiaoao Jianghu']
if 'The Dream of Red Mansion' in book:
    print('Dream of Red Mansions is on the list')
else:
    print('Dream of Red Mansions is not on the list')
if 'The Dream of Red Mansion'  not in book:
    print('Dream of Red Mansions is not on the list')
else:
    print('Dream of Red Mansions is on the list')
# index
print(book.index('The Dream of Red Mansion'))
book.append('The Dream of Red Mansion')
print(book)
print(book.index('The Dream of Red Mansion'))
# count
print(book.count('The Dream of Red Mansion'))

Operation results:

4. Delete

(1) del statement: delete according to index

(2) Method pop (): delete the last element

(3) Method remove (): delete according to the value of the element and delete the first matching element (an error will be reported if the element does not exist)

# del delete by index
book = ['The Dream of Red Mansion', 'Journey to the West', 'Water Margin', 'Romance of the Three Kingdoms', 'Tianlong Babu', 'Xiaoao Jianghu', 'The Dream of Red Mansion']
del book[-1]
print(book)
# pop deletes the last element
book.pop()
print(book)
# remove deletes the specified element
book.remove('Water Margin')
print(book)
book = ['The Dream of Red Mansion', 'Journey to the West', 'Water Margin', 'Romance of the Three Kingdoms', 'Tianlong Babu']
book.append('Water Margin')
book.remove('Water Margin')
print(book)

Operation results:

5. Exhaust

(1) Method sort(): rearrange the list in a specific order. The default is from small to large. The parameter reverse=True can be changed to reverse order, from large to small.

list_val = [23,13,56,78,90]
# Sort sort
list_val.sort()
print(list_val)

Operation results:

After changing parameters:

list_val = [23,13,56,78,90]
# Sort sort
list_val.sort(reverse=True)
print(list_val)

Operation results: 

 

(2) Method reverse (): reverse the list.

list_val = [23,13,56,78,90]
# reverse inversion
list_val.reverse()
print(list_val)

Operation results:

6. Copy method: copy a new list.

list_val = [23,13,56,78,90]
new_list = list_val.copy()
print(new_list)

Operation results:

Note: the copied list has no relationship with the original list, but the values are the same

The id () function can be used to determine whether the copied list is related to the original list. The id () function is used to find the location of the variable in memory.

list_val = [23,13,56,78,90]
new_list = list_val.copy()
print(new_list)
print(id(list_val))
print(id(new_list))

Operation results:

 

Topics: Python Windows Pycharm