当前位置:网站首页>11 important operations of Python list

11 important operations of Python list

2020-11-08 16:37:42 Jabami Mengzi

The list is python Built in data structure in , It takes the form of a collection of different data in square brackets , Separate... With commas . Lists can be used to store the same data type or different data types .

The list is variable , That's why it's so common , In some cases, however , Variability requires special attention .

This paper introduces 11 about python Important operation of list , Help you master the list better .

1. Three ways to remove elements from a list

(1)del

del According to the index position , Delete an element , You can also set the index range to delete , You can also delete the entire list , no return value

Delete single element

a = [1, 2, 'x', 4, 5]
del(a[0])
print(f'a is {a}')

Output

a is [2, 'x', 4, 5]

Range delete element

a = [1, 2, 'x', 4, 5]
del (a[0:2])
print(f'a is {a}')

Output

a is ['x', 4, 5]

Delete the entire list

a = [1, 2, 'x', 4, 5]
del (a[0:5])
print(f'a is {a}')
#  Delete the elements of the entire list by index range , An empty list will be output a is []

a = [1, 2, 'x', 4, 5]
del (a)
print(f'a is {a}')
# name 'a' is not defined  no return value , Error report, no object found 

 

(2)remove

remove() Function to remove the first match of a value in the list , no return value

Example

a = [1, 2, 'x', 4, 5,]
a.remove('x')
print(f'a is {a}')

Output

a is [1, 2, 4, 5]

Be careful : Only the first matching value will be deleted , If there is a duplicate, it will not be deleted

a = [1, 2, 'x', 4, 5,'x']
a.remove('x')
print(f'a is {a}')

Output :

a is [1, 2, 4, 5, 'x']

(3)pop

pop() Function to remove an element from the list ( Default last element ), And returns the value of that element .

Example

a = [1, 2, 'x', 4, 5]
b = a.pop()
print(f'a is {a}')
print(f'b is {b}')

Output

a is [1, 2, 'x', 4]
b is 5

We can specify the index value of the deleted element , Cannot exceed the total length of the list .

a = [1, 2, 'x', 4, 5]
b = a.pop(0)
print(f'a is {a}')

Output

a is [2, 'x', 4, 5]

2. Add list , Expand , Insert

(1)append

append() Function is used to add a new object to the end of the list .

a = [1, 2]
a.append(3)
print(a)

Output

[1, 2, 3]

It can be any data type , The appended element remains the original structure type in the list

a = [1, 2]
b = ['x', 'y', 'z']
a.append(b)
print(a)

Output

[1, 2, ['x', 'y', 'z']]

list b Became a list a The third item in , If you want to create a list, it's a,b A combination of elements in a list a =(1, 2, 'x', 'y', 'z'), You need to use extend() Function or "+“.

(2)extend

extend() Function to append multiple values in another sequence at the end of the list at one time ( Extend the original list with the new list ).

a = [1, 2]
b = ['x', 'y', 'z']
a.extend(b)
print(a)

Output

[1, 2, 'x', 'y', 'z']

(3)insert

insert() Function is also used to add elements to a list . however , It allows you to specify the index of the new element . for example , We can add a new element at the beginning of the list

a = [1, 2, 3, 4, 5]
a.insert(0, 'a')
print(a)

Output

['a', 1, 2, 3, 4, 5]

insert() The first parameter in is the index , The second is the inserted object

3. use ”=“ Copy list

We can copy a list and create a new variable .

a = [1, 2, 3]
b = a
print(b)

however , New variable “ b” It's just pointing to “ a” Pointer to value . therefore ,a Any change in the will change b. Let's add a value to confirm .

a = [1, 2, 3]
b = a
a.append(4)
print(a)
print(b)

Output

[1, 2, 3, 4]
[1, 2, 3, 4]

4. Copy the list using the index

We can copy the list by selecting all the indexes of the list .

a = [1, 2, 3]
b = a[:]
print(b)

New list b Include and a The same value , But in different memory locations . If we change a,b Not affected .

a = [1, 2, 3]
b = a[:]
a.append(4)
print(a)
print(b)

Output

[1, 2, 3, 4]
[1, 2, 3]

Use copy() Function copy list works the same way as index replication .

a = [1, 2, 3]
b = a.copy()
a.append(4)
print(a)
print(b)

Output

[1, 2, 3, 4]
[1, 2, 3]

5. use sort() and sorted() Sort list .

sort and sorted Function can be used to sort a list .

  • sort(): Sort list , But it doesn't return anything .
  • sorted(): Returns a sorted copy of the list , But don't sort the original list .

sort()

a = [2, 0, 4, 3]
b = a.sort()
print(a)
print(b)

Output

[0, 2, 3, 4]
None

use sorted() Function does the same thing .

a = [2, 0, 4, 3]
b = sorted(a)
print(a)
print(b)

Output

[2, 0, 4, 3]
[0, 2, 3, 4]

6. Create nested lists by copying

When creating a nested list by copying , We need to pay attention to the changes in the elements . Any modification of the original list element changes the copied list .

a = [1, 2]
b = ['x', 'y']
c = [a, b]
a.append(3)
b.pop()
print(a)
print(b)
print(c)

list c By list a And list b form , We updated the list by adding and removing an element a And list b. therefore , list c Also updated .

Output

[1, 2, 3]
['x']
[[1, 2, 3], ['x']]

To be continued ...

版权声明
本文为[Jabami Mengzi]所创,转载请带上原文链接,感谢