list1 = [1, 18, 30, 22, 4, 67, 56]
#增
list1.append(100)
#删
del list1[1]
#改
list1[0] = 3
#查
if 30 in list1:
print("30 is in list1")
#取数据
#1.下标取值
print(list1[1]) #取下标为1的值
print(list1[-2]) #取倒数第二个值
print(list1[1:3]) #取下标为1,2的值
print(list1[1:]) #取下标1到结尾的值
#列表相加
other = [123, 456]
list1 += other
print(list1)
#func
#1.比较两个列表的元素,两个列表的值为同一类型
if list1 > other:
print("list1 > other")
else:
print("list1 < other")
#2. 列表的长度
print(len(list1))
#3. 列表的值为同一类型,可以求最小值
print(min(list1))
#4. 元祖转列表
tup = (1, 2, 3, 4)
print(list(tup))
#5. 添加元素
list1.append('string')
print(list1)
#6. 某个元素在列表中的出现的次数
list1.append(22)
print(list1.count(22))
#7.扩展列表
newList = [1, 2, 3, 4]
list1.extend(newList)
print(list1)
#8.获取元素的索引
if 0 in list1:
print(list1.index(0))
#9.插入元素
list1.insert(2, 'hello')
print(list1)
#10.删除匹配元素
if 0 in list1:
list1.remove(0)
#11.反向列表中的元素
list1.reverse()
print(list1)
#12.列表排序 // 同一类型元素
list_1 = [1, 34, 23, 9]
list_1.sort()
print(list_1)
网友评论