python Processing List Partial Element Example Explanation

  • 2021-12-04 19:06:34
  • OfStack

1. Dealing with some elements of the list is called slice, creating slice, and specifying the index of the first element and the last element to be used.

2. This allows Python to create a slice that starts with the first element and ends with the last one, that is, to copy the entire list.

Instances


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 indexes return elements corresponding distances from the end of the list , To output the last 3 Players, slices can be used names[-3:]
 
 
 And function range()1 Sample,  Python After arriving at your designated number 2 Stopped after the element in front of the index 
['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:

Lists are similar to arrays in java, represented in square brackets and separated by commas


# Assign value, 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 one of the 1 elements


children_names = [' Du Ziteng ',' Du Xiaoyue ',' Du Xiaoxing ',' Du Xiaoyang ',' Du Xiaohua ']
print(children_names[2])   # Print one of them according to the index 1 Elements with indexes derived from 0 Begin 
print(children_names[-1])  # According to the index, print the last 1 Elements, and so on -1,-2,-3...
print(len(children_names))  # Gets the length of the list 

Run results:

Du Xiaoxing
Du Xiaohua
5

Modify element


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

Run results:

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

Add Element


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

Run results:

['Du Xiaodu', 'Du Ziteng', 'Du Xiaoyue', 'Du Xiaoxing', 'Du Xiaoyang', 'Du Xiaohua', 'Du Xiaolan No.2']

Delete Element

The difference between del and pop is that "by index" is not used after deletion Delete by value, remove

del children_names[0]  # Delete elements completely by index 
children_pop = children_names.pop()
# To be precise, the tail element of the pop-up list "can also specify an index" and assign a value to 1 Variables, temporarily saved 
children_names.remove(" Du Xiaolan 2 No. ") # If duplicate data exists, only the 1 A  

Sorting of lists

Permanently sort alphabetically using sort Temporarily sort the list alphabetically using sorted Print the list backwards

visitors = ['a1','b1','c1','d1','e']
visitors.sort() # Alphabetical, sorted, irreversible 
visitors.sort(reverse=True) # In reverse alphabetical order, irreversible 
print(sorted(visitors)) # Temporary sorting without affecting the order of existing data 
print(sorted(visitors,reverse=True)) # Temporary reverse sorting without affecting the existing data order 
visitors.reverse()  # Direct reverse order, independent of alphabetical order, reversible, and then execute 1 You can do it once 

Run results:

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


Related articles: