程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python -- basic operation of lists

編輯:Python

list : stay python in , If you store multiple data, use a list .
The list is an ordered , Modifiable , Elements are separated by commas , With brackets Surrounded sequence .
The index of a list is similar to a string index , But it's not exactly the same , The list can be modified , So we can modify the list through the list index . The elements in the list are mutable , The elements in the list can be repeated .
Definition of list :

name = [] # An empty list 

We can go through **type()** To see the types of variables .

 type(name)
<class 'list'>

Here you can see ,name It's a list , An empty list . Next , We can add elements to the list , Let's first look at the memory address of the list , Use id() Function to view the memory address where the elements in the list are stored , Let's redefine a list of elements .

name = []
a = id(name)
print(a)
name.append('jk')
b = id(name)
print(b)
name.insert(1,'hi')
c = id(name)
print(c)

result :

1737781446984
1737781446984
1737781446984

It can be seen that , Add elements to the list , The memory address of the list has not changed , therefore , Using lists can prevent excessive memory consumption , You can save memory .
Add elements to the list :

  1. append , Additional , Add the specified element at the end of the list :
name = ['jake','marry','hery','lucy',10]
name.append('hu')
print(name)
['jake', 'marry', 'hery', 'lucy', 10, 'hu']

There is also a problem here , If it's directly in print() Print the added list inside , There will be such problems :
The first one is :

name = ['jake','marry','hery','lucy',10]
name1 = name.append('hu')
print(name1)
None

The second kind :

name = ['jake','marry','hery','lucy',10]
print(name.append('hu'))
None

Why can't you save the list to a variable when you change it ?
Because this is to add elements to the list , The value of the list has changed , So you can't print directly , Instead, add , Then print to conform to the corresponding logic . And the reason why it outputs None, Because when printing ,name.append No return value , So what's printed out is None.

  1. insert , Inserts the specified element into the corresponding index bit , Note the negative index :
name = ['jake','marry','hery','lucy',10]
name.insert(0,'anna')
print(name)
['anna', 'jake', 'marry', 'hery', 'lucy', 10]
  1. extend, Appends the specified elements to the end of the list :
name = ['jake','marry','hery','lucy',10]
name1 = [1, 2, 3]
name.extend(name1)
print(name)
 ['jake', 'marry', 'hery', 'lucy', 10, 1, 2, 3]

Delete the elements in the list :
1,pop eject , Returns and deletes the data at the specified index bit , The default deletion index is -1 The data of ; We can also use a variable to store the pass function pop() Deleted values , as follows :

name = ['jake','marry','hery','lucy',10]
name1 = name.pop()
print(name)
print(name1)
['jake', 'marry', 'hery', 'lucy']
10

pop() You can also delete the specified element , Just point out the corresponding index :

name = ['jake','marry','hery','lucy',10]
name1 = name.pop(2)
print(name)
print(name1)
['jake', 'marry', 'lucy', 10]
hery

2,remove Delete a specified element from left to right ;

name = ['jake','marry','hery','lucy',10]
name.remove('marry')
print(name)
 ['jake', 'hery', 'lucy', 10]

3,del Delete the entire list or the data of the list ,del yes Python The built-in function , Not unique to the list ;

name = ['jake','marry','hery','lucy',10]
del name[0]
print(name)
['marry', 'hery', 'lucy', 10]

The elements in the list are modified :
Direct assignment modification :

name = ['jake','marry','hery','lucy',10]
name[0] = 'petter'
print(name)
['petter', 'marry', 'hery', 'lucy', 10]

Search for elements in the list :
1,count Count , Returns the number of elements to count in the list ; Return on no 0.

name = ['jake','marry','hery','lucy',10]
print(name.count('lucy'))
 1

2,index lookup , Returns the index of the first specified element found from left to right , If not found , Report errors ; Be careful : The list doesn't have find To find the way .

name = ['jake','marry','hery','lucy',10]
print(name.index('lucy'))
3

3,in Find out if the element is in the list , At output true, otherwise false;

name = ['jake','marry','hery','lucy',10]
'lucy' in name
 True
name = ['jake','marry','hery','lucy',10]
'peter' in name
False

Sort the list :
1,reverse Reverse order

name = ['jake','marry','hery','lucy',10]
name.reverse()
print(name)
[10, 'lucy', 'hery', 'marry', 'jake']

*2,sort according to ascii Code table sequence

list() function :
list(), Take a sequence as an argument and convert it to a list , If the parameter is list, The parameter will be returned as is . If you define list This variable , Reuse list() This function will report an error .

num = 'asdfg'
list(num)
['a', 's', 'd', 'f', 'g']
num = 'asdfg'
num1 = list(num)
print(num1)
''.join(num1)# Converts the string converted to a list back to a string 
['a', 's', 'd', 'f', 'g']
'asdfg'

List slice :
List slicing is the same as string slicing . The head is not the tail

name = ['jake','marry','hery','lucy',10]
print(name[0:2])
 ['jake', 'marry']
name = ['jake','marry','hery','lucy',10]
print(name[0:4:2])
['jake', 'hery']

If the step size is negative , Is to reverse the order of the list , But the subscript of the element doesn't change , in other words , In turn, the subscript of the first element after that is the subscript of the last element of the initial list . Again ,name[startstep] The start and end bits of are also in reverse order .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved