435421 发表于 2016-4-25 09:25:20

python的list由多个tuple组成,如何返回每个tuple的每个元素


这个问题,看着也很简单:

比如:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
list_1 = [('a','b'),('c','d'),('e','f')]

for i in list_1:
    print i
   
   
运行结果会返回:
('a', 'b')
('c', 'd')
('e', 'f')

返回的结果类型是tuple,即list的每个构成单元

现在希望返回的是每个元组的每个元素,就是说,希望分别取得每个元组的所有元素:
list_1 = [('a','b'),('c','d'),('e','f')]
for x,y in list_1:
    print x,y




运行程序,返回的结果是:
a b
c d
e f

返回的类型是字符串,即list的每个构成单元的构成元素


页: [1]
查看完整版本: python的list由多个tuple组成,如何返回每个tuple的每个元素