posted on 2023-05-03 20:40 read(910) comment(0) like(12) collect(0)
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
names=['james', 'michael', 'emma', 'emily']
print('names的数据类型:', type(names))#type()函数是查看变量类型
print(names)
The result of the operation is as follows:
ls2 = list([1, 2, 3, 4, 5])
print(ls2)
The result of the operation is as follows:
age=[]#直接创建空列表
print(age)
print(len(age))#len()函数计算长度
ls1 = list()#通过list()函数创建
print(ls1)
operation result:
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:
names = ['james', 'michael', 'emma', 'emily']
for name in names:
print(name)
for i in range(len(names)):
print(names[i])
operation result:
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:
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:
food=['egg', 'fish', 'cake', 'tomato']
food.append('ice') #在list的末尾添加元素
print(food)
operation result:
food=['egg', 'fish', 'cake', 'tomato']
food.insert(1, 'meat') #在1这个位置添加元素
print(food)
operation result:
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:
list1 = [1, 3, 3, 4, 5]
print('count:', list1.count(3))#统计3在列表中出现的次数
operation result:
list1 = [1, 3, 3, 4, 5]
list2 = [6, 5, 8, 9]
list1.extend(list2) #在列表1后面添加列表2
print(list1)
运行结果:
list2 = [1, 2, 4, 5, 7, 4]
print('index:', list2.index(4)) # 从列表中找出第一个数值为4的索引位置,不管第二个
运行结果:
list2 = [1, 2, 4, 5, 7, 4]
list2.remove(4)#从列表中找出第一个数值为4的值然后删除,不管第二个
print('remove:', list2)
运行结果:
list2 = [1, 2, 4, 5, 7, 4]
list2.sort()#对原列表进行排序
print('sort;', list2)
运行结果:
list2 = [1, 2, 4, 5, 7, 4]
list2.reverse()
print('reverse;', list2)
运行结果:
list2 = [1, 2, 4, 5, 7, 4]
list2.clear()
print(list2)
运行结果:
list1 = [8, 9, 0]
list2 = list1.copy()
print(list2)
运行结果:
print('列表相加:', [1, 2, 3]+ [4, 5, 6]) #列表的操作符 + 相当于拼接列表
print('列表相加:', [1, 2, 3]+['a', 'b'])
运行结果:
print('列表相乘:', ['a', 'b']*3) #列表相乘
运行结果:
print('判断列表元素是否存在于列表中:', 'a' in ['a', 'b'])
print('判断列表元素是否存在于列表中:', 'a' not in['a', 'b'])
运行结果:
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])
运行结果:
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)
运行结果:
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)
运行结果:
list_list=[x**2 for x in [1, 2, 3, 4]]
print(list_list)
运行结果:
tuple_list=[x+2 for x in (1, 2, 3, 4)]
print(tuple_list)
运行结果:
ge_list=[x for x in range(8)]
print(ge_list)
运行结果:
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 =
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.
name:
Comment content: (supports up to 255 characters)
Copyright © 2018-2021 python black hole network All Rights Reserved All rights reserved, and all rights reserved.京ICP备18063182号-7
For complaints and reports, and advertising cooperation, please contact vgs_info@163.com or QQ3083709327
Disclaimer: All articles on the website are uploaded by users and are only for readers' learning and communication use, and commercial use is prohibited. If the article involves pornography, reactionary, infringement and other illegal information, please report it to us and we will delete it immediately after verification!