The basic data structure in Python-list

零下一度
Release: 2017-06-30 13:22:47
Original
1675 people have browsed it

The most basic data structure in Python. Each element in the sequence is assigned a number - its position, or index, with the first index being 0, the second index being 1, and so on

List

1, Define the list and take out the values ​​in the list

 1 names = [] #定义空列表 2 names = ['a','b','c'] #定义一个非空列表 3  4 # 取出列表中的值 5  6 >>> names = ['a','b','c'] 7 >>> names[0] 8 'a' 9 >>> names[1]10 'b'11 >>> names[2]12 'c'13 >>> names[-1]#倒着取最后一个值14 'c'
Copy after login

2. Slice

 1 >>> names = ['a','b','c','d'] # 列表的下标值是从0开始取值的 2 >>> names[1:3] #取1到3之间的元素,包括1,不包括3 3 ['b', 'c'] 4 >>> names[1:-1] #取1到-1之间的元素,包括1,不包括-1 5 ['b', 'c'] 6 >>> names[0:3] 7 ['a', 'b', 'c'] 8 >>> names[:3] #从头开始取,0可以省略,效果等同于names[0:3] 9 ['a', 'b', 'c']10 >>> names[3:]  #想取到最后一个值,必须不能写-1,只能这么写11 ['d']12 >>> names[0::2] #后面的2表示:每隔一个元素就取一个13 ['a', 'c']14 >>> names[::2] #从头开始0可以省略,效果跟上一句一样15 ['a', 'c']
Copy after login

Slice summary:

①The sequence is always sliced ​​from left to right, not from right to left

When the list is sliced, the elements at the start position are included, and the elements at the end position are not included (Also called looking at the beginning but not the tail), the last position represents the step size (names [start bit: end bit: step size])

② If the value is taken from the 0 position, 0 can be omitted

③When you want to get the last value, the end bit cannot be -1, because the elements of the end bit are not included, so you can only leave it blank

3. List functions & methods

Function:

Method:

Demo:

 1 # append 列表末尾添加新的对象 2 >>> names = ['a','b','c','d'] 3 >>> names.append('e') 4 >>> names 5 ['a', 'b', 'c', 'd', 'e']#e是新加的元素 6  7 # insert(下标值,插入的内容)  8 >>> names = ['a','b','c','d'] 9 >>> names.insert(0,'1') #0表示需要插入的下标值,'1'表示插入的内容10 >>> names11 ['1', 'a', 'b', 'c', 'd'] #在下标值为0的地方插入'1'12 13 #修改列表中的元素,直接是 names[下标值] = 新值14 >>> names = ['a','b','c','d']15 >>> names[1] = '1'16 >>> names17 ['a', '1', 'c', 'd']18 19 20 #扩展(extend)names2的列表合并到names1中,但是,names2这个列表依然存在,如果想删除names2这个变量,则只需del names2即可21 >>> names1 = ['a','b','c','d']22 >>> names2 = [1,2,3,4]23 >>> names1.extend(names2)24 >>> names125 ['a', 'b', 'c', 'd', 1, 2, 3, 4]26 27 #统计(count(元素))28 >>> names = ['a','b','c','d','a']29 >>> names.count('a') #统计'a'元素的个数30 231 32 #翻转(reverse())33 >>> names = ['a','b','c','d']34 >>> names.reverse()35 >>> names36 ['d', 'c', 'b', 'a']  #将整个列表翻转过来37 38 #排序(sort())39 >>> names = [4,2,3,1]40 >>> names.sort()41 >>> names42 [1, 2, 3, 4]43 44 #获取下标值(index(元素))45 >>> names = ['a','b','c','d']46 >>> names.index('a')47 048 49 #清空列表(clear())50 >>> names = ['a','b','c','d']51 >>> names.clear()52 >>> names53 []
Copy after login

Delete (del, remove(element), pop())

#根据下标值删除元素>>> names = ['a','b','c','d']>>> del names[0] 
>>> names
['b', 'c', 'd']#根据元素删除>>> names = ['a','b','c','d']>>> names.remove('a')>>> names
['b', 'c', 'd']#删除最后一个>>> names = ['a','b','c','d']>>> names.pop()'d'>>> names
['a', 'b', 'c']
Copy after login

Extension :

#如果pop()中有下标值,则是删掉具体某个元素,其效果和del的效果是一样的>>> names = ['a','b','c','d']>>> names.pop(1) #在输入下标值得情况下和del的效果是一样的'b'>>> names
['a', 'c', 'd']#del关键字不仅可以删除列表中的元素,也可以删除变量names = ['a','b','c','d']#删除names这个变量del names
Copy after login

copy:

1 >>> names = ['a','b','c','d']2 >>> names2 = names.copy()3 >>> names24 ['a', 'b', 'c', 'd']
Copy after login

Note: The copies here are shallow copies and can only copy the first layer. Detailed information on dark and light copies: click here

The above is the detailed content of The basic data structure in Python-list. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!