Python Basic-How to use a list (array)

@

List (List)

- 类似于C语言中的“数组”,将多个相同类型数据存储在一个变量中,“增删查改”的时候使用下标的方式进行引用
- `name = ["ligang","zhangsan","lisi","laoda","laoer","laosan"]`
- 下标从“0”开始的\
name=["zhangwuji","zhaomin","yideng","yangguo","zhaomin","zhaomin","zhangwuji","1","1","1","1"]

"查"

	- name[0]	#只取第一个元素
	- name[0:3]	#取第一个到第3个元素,不包括下标为3个
	- name[1:]	#从下标为1的元素取到最后,:后什么都不加就表示取到最后一个元素
	- name[1:-1]	#从下标为1的元素到倒数第一个元素,也就是最后一个,但是python是左闭右开的区间,所以不包含最右边的元素,所以最后一个(倒数第一个)取不出。中间无论有多少都可以取出来。
	- name[0::2]	#从第一个到最后一个元素隔一个取一个,即取下标为偶数的元素。
	- name[0: -1:2]	#本代码的意思是从第一个元素到倒数第一个元素中隔一个取一个
	- name[3::-2]	# 从下标为3的元素从右往左数,每隔一个取一个
	- name[-2::-1]	#从倒数第二个元素开始直到最后,从右往左全查出来。
	- name[1:-1:-2]	#

2 indicates the step size, that is, how many elements are taken at a time. The
step size is directional. If it is "-2", it means from right to left, so the order is from right to left
. It means that it is from right to left, and 3 is the element with subscript 3, then from right to left to find the last, which is the left

>>> print(name[0])
zhangwuji
>>> print(name[0:3])
['zhangwuji', 'zhaomin', 'yideng']
>>> print(name[1:])   
['zhaomin', 'yideng', 'yangguo', 'zhaomin', 'zhaomin', 'zhangwuji', '1', '1', '1', '1']

>>> print(name[1:-1]) 
['zhaomin', 'yideng', 'yangguo', 'zhaomin', 'zhaomin', 'zhangwuji', '1', '1', '1']

>>> print(name[0::2])
['zhangwuji', 'yideng', 'zhaomin', 'zhangwuji', '1', '1']

>>> print(name[0:-1:2])
['zhangwuji', 'yideng', 'zhaomin', 'zhangwuji', '1']

>>> print(name[3::-2])
['yangguo', 'zhaomin']

>>> print(name[-2::-1])
['1', '1', '1', 'zhangwuji', 'zhaomin', 'zhaomin', 'yangguo', 'yideng', 'zhaomin', 'zhangwuji']

>>> print(name[1:-1:2])
['zhaomin', 'yangguo', 'zhaomin', '1', '1']

"增"

Ways to increase data:

1. append 	: 直接添加到列表的末尾
	name.append("zhangsanfeng")
			name:为上述的列表名,其实是一个“object”
			append:为增加数据的方法,意义为在此列表的尾部增加一个元素。
			zhangsanfeng:为增加的内容
2. insert		: 往列表中指定位置中插入元素
	name.insert(1,"zhangwuji")
			name:为上述的列表名,其实是一个“object”
			1:列表中的下标,指哪儿插哪儿,这里指定的为1,则插入的元素就在1位置上,原先的元素依次往后挪动。
			zhangwuji :为插入的元素

"改"

name [1] = "guojing" # Directly assign the corresponding subscript to the specified character string.
name [1: 3] = ["huangrong", "limochou"] # modify multiple values ​​at once

"删"

1. remove
is a built-in method of the list:
name.remove ("zhangwuji")
directly specifies the deleted content, not the subscript. After deletion, the subscripts of other elements are moved forward in sequence
name.remove ("name [1 ] ")

2. pop
name.pop (2)
directly specify the subscript value
pop to delete the corresponding subscript value, you can assign the corresponding value to another variable
For example: b = name.pop (2)
print (b)
3. del
del name [0]
delete the value with subscript 0
del name
directly delete this list
4, clear ()
clear data

List of other operations

count()

用于统计某个元素在列表中出现的次数:
name=["zhangwuji","zhaomin","yideng","yangguo","zhaomin","zhaomin","zhangwuji","1","1","1","1"]
num = name.count("zhaomin")
print(num)

"""
>>> name=["zhangwuji","zhaomin","yideng","yangguo","zhaomin","zhaomin","zhangwuji","1","1","1","1"]
>>> name=["zhangwuji","zhaomin","yideng","yangguo","zhaomin","zhaomin","zhangwuji","1","1","1","1"]
>>> num = name.count("zhaomin")
>>> print(num)
3
>>>
""" 

extend()

用于在列表中追加另外一个列表中的所有元素
name.extend(skill)
	将“skil”这个列表直接附加在“name”这个列表后面

index()

Find the subscript
name.index ("zhangwuji") of the corresponding element by value

>>> print(name.index("zhangwuji"))
0
>>> print(name.index("yangguo"))  
3

reverse()

Reverse the order of the list

>>> print(name)
['1', '1', '1', '1', 'zhangwuji', 'zhaomin', 'zhaomin', 'yangguo', 'yideng', 'zhaomin', 'zhangwuji']
>>> name.reverse()
>>> print(name)
['zhangwuji', 'zhaomin', 'yideng', 'yangguo', 'zhaomin', 'zhaomin', 'zhangwuji', '1', '1', '1', '1']
>>> 

sort()

Sort the list, you can sort numbers or strings, the default is from small to large,

>>> name.sort()
>>> print(name)
['1', '1', '1', '1', 'yangguo', 'yideng', 'zhangwuji', 'zhangwuji', 'zhaomin', 'zhaomin', 'zhaomin']

name.sort (reverse = True) # Sort from largest to smallest

>>> name.sort(reverse=True)
>>> print(name)
['zhaomin', 'zhaomin', 'zhaomin', 'zhangwuji', 'zhangwuji', 'yideng', 'yangguo', '1', '1', '1', '1']

Determine if an element is in a python list

name.count("zhangwuji")

>>> name.count("zhangwuji")
2    #大于0 表示存在

print("zhangwuji" in name)

>>> print("zhangwuji" in name)
True
>>>

Determine if it is a list

type(name) is list

>>> type(name) is list
True

Guess you like

Origin www.cnblogs.com/fei-huang/p/12733927.html