-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfor_ex.py
More file actions
198 lines (176 loc) · 3.07 KB
/
for_ex.py
File metadata and controls
198 lines (176 loc) · 3.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# -*- coding:utf-8 -*-
#基本的for循环语句
test_list = [2,"Jone",3,6,7,'hongten','hanyuan','good',"Tom"]
#打印列表的长度
print(len(test_list))
#遍历列表
for i in test_list:
print(i)
test_str = "hello,i'm hongten"
print('print string: ' + test_str)
#遍历一个字符串
print('Traversal a string')
for i in test_str:
print(i)
test_tuple = [("a",1),("b",2),("c",3),("d",4)]
print(test_tuple)
#遍历一个元组
print('Traversal a tuple ')
for (i,j) in test_tuple:
print(i,j)
test_dict = {'name':'hongten','age':'20','gender':'M','sports':' football, bingpongball, swimming'}
#字典迭代器
for key in test_dict:
print(key + ':' + test_dict[key])
L1 = [1,3,5,7]
L2 = [2,4,6,8]
#使用zip将两个列表合并
print(zip(L1,L2))
for (i,j) in zip(L1,L2):
print(i,j)
print('#######################################################')
L3 = L2[:]
L3.remove(8)
print('L1,L3 list: ')
print(L1)
print(L3)
for (i,j) in zip(L1,L3):
print(i,j)
#可以看出来当长度不一的时候,多余的被忽略
test_keys = ['name','age','gender','weight','hight']
test_values = ['Hongten','20','M','55','170']
#使用zip来构造一个字典
print('dict\'s keys: ')
print(test_keys)
print('key\'s value:')
print(test_values)
print('after constructing ')
test_dic = dict(zip(test_keys,test_values))
for key in test_dic:
print( key + ':' + test_dic[key])
'''
D:\Py_exam>python for_ex.py
9
2
Jone
3
6
7
hongten
hanyuan
good
Tom
print string: hello,i'm hongten
Traversal a string
h
e
l
l
o
,
i
'
m
h
o
n
g
t
e
n
[('a', 1), ('b', 2), ('c', 3), ('d', 4)]
Traversal a tuple
('a', 1)
('b', 2)
('c', 3)
('d', 4)
gender:M
age:20
name:hongten
sports: football, bingpongball, swimming
[(1, 2), (3, 4), (5, 6), (7, 8)]
(1, 2)
(3, 4)
(5, 6)
(7, 8)
#######################################################
L1,L3 list:
[1, 3, 5, 7]
[2, 4, 6]
(1, 2)
(3, 4)
(5, 6)
dict's keys:
['name', 'age', 'gender', 'weight', 'hight']
key's value:
['Hongten', '20', 'M', '55', '170']
after constructing
gender:M
age:20
name:Hongten
weight:55
hight:170
D:\Py_exam>
'''
'''
Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>>
Jone
hongten
hanyuan
good
Tom
打印字符串:hello,i'm hongten
遍历一个字符串
h
e
l
l
o
,
i
'
m
h
o
n
g
t
e
n
[('a', 1), ('b', 2), ('c', 3), ('d', 4)]
遍历一个元组
a 1
b 2
c 3
d 4
sports:足球,乒乓球,游泳
gender:M
name:hongten
age:20
<zip object at 0x01FA1AA8>
2
4
6
8
#######################################################
L1,L3列表为:
[1, 3, 5, 7]
[2, 4, 6]
2
4
6
字典中的keys:
['name', 'age', 'gender', 'weight', 'hight']
字典中的key对应的value:
['Hongten', '20', 'M', '55', '170']
构造字典后
weight:55
hight:170
gender:M
name:Hongten
age:20
>>>
'''