序列的通用操作
序列 : 列表(list),元组(tuple),字典(dict),集合(set)
1. list()函数#
print(list("abd")) #字符串转列表
print(list(("1","aaa","哈哈"))) # 元组转列表
print(list(range(0,6))) # range()转列表
2. 判断元素是否在序列内(in/not in)
a = list(range(0,6))
b = ["tom","zhangsan","马大哈"]
print(1 in a)#判断1是否在列表里,如果在返回True
print(10 not in a)
print("tom" in b)
3. 连接序列
a = list(range(0,6))
b = ["tom","zhangsan","马大哈"]
c=a+b
print(c)
4. 重复输出序列元素
print(["shi","sheng","yi"] * 3)
5. 通过下标获取元素,单个访问,范围访问(切片),步长访问
c = list(range(0,7))
print(c[::2])
6. 获取序列的长度
c = list(range(0,7))
print(len(c))
7. 最大值max(),最小值min(),求和sum()
c = list(range(0, 7))
print(max(c))
print(min(c))
print(sum(c))
8. 检测序列中的某个元素第一次出现的位置
d = [1,3,2,4,2,5,2]
print(d.index(4))
9. 统计某个元素出现的次数
d = [1,3,2,4,2,5,2]
print(d.count(2))