列表的常规使用
初始化
def test() -> None:
my_list: list = [1, 2, 3, "zhangSan", [3, 5]]
print(my_list)
if __name__ == '__main__':
test()
查询
def test() -> None:
my_list: list = [1, 2, 3, "zhangSan", [3, 5]]
print(my_list)
print(type(my_list))
print(type(my_list[0]), my_list[0])
print(type(my_list[4][1]), my_list[4][1])
for i in range(len(my_list)):
if type(my_list[i]) is list:
for j in range(len(my_list[i])):
print(my_list[i][j])
else:
print(my_list[i])
print(type(my_list[1:2]), my_list[1:2])
print("zhangSan" in my_list)
if __name__ == '__main__':
test()
修改
def test() -> None:
my_list: list = [1, 2, 3, "zhangSan", [3, 5]]
print(my_list)
print(type(my_list))
my_list[0] = 100
print(my_list)
if __name__ == '__main__':
test()
添加
def test() -> None:
my_list: list = [1, 2, 3, "zhangSan", [3, 5]]
print(my_list)
print(type(my_list))
my_list.append(100)
print(my_list)
list.append(my_list, "address")
print(my_list)
my_list.insert(0, 200)
print(my_list)
my_list.insert(0, ["hello", "python"])
print(my_list)
my_list.extend([1, 2, 3, 1000, 2000, 3000])
print(my_list)
if __name__ == '__main__':
test()
删除
def test() -> None:
my_list: list = [1, 2, 3, "zhangSan", [3, 5]]
print(my_list)
print(type(my_list))
res = my_list.pop(3)
print(res)
print(my_list)
my_list = [100, 200, 300, 100, 500]
my_list.remove(100)
print(my_list)
if __name__ == '__main__':
test()
排序 、 反转
def test() -> None:
my_list: list = [100, 200, 300, 100, 500]
my_list.sort()
print(my_list)
my_list.sort(reverse=True)
print(my_list)
print(my_list)
my_list.reverse()
print(my_list)
if __name__ == '__main__':
test()
获取长度
def test() -> None:
my_list: list = [1, 2, 3, "zhangSan", [3, 5]]
print(my_list)
print(type(my_list))
print(len(my_list))
if __name__ == '__main__':
test()
|