这里会显示出您选择的修订版和当前版本之间的差别。
python-basic:list [2010/06/02 01:18] |
python-basic:list [2010/06/02 01:18] (当前版本) |
||
---|---|---|---|
行 1: | 行 1: | ||
+ | ====== Python 列表(list)操作 ====== | ||
+ | |||
+ | ===== 创建列表 ===== | ||
+ | <code python> | ||
+ | sample_list = ['a',1,('a','b')] | ||
+ | </code> | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | ===== Python 列表操作 ===== | ||
+ | <code python> | ||
+ | sample_list = ['a','b',0,1,3] | ||
+ | </code> | ||
+ | |||
+ | ==== 得到列表中的某一个值 ==== | ||
+ | |||
+ | <code python> | ||
+ | value_start = sample_list[0] | ||
+ | end_value = sample_list[-1] | ||
+ | </code> | ||
+ | |||
+ | ==== 删除列表的第一个值 ==== | ||
+ | |||
+ | <code python> | ||
+ | del sample_list[0] | ||
+ | </code> | ||
+ | |||
+ | ==== 在列表中插入一个值 ==== | ||
+ | |||
+ | <code python> | ||
+ | sample_list[0:0] = ['sample value'] | ||
+ | </code> | ||
+ | |||
+ | ==== 得到列表的长度 ==== | ||
+ | |||
+ | <code python> | ||
+ | list_length = len(sample_list) | ||
+ | </code> | ||
+ | |||
+ | ==== 列表遍历 ==== | ||
+ | |||
+ | <code> | ||
+ | for element in sample_list: | ||
+ | print(element) | ||
+ | </code> | ||
+ | |||
+ | ===== Python 列表高级操作/技巧 ===== | ||
+ | ==== 产生一个数值递增列表 ==== | ||
+ | <code python> | ||
+ | num_inc_list = range(30) | ||
+ | #will return a list [0,1,2,...,29] | ||
+ | </code> | ||
+ | |||
+ | ==== 用某个固定值初始化列表 ==== | ||
+ | <code python> | ||
+ | initial_value = 0 | ||
+ | list_length = 5 | ||
+ | sample_list = [ initial_value for i in range(10)] | ||
+ | sample_list = [initial_value]*list_length | ||
+ | # sample_list ==[0,0,0,0,0] | ||
+ | </code> | ||