list 应用的三种方式 and 切片 (步长) index
# List 应用
# 方式一:
List_01 = [1,23,2]
print(List_01) #[‘1’, ‘23’ ,’2’]
# 方法二:
# 加入可迭代对象------ list_01 = list(iterable)
s1 = list('1516741niabjihbu')
print(s1) # [“1”, “5”, “1”, “6”, “7”, “4”, “1”, “n”, “i”, “a”, “b”, “j”, “i”, “h”, “b”, “u”]
#
# 方法三:
# 列表推导式
# [i for i in iterrable] (标签;发生变化后的标签 for 标签 in iterable)
# i 是可迭代对象的元素 前边的 i 需要发生变化 会把i发生变化后传出
list_02 = [i for i in "nihao"]
print(list_02) # ['n', 'i', 'h', 'a', 'o']
list_02 = [i*2 for i in "nihao"]
print(list_02) # ["nn","ii","hh","aa","oo"
# 使用range 范围
list_02 = [i for i in range(1,10)] # 区间1-10 的元素
print(list_02) # ['1', '2', '3', '4', '5', '6', '7','8','9']
# 添加条件 用 if
list_02 = [i*2 for i in range(1,6) if i%s ==2] # 区间1-6 的偶数 *2
print(list_02) # ['4', '8']
# 生成器推导式 用 for in 列
list_01 = [i for i in range(1,5)]
for i in list_01:
print(i)
# 1
# 2
# 3
# 4
##list的简单操作 list变量[值下标:结束值下标]
# 切片 [:] 左边开始位置 右边终止位置(不带该位置)
list_03 = [1,2,3,6,5,9]
print(list_03[:3]) # [1, 2, 3]
#步长 [::]
# [1::2] 从下标1的数据开始取值,取后面全部的值, 并且取值的时候设置了步长,取值的时候跳着取,跳2个下标
list_03 = [1,2,3,6,5,9]
print(list_03[::3])# [1, 6]
# [::-1] 表示倒数
list_03 = [1,2,3,6,5,9]
print(list_03[::-1]) # [9, 5, 6, 3, 2, 1]
# 利用 索引 len 辅助
list_03 = [1,2,3,6,5,9]
l = 0
while l <= len(list_03)-1:
print(list_03[l])
l += 1
# 1
# 2
# 3
# 6
# 5
# 9
list_03 = [1,2,3,6,5,9]
for i in list_03:
print(i)
# 1
# 2
# 3
# 6
# 5
# 9