Python 之列表

1.基本操作

#输出
color=['red','green','purple','blue']
print(color)

#索引输出
print(color[0])

#修改
color[0] = 'white'
print(color)

#在末尾添加
color.append('black')
print(color)

#插入元素
color.insert(0,'yellow')
print(color)

#使用del删除元素
del color[0]
print(color)

#使用pop删除元素
#删除栈顶
pop_color = color.pop()
print(pop_color)
print(color)

#删除指定索引
pop_color = color.pop(0)
print(pop_color)
print(color)

#若不使用删除值,使用del,否则使用pop

#删除指定值
color.remove('blue')
print(color)

2.高级操作

#排序
color=['red','green','blue','purple']
print(color)
//从小到大
color.sort()
print(color)
//从大到小
color.sort(reverse=True)
print(color)

#临时排序,不改变列表的值
color=['red','green','blue','purple']
print(sorted(color))
print(sorted(color,reverse=True))
print(color)

#列表翻转
color.reverse();
print(color)

#列表长度
print(len(color))

3.访问链表

#循环访问
color=['red','green','blue','purple']
for col in color:
	print(col)

#创建数字列表
for value in range(1,10):
	print(value)

#数字列表统计
even_number = list(range(2,11,2))
print(even_number)
print(max(even_number))
print(min(even_number))
print(sum(even_number))

#切片
color=['red','green','blue','purple']
print(color[1:3])
print(color[:4])
print(color[1:])
print(color[:-1])

#复制列表
#仅生成一个新的变量名
color1 = color
#生成一个新的列表
color2 = color[:]

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值