python基础操作---list

2020-12-11 10:06

阅读:653

标签: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


评论


亲,登录后才可以留言!