Home > Backend Development > Python Tutorial > Python中列表(list)操作方法汇总

Python中列表(list)操作方法汇总

WBOY
Release: 2016-06-16 08:42:46
Original
1545 people have browsed it

本文实例汇总了Python中关于列表的常用操作方法,供大家参考借鉴。具体方法如下:

一、Python创建列表:

sample_list = ['a',1,('a','b')]

Copy after login

二、Python 列表操作:

假设有如下列表:

sample_list = ['a','b',0,1,3]
Copy after login

1.得到列表中的某一个值:

value_start = sample_list[0]
end_value = sample_list[-1]

Copy after login

2.删除列表的第一个值:

del sample_list[0]

Copy after login

3.在列表中插入一个值:

sample_list[0:0] = ['sample value']

Copy after login

4.得到列表的长度:

list_length = len(sample_list)

Copy after login

5.列表遍历:

for element in sample_list:
  print(element)

Copy after login

三、Python 列表高级操作/技巧

1.产生一个数值递增列表:

num_inc_list = range(30)
#will return a list [0,1,2,...,29]

Copy after login

2.用某个固定值初始化列表:

initial_value = 0
list_length = 5
sample_list = [ initial_value for i in range(10)]
sample_list = [initial_value]*list_length
# sample_list ==[0,0,0,0,0]

Copy after login

读者还可以在此基础上继续搜集关于Python列表操作的其他方法,进一步巩固及加深对Python列表操作的认识。

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