Python 如何同时循环不同的列表?
我正在尝试循环一系列字符串:Python 如何同时循环不同的列表?,python,string,list,loops,Python,String,List,Loops,我正在尝试循环一系列字符串: someplace["Canada", "USA", "England"] for charNum in range(len(lst)): #charNum the length of the characters print(lst[charNum]) for country in range(len([charNum])): #country is the the string print(lst[charNum][country]) 我试图
someplace["Canada", "USA", "England"]
for charNum in range(len(lst)): #charNum the length of the characters
print(lst[charNum])
for country in range(len([charNum])): #country is the the string
print(lst[charNum][country])
我试图实现的输出是:
c U E
a S n
n A g
a l
d a
a n
d
更多详情:
for k in range(len(lst[0])):
print(lst[0][k])
如果运行此命令,它将打印出来
c
a
n
a
d
a
这是因为它得到的是索引0的长度。但我必须循环其他数字:0,1,2
我取得了一些进展,创建了嵌套for循环:
for i in range(len(lst)): # length of list
for j in range(len(lst[i])): # length of each string
print(lst[i][j])
要将列表国际化,您需要有一个列表
my_list = ["Paris"]
要将列表国际化,您需要有一个列表
my_list = ["Paris"]
要将列表国际化,您需要有一个列表
my_list = ["Paris"]
要将列表国际化,您需要有一个列表
my_list = ["Paris"]
用于同时在所有位置上循环
from itertools import izip_longest # zip_longest in python 3
places = ["Canada", "USA", "England"]
for chars in izip_longest(*places, fillvalue=' '):
print(' '.join(chars))
输出:
过程:
izip_longest
的输出为:
[('C', 'U', 'E'), ('a', 'S', 'n'), ('n', 'A', 'g'), ('a', ' ', 'l'), ('d', ' ', 'a'), ('a', ' ', 'n'), (' ', ' ', 'd')]
然后,for循环从('C','U','E')开始,按顺序将每个“行”分配给字符
join(chars)
将该元组组合成一个字符串,每个列表成员之间有空格。对于第一个元素,它将是'cue'
用于同时循环所有元素
from itertools import izip_longest # zip_longest in python 3
places = ["Canada", "USA", "England"]
for chars in izip_longest(*places, fillvalue=' '):
print(' '.join(chars))
SomePlaces = ['Canada', 'USA', 'England'] //Array
for SomePlaces in range(len(lst)):
print 'Places :', lst[SomePlaces]
输出:
过程:
izip_longest
的输出为:
[('C', 'U', 'E'), ('a', 'S', 'n'), ('n', 'A', 'g'), ('a', ' ', 'l'), ('d', ' ', 'a'), ('a', ' ', 'n'), (' ', ' ', 'd')]
然后,for循环从('C','U','E')开始,按顺序将每个“行”分配给字符
join(chars)
将该元组组合成一个字符串,每个列表成员之间有空格。对于第一个元素,它将是'cue'
用于同时循环所有元素
from itertools import izip_longest # zip_longest in python 3
places = ["Canada", "USA", "England"]
for chars in izip_longest(*places, fillvalue=' '):
print(' '.join(chars))
SomePlaces = ['Canada', 'USA', 'England'] //Array
for SomePlaces in range(len(lst)):
print 'Places :', lst[SomePlaces]
输出:
过程:
izip_longest
的输出为:
[('C', 'U', 'E'), ('a', 'S', 'n'), ('n', 'A', 'g'), ('a', ' ', 'l'), ('d', ' ', 'a'), ('a', ' ', 'n'), (' ', ' ', 'd')]
然后,for循环从('C','U','E')开始,按顺序将每个“行”分配给字符
join(chars)
将该元组组合成一个字符串,每个列表成员之间有空格。对于第一个元素,它将是'cue'
用于同时循环所有元素
from itertools import izip_longest # zip_longest in python 3
places = ["Canada", "USA", "England"]
for chars in izip_longest(*places, fillvalue=' '):
print(' '.join(chars))
SomePlaces = ['Canada', 'USA', 'England'] //Array
for SomePlaces in range(len(lst)):
print 'Places :', lst[SomePlaces]
输出:
过程:
izip_longest
的输出为:
[('C', 'U', 'E'), ('a', 'S', 'n'), ('n', 'A', 'g'), ('a', ' ', 'l'), ('d', ' ', 'a'), ('a', ' ', 'n'), (' ', ' ', 'd')]
然后,for循环从('C','U','E')开始,按顺序将每个“行”分配给字符
join(chars)
将该元组组合成一个字符串,每个列表成员之间有空格。对于第一个元素,这将是'cue'
SomePlaces = ['Canada', 'USA', 'England'] //Array
for SomePlaces in range(len(lst)):
print 'Places :', lst[SomePlaces]
注意:我没有测试它,如果我错了,很抱歉
注意:我没有测试它,如果我错了,很抱歉
注意:我没有测试它,如果我错了,很抱歉
注意:我没有测试它,如果我错了,很抱歉。我还没有学会使用这个方法。我正在学习python 3的开始步骤。我一直在讨论如何更改for循环中的索引号。@user3339203,我编辑了答案,并对该过程进行了更完整的解释。这样,我们就不会更改索引号。我们直接访问成员我还没有学会使用这种方法。我正在学习python 3的开始步骤。我一直在讨论如何更改for循环中的索引号。@user3339203,我编辑了答案,并对该过程进行了更完整的解释。这样,我们就不会更改索引号。我们直接访问成员我还没有学会使用这种方法。我正在学习python 3的开始步骤。我一直在讨论如何更改for循环中的索引号。@user3339203,我编辑了答案,并对该过程进行了更完整的解释。这样,我们就不会更改索引号。我们直接访问成员我还没有学会使用这种方法。我正在学习python 3的开始步骤。我一直在讨论如何更改for循环中的索引号。@user3339203,我编辑了答案,并对该过程进行了更完整的解释。这样,我们就不会更改索引号。我们直接访问成员如果你不测试你的代码,不要抱怨否决票。你把变量名
SomePlaces
和lst
混在一起,你的注释语法是错误的,但最重要的是,你的答案甚至没有尝试打印OP要求的内容!如果不测试代码,不要抱怨否决票。你把变量名SomePlaces
和lst
混在一起,你的注释语法是错误的,但最重要的是,你的答案甚至没有尝试打印OP要求的内容!如果不测试代码,不要抱怨否决票。你把变量名SomePlaces
和lst
混在一起,你的注释语法是错误的,但最重要的是,你的答案甚至没有尝试打印OP要求的内容!如果不测试代码,不要抱怨否决票。你把变量名SomePlaces
和lst
混在一起,你的注释语法是错误的,但最重要的是,你的答案甚至没有尝试打印OP要求的内容!