数据类型,列表
功能:
创建: 空列表 a = [] 非空列表 a = ['alex','jack','ella','mark'] b = a.copy()
查询: 由索引查找元素,由元素查找索引,
切片: 默认顾头不顾尾
增加: append insert
删除: clear del remove
循环: for
排序:sort
循环names列表,打印每个元素的索引值,和元素,当索引值为偶数时,把对应得元素改成-1
names = ["old_driver", "rain", ["oldboy", "oldgirl"], "jack", "姗姗", "peiqi", "alex", "black_girl", 1, 2, 3, 4, 2, 5, 6, 2] # enumerate()枚举 for index, i in enumerate(names): if index % 2 == 0: names[index] = -1 print(index, i) print(names)
names里面有3个2,请返回第2个2 的索引值,不要人肉数,要动态查找(提示,找到第一个2的位置,在此基础上再找第2 个)
1 names = ["old_driver", "rain", ["oldboy", "oldgirl"], "jack", "姗姗", "peiqi", "alex", "black_girl", 1, 2, 3, 4, 2, 5, 6, 2] 2 3 first_index = names.index(2) 4 5 new_list = names[first_index+1:]#切片 6 second_index = new_list.index(2) 7 second_val = first_index+second_index+1 8 print(new_list, first_index, second_index) 9 10 print('second values: ', second_val)