News from this site

 Rental advertising space, please contact the webmaster if you need cooperation


+focus
focused

classification  

no classification

tag  

no tag

date  

no datas

Detailed list of python

posted on 2023-05-03 20:40     read(850)     comment(0)     like(12)     collect(0)


1. Create a list

Data type list, list is an advanced data type built into python. List is an ordered collection, which is widely used in python based on linked list implementation

1. Definition based on weak data type language

names=['james', 'michael', 'emma', 'emily']
print('names的数据类型:', type(names))#type()函数是查看变量类型
print(names)

The result of the operation is as follows:
insert image description here

2. Defined by the global function list()

ls2 = list([1, 2, 3, 4, 5])
print(ls2)

The result of the operation is as follows:
insert image description here

3. Create an empty list

age=[]#直接创建空列表
print(age)
print(len(age))#len()函数计算长度
ls1 = list()#通过list()函数创建
print(ls1)

operation result:
insert image description here

2. Access the value of the list

1. By subscript index

names = ['james', 'michael', 'emma', 'emily']
print(names[0])
print(names[-1])  #访问列表中的最后一个值
print(names[-2])#访问列表中的倒数第二个元素,反向索引
print(names[len(names)-2]) #效果如上

The result of the operation is as follows:
insert image description here

2. Through for loop traversal

names = ['james', 'michael', 'emma', 'emily']
for name in names:
    print(name)  

for i in range(len(names)):
    print(names[i])

operation result:
insert image description here

3. Traverse through while loop

names = ['james', 'michael', 'emma', 'emily']
index = 0    #通过while循环来列出所有元素
while index < len(names):
    print(names[index])
    index += 1

The result of the operation is as follows:
insert image description here

3. Fragmentation of the list

a=['egg', 'fish', 'cake', 'tomato', 'james', 'ava', 'michael', 'emma', 'emily']
print(a[1:3]) #按下标0开始,不包括最右边的3
print(a[1:]) #1以及之后的全部
print(a[:3]) #3之前的但不包括3
print(a[:]) #所有
print(a[::2])#[start:end:step]start和end为空的时候,默认是全选,step为空时默认是1,这个表示的是从索引为0开始,以步长为2来选择元素
print(a[1:3:2])#以索引为1开始,索引3结束,步长为2来选择元素
print(a[::-1])#当step为-1时,将列表进行了逆序排序
print(a[::-2])#当步长为正数时,是从左到右以该步长来获取列表中的元素,当步长为负数时,是从右边到左以该步长的绝对值来获取的元素
print(a[0, 1, 3]) #不可以通过离散的索引值来获取

operation result:
insert image description here

Four. List method

1. append () add elements after the list

food=['egg', 'fish', 'cake', 'tomato']
food.append('ice') #在list的末尾添加元素
print(food)

operation result:
insert image description here

2. insert () adds elements at the specified position

food=['egg', 'fish', 'cake', 'tomato']
food.insert(1, 'meat') #在1这个位置添加元素
print(food)

operation result:
insert image description here

3.pop() deletes the element

food=['egg', 'fish', 'cake', 'tomato']
food.pop() #删除list末尾的元素
print(food)
food.pop(0) #删除索引0的元素
print(food)
food[0]='egg' #修改索引0的元素为egg
print(food)

operation result:
insert image description here

4.count() returns the number of elements in the list

list1 = [1, 3, 3, 4, 5]
print('count:', list1.count(3))#统计3在列表中出现的次数

operation result:
insert image description here

5.extend() 合并列表

list1 = [1, 3, 3, 4, 5]
list2 = [6, 5, 8, 9]
list1.extend(list2) #在列表1后面添加列表2
print(list1)

运行结果:
insert image description here

6.index()返回的是元素在列表中的第一个位置

list2 = [1, 2, 4, 5, 7, 4]
print('index:', list2.index(4))  # 从列表中找出第一个数值为4的索引位置,不管第二个

运行结果:
insert image description here

