码迷,mamicode.com
首页 > 编程语言 > 详细

python基础操作---list

时间:2019-06-10 13:45:46      阅读:101      评论:0      收藏:0      [点我收藏+]

标签:odi   mis   python基础   str   pen   utf-8   for   切片   print   

 1 #coding:utf-8
 2 list1 = [physics, chemistry, 1997, 2000];
 3 list2 = [1, 2, 3, 4, 5 ];
 4 list3 = ["a", "b", "c", "d"];
 5 
 6 #切片功能跟str一样
 7 print "list1[0]: ", list1[0]
 8 print "list2[1:5]: ", list2[1:5]
 9 print list1[::-1]
10 list3.append("e")# 追加
11 print list3
12 
13 del list1[2];
14 print list1;
15 
16 list4 = list2 + list3
17 
18 print list4
19 
20 print 3 in list2
21 
22 for item in list2:
23 print item
24 
25 #等价
26 # for i in range(len(list2)):
27 # print list2[i]
28 
29 # list_2d = [[0 for col in range(cols)] for row in range(rows)]
30 list_2d = [ [0 for i in range(5)] for i in range(4)] #2维数据, 0为初始化数值
31 
32 print list_2d
33 list_2d[0][1]=1
34 print list_2d

 

输出----------------------------------------------------
list1[0]: physics
list2[1:5]: [2, 3, 4, 5]
[2000, 1997, ‘chemistry‘, ‘physics‘]
[‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘]
[‘physics‘, ‘chemistry‘, 2000]
[1, 2, 3, 4, 5, ‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘]
True
1
2
3
4
5
[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
[[0, 1, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]

python基础操作---list

标签:odi   mis   python基础   str   pen   utf-8   for   切片   print   

原文地址:https://www.cnblogs.com/xiebinbo/p/10997075.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!