Self-study python--list (list)

=List=====

1 Introduction to the list

Python 内置的一种数据类型是列表:list。list 是一种有序的集合,可以随时添加和删除其中的元素

2 Definition list

     创建一个列表,只要把逗号分隔的不同的数据项使用方括号括起来即可。如下所示
list1 = ['Google', 'Runoob', 1997, 2000];
list2 = [1, 2, 3, 4, 5 ];

3 Python list

   列表对 + 和 * 的操作符与字符串相似。+ 号用于组合列表,* 号用于重复列表。
如下所示:

Insert picture description here

4 Python list functions and methods

Python包含以下函数:

Insert picture description here
Python contains the following methods:
Insert picture description here

5 Basic operations of lists

#列表
#列表名= [值,值...]
#创建列表
names = ["ca","cc","cb"]

=Access the elements in the list=====

#当索引超出了范围时,Python 会报一个 IndexError 错误,所以,要确保
#索引不要越界,记得最后一个元素的索引是 len(classmates) - 1 。
	print(names[0])

=Add to=

#append append  往 list 中追加元素到末尾
	names.append("root")
	names.insert(11,"root")#11 会出现下末尾
	print(names)

=delete=

#pop() 把删除的数据,并返回
#pop(12) index(下标)  下标必须存在,不然报错。
	print(names.pop(12))
#remove 指定值来删除  ,没有返回值,如果删除的值,不存在则报错
	print(names.remove("tom2"))
#names.clear()#清除
	print(names)

===============Modify===============

	names[3] ="root"
	print(names)

=List nesting=

	s = ['python', 'java', ['asp', 'php'], 'scheme']
	print(s[2][1])

The max data type must be consistent

	print(max(names))

=List sort=

#reverse -- 排序规则,reverse = True 降序, reverse = False 升序(默认)。
	names.sort(reverse = True)
	print(names)

===List copy=

#复制
	names2= names.copy()
	names2.pop()
	print(names2)
	print(names)

Guess you like

Origin blog.csdn.net/weixin_47580822/article/details/112927658