SoFunction
Updated on 2024-10-30

Example of python processing partial elements of a list in detail

1, part of the elements of the processing list is called a slice, create a slice, you can specify the first element to be used and the index of the last element.

2. This lets Python create a slice that starts at the first element and ends at the last, i.e., copy the entire list.

an actual example

names = ['zhang_san','chen_cheng','li_hong','liu_li','chen_yu']
print(names[0:3])
print(names[0:-1])
print(names[:])
print(names[-1])
print(names[-3:])
 
Negative indexing returns the element at the corresponding distance from the end of the list,To export the last three players on the list,Slices can be usednames[-3:]
 
 
function (math.)range()equal to, PythonStops after reaching the element preceding the second index you specify
['zhang_san', 'chen_cheng', 'li_hong']
['zhang_san', 'chen_cheng', 'li_hong', 'liu_li']
['zhang_san', 'chen_cheng', 'li_hong', 'liu_li', 'chen_yu']
chen_yu
['li_hong', 'liu_li', 'chen_yu']

Instance Extension:

A list is similar to an array in java, with square brackets and commas separating the elements.

#Assign, print
children_names = ['Du Ziteng','Du Xiaoyue','Du Xiaoxing','Du Xiaoyang','Du Xiaohua']
print(children_names)

Run results:

['Du Ziteng', 'Du Xiaoyue', 'Du Xiaoxing', 'Du Xiaoyang', 'Du Xiaohua']

Access to one of the elements

children_names = ['Du Ziteng','Du Xiaoyue','Du Xiaoxing','Du Xiaoyang','Du Xiaohua']
print(children_names[2])   # Print one of the elements by index, starting at 0
print(children_names[-1])  # Print the last element, by index, and so on -1,-2,-3...
print(len(children_names))  # Get the length of the list

Run results:

Du Xiaoxing (1905-1992), Mao *'s second wife
Dulcimer (Harry Potter)
5

Modifying elements

children_names = ['Du Ziteng','Du Xiaoyue','Du Xiaoxing','Du Xiaoyang','Du Xiaohua']
children_names[2]='Du Xiaolao'  # Follow the index and directly override the assignment
print(children_names)

Run results:

['Du Ziteng', 'Du Xiaoyue', 'Du Xiaolao', 'Du Xiaoyang', 'Du Xiaohua']

Adding Elements

children_names = ['Du Ziteng','Du Xiaoyue','Du Xiaoxing','Du Xiaoyang','Du Xiaohua']
children_names.append("Du Xiaolai 2.")  #Append to the end of the list
children_names.insert(0,"Du Xiaodu.")   # Insert elements according to index position
print(children_names)

Run results:

['Du Xiaodu', 'Du Ziteng', 'Du Xiaoyue', 'Du Xiaoxing', 'Du Xiaoyang', 'Du Xiaohua', 'Du Xiaolao 2']

Delete element

  • The difference between the use of del and pop is that after deletion it is used or not [based on the index].
  • Delete by value, remove
del children_names[0]  # Delete elements completely, by index
children_pop = children_names.pop()
# To be precise, pop up the tail element of the list [you can also specify the index], assign it to a variable, and save it temporarily
children_names.remove("Du Xiaolai 2.") # If there are duplicates, only the first one will be deleted.

Sorting the list

  • Use sort to sort permanently in alphabetical order
  • Temporarily sort the list in alphabetical order using sorted
  • Print the list backwards
visitors = ['a1','b1','c1','d1','e']
() # Alphabetical, sorted, irreversible #
(reverse=True) # In reverse alphabetical order, irreversible
print(sorted(visitors)) # Temporary sorting without affecting the existing data order
print(sorted(visitors,reverse=True)) # Temporary reverse sorting without affecting the existing data order
()  # Directly inverted, not related to alphabetical order, reversible, just do it again #

Running results:

['a1', 'b1', 'c1', 'd1', 'e']
['e', 'd1', 'c1', 'b1', 'a1']

to this article on the python processing list of some of the elements of the example details of the article is introduced to this, more related python processing list of some of the elements of the content, please search for my previous articles or continue to browse the following related articles I hope you will support me more in the future!