Python | 数据类型之列表

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

知识目录


列表是最常用的Python数据类型它可以作为一个方括号内的逗号分隔值出现。

列表的数据项不需要具有相同的类型

创建一个列表只要把逗号分隔的不同的数据项使用方括号括起来即可

一、定义和遍历

1.1 定义列表

list = []  # 1.定义一个空列表
print(type(list))

namelist = ["xiaoguo", "123", "321"]  # 2.定义并初始化一个列表
print(namelist[0])
print(namelist[1])
print(namelist[2])

list1 = [123, "Jerry"]  # 3.列表可以存储不同数据类型
print(type(list1[0]))
print(type(list1[1]))

1.2 打印和截取列表

list = [11,22,33]
print(list) # 1.打印列表所有元素
print(list[1:3]) # 2.方括号截取左闭右开

nameList = ["xiaoguo","xiaojiang","xiaoma"]
print(nameList[2]) # 读取列表第三个元素
print(nameList[-2]) # 读取列表倒数第二个元素
print(nameList[1:]) # 从第二个元素读到最后
print(nameList[:2]) # 从第一个元素读到第二个元素

1.3 遍历列表

namelist = ["xiaoguo", "123", "321"]  # 定义并初始化一个列表
for name in namelist:
    print(name, end="\t")  # for循环遍历
    
print()

length = len(namelist)  # 获取列表长度
i = 0
while i < length:  # while循环遍历
    print(namelist[i], end="\t")
    i += 1

二、增删改查

2.1 更新和删除列表

list1 = [];
list1.append("Str") # 1.使用append()函数追加元素
list1.append(123)
print(list1)
list2 = [123,"Json","carry",6]
print(list2)
# 1.del()
del list2 # 删除整个列表
del list2[1]  # 删除第二个元素
print(list2)

# 2.pop() 移除列表中的一个元素默认最后一个元素并且返回该元素的值
print(list2.pop())
print(list2)
print(list2.pop(0))

# 3.remove() 移除列表中某个值的第一个匹配项
list3 = ["tom", "jery", "kete", "hello", "tom"]
list3.remove("tom")
print(list3)


2.3 脚本操作符 * + in

list = [1,"小王","xiaoLi"]
length = len(list) # 长度
print(length)

list1 = [2,"xiaoguo"]
print(list + list1) # 相加

list2 = list1*3 # 重复
print(list2)

b = 1 in list2 # 判断元素是否在列表中
print(b)

三、函数和方法

函数和方法的区别
与类和实例无绑定关系的function都属于函数function与类和实例有绑定关系的function都属于方法method
方法和函数类似同样封装了独立的功能简单的说函数在python中独立存在是可以直接使用的而方法必须是别人调用才能实现的

3.1 函数

函数用法作用
cmp()Python 3.X 的版本中已经没有 cmp 函数如果你需要实现比较功能需要引入 operator 模块适合任何对象,例如operator.lt(a,b)
len()列表元素个数
max()列表元素最大值在列表中的元素是同类型时才可以比较否则报错如含有数字和字符串的列表不能比较
min()列表元素最小值规则和max函数一致
list()将元组转化为列表元组和列表是非常相似的区别在于元组的值不能更改包含在圆括号中
# 1.cmp() 比较
list1 = [11, 22, 33, "123"]
list2 = [22, 33]
print(operator.lt(list1, list2))
# 2.len() 长度
print("length:%d"%len(list2))
# 3.max()
print(max(list2))  
# 4.min()
print(min(list2))  
# 5.list() 
tmp = (1, 2, 3, "jack")
print(type(tmp))
print(list(tmp))  # 将元组转化为列表

3.2 方法

# 1.append() 
list1 = ["mali", "tom"]
list1.append([12])
print(list1)

# 2.count() 
print(list1.count("tom"))

# 3.extend() 
list1.extend(["str", 1])
print(list1)

# 4.index()
print(list1.index(1))

# 5.insert()
list2 = [11, 22, 33]
list2.insert(1, 123)
print(list2)

# 6.pop()
print(list2.pop())
print(list2)
print(list2.pop(0))

# 7.remove()
list3 = ["tom", "jery", "kete", "hello", "tom"]
list3.remove("tom")
print(list3)

# 8.reverse()
list4 = [1, 2, 3]
list4.reverse()
print(list4)

# 9.sort()
list5 = [1, 5, -234, 329]
list5.sort()
print(list5)
list5.sort(reverse=True)
print(list5)

列表例题8位老师随机分到3个办公室

# 定义三个办公室
offices = [[], [], []]
# 定义8个老师
teachers = ["A", "B", "C", "D", "E", "F", "G", "H"]
for name in teachers:
    # 生成0-2的3个随机数
    ran = random.randint(0, 2)
    # 将老师添加到随机办公室中
    offices[ran].append(name)
# 查看每间办公室的人数
i = 1
for office in offices:
    print("第%d间办公室有%d人分别是" %(i, len(office)))
    i+=1
    for off in office:
        print(off, end="\t")
    print()

首先恭喜大家已经阅读完这篇文章啦虽然我尽量以通俗简单的形式将内容体现出来但水平有限望大家海涵。我们下期再见拜拜~

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6
标签: python