Python-列表
2018-10-11 本文已影响5人
GaoYuan117
names = ["小明", "小花", "小红", "略略", "呵呵"]
print(names[0]) # 小明 取值
print(names[-1]) # 小红 从后面取值
print(names[1:3]) # 切片['小花', '小红'] 左闭右开
print(names[1:3:2]) # 切片 步长
print(names[-2:-1]) # ['小明', '小花'] 从后面切片
names.append("哈哈") # 追加
print(names)
names.insert(0, "嘿嘿") # 插入
print(names)
names[2] = "吼吼" # 改
print(names)
names.remove("吼吼") # 删
print(names)
del names[0] # 删
print(names)
names.pop(0) # 删 默认删除最后一个
print(names)
print(names.index("小红")) # 确认位置
print(names.count("小红")) # 统计数量
names.reverse() # 反转
print(names)
names.sort() # 排序
print(names)
names2 = [1, 2, 3]
names.extend(names2) # 合并
print(names)
names3 = names.copy() # 复制 只copy第一层
print(names3)
name4 = ["小明", "小花", ["嘿嘿", "哦哦"], "小红"]
print(name4[2][0])
for i in name4: # for循环
print(i)