7.remove()---------- 删除某个元素,如果有重复,删除的是第一次出现的元素,如果元素不存在会报错

list2 = [1, 2, 4, 5, 7, 4]
list2.remove(4)#从列表中找出第一个数值为4的值然后删除,不管第二个
print('remove:', list2)

运行结果:
insert image description here

8.sort()进行排序(从小到大 int类型)可以对字母进行排序(ASCII值)类型不能混

list2 = [1, 2, 4, 5, 7, 4]
list2.sort()#对原列表进行排序
print('sort;', list2)

运行结果:
insert image description here

9.reverse()将列表进行翻转

list2 = [1, 2, 4, 5, 7, 4]
list2.reverse()
print('reverse;', list2)

运行结果:
insert image description here

10.clear() 清除元素

list2 = [1, 2, 4, 5, 7, 4]
list2.clear()
print(list2)

运行结果:
insert image description here

11.copy() 浅拷贝对象 不等价与 =

list1 = [8, 9, 0]
list2 = list1.copy()
print(list2)

运行结果:
insert image description here

五.列表的操作

1.拼接列表

print('列表相加:', [1, 2, 3]+ [4, 5, 6]) #列表的操作符 + 相当于拼接列表
print('列表相加:', [1, 2, 3]+['a', 'b'])

运行结果:
insert image description here

2.列表相乘

print('列表相乘:', ['a', 'b']*3) #列表相乘

运行结果:

insert image description here

3.判断

print('判断列表元素是否存在于列表中:', 'a' in ['a', 'b'])
print('判断列表元素是否存在于列表中:', 'a' not in['a', 'b'])

运行结果:
insert image description here

六.列表中元素的类型的多样性

a_list=['lemon', 100, ['a', 'b', 'c', 'd'], True]#同一个list的类型可以是字符串,整型,布尔型(true和false),以及嵌套的list
print(a_list)
print(a_list[0])
print(a_list[2])
print(a_list[3])

运行结果:
insert image description here

b_list=['a', 'b', 'c', 'd']
a_list=['lemon', 100, b_list, True]
print(a_list)
item_b=a_list[2][1]#获取嵌套中列表中的单个元素可以用二维的索引获取,类似于二维数组。[2]表示获取a_list的第三个元素,即b_list,#[1]表示获取b_list的第二个元素,即’b‘
print(item_b)

运行结果:
insert image description here

七.列表推导式

new_list=[x for x in iterable]其中的iterable表示可迭代的对象,包括字符串(str)、列表(list)、元组(tuple)、字典(dict)、集合(set)生成器(generator)

str_list=[x.lower()for x in 'Lemon']#lower()将大写转为小写
print(str_list)

运行结果:
insert image description here

list_list=[x**2 for x in [1, 2, 3, 4]]
print(list_list)

运行结果:
insert image description here

tuple_list=[x+2 for x in (1, 2, 3, 4)]
print(tuple_list)

运行结果:
insert image description here

ge_list=[x for x in range(8)]
print(ge_list)

运行结果:
insert image description here

八.列表常用方法总结

1.append()---------add elements after the list
2.insert()---------add elements to the specified subscript
3.pop()----- ----Delete element
4.count()------returns the number of an element in the list
5.extend() -------merge list
6.index()-- ------Returns the first position of the element in the list
7.remove()---------- Delete an element, if there are duplicates, delete the first occurrence Element, if the element does not exist, an error will be reported
8. sort()-------Sort (small to large int type) can sort letters (ASCII value) types cannot be mixed
9.reverse()---- ---Flip the list
10.clear() -------Clear the element
11.copy()----- The shallow copy object is not equivalent to =



Category of website: technical article > Blog

Author:cindy

link:http://www.pythonblackhole.com/blog/article/275/8815e2e88d00c3fbd048/

source:python black hole net

Please indicate the source for any form of reprinting. If any infringement is discovered, it will be held legally responsible.

12 0
collect article
collected

Comment content: (supports up to 255 characters)