循环循环? [英] Loop in a loop?

查看:62
本文介绍了循环循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我是Python的新手,遇到了一个我不知道如何解决的问题,请输入com.lang.python :)


我正在写一些小应用程序来学习这门语言,而且我很喜欢

到目前为止。


我的问题我偶然发现,我不知道怎么做我想要的b $ b b。我想在循环中做一个循环。我想。


我有两个随机内容的数组,就像这样。


array1 = [''one' ','''''',''''',''4'''

array2 = [''''',''b'',''c'',' 'd'']


我想循环遍历array1并在最后添加array2中的元素,

所以它看起来像这样:


一个

两个b

三个c

四个c


我被困住了。我知道如何分别遍历数组并打印

它们,但两者同时存在?嗯。


推动正确的方向,任何人?


R,

SH

解决方案

1月17日下午1点21分,Sacred Heart< scrd ... @ gmail.comwrote:




我是Python的新手,遇到了一个我不知道如何解决的问题,请输入com.lang .python :)


我正在写一些小应用程序来学习这门语言,而且我很喜欢

到目前为止。


我的问题我偶然发现,我不知道如何做我想要的b $ b b。我想在循环中做一个循环。我想。


我有两个随机内容的数组,就像这样。


array1 = [''one' ','''''',''''',''4'''

array2 = [''''',''b'',''c'',' 'd'']


我想循环遍历array1并在最后添加array2中的元素,

所以它看起来像这样:


一个

两个b

三个c

四个c


我被困住了。我知道如何分别遍历数组并打印

它们,但两者同时存在?嗯。


推动正确的方向,任何人?


R,

SH



for i in zip(array1,array2):

print i


虽然我认为你意味着四,这个方法的问题是

,一旦你到达一个数组的末尾,另一个数据的其余部分是

被忽略。


1月17日下午1点35分,cokofree ... @ gmail.com写道:


for i in zip(array1, array2):

打印我


虽然我认为你的意思是四,这个方法的问题是

一次你击中一个阵列的末尾,另一个阵列的其余部分是

被忽略。



是的,那里有小错字。


Okey,所以如果我的array1有4个元素,而array2有6个,它不会通过array2中的最后2个循环
循环?我怎么做到这一点?


R,

SH


1月17日下午2:35,cokofree ... @ gmail.com写道:


1月17日下午1点21分,Sacred Heart< scrd。 .. @ gmail.comwrote:




我是Python的新手并且遇到了一个问题我没有''知道如何解决,请输入com.lang.python :)


我正在写一些小应用程序学习这门语言,我很喜欢

到目前为止。


我的问题我偶然发现我不知道怎么做我想要的b $ b想要什么。我想在循环中做一个循环。我认为。


我有两个带有一些随机内容的数组,就像这样。


array1 = [''one'',''two'',''three'',''four'']

array2 = ['''',''b'',''c'',''d'']


我想循环遍历array1并在最后添加array2中的元素,

所以它看起来像这样:


一个

两个b

三个c

四个c


我被困住了。我知道如何分别遍历数组并打印

它们,但两者同时存在?嗯。


推进正确的方向,有人吗?


R,

SH



for i in zip(array1,array2):

打印我


虽然我认为它是4 d,但这个方法的问题是

,一旦你到达一个阵列的末尾,另一个阵列的其余部分将被忽略。



在使用zip

功能之前,您可以随时预先填写您使用的列表,有点像

def pad(* iterables):

max_length = 0

for iterables中的each_iterable:

if len(each_iterable)max_length :max_length =

len(each_iterable)

for iterables中的each_iterable:

each_iterable.extend([x in xrange(0,max_length) -

len(each_iterable))])


pad(array1,array2,array3)

for i in zip(array1 ,array2,array3):

print i


您还可以创建一个索引来使用它。


for i in xrange(0,length_of_longest_list):

try:print array1 [i]

除了IndexError:传递

尝试:print array2 [i]
除了IndexError之外的
:传递


Hi,
I''m new to Python and have come across a problem I don''t know how to
solve, enter com.lang.python :)

I''m writing some small apps to learn the language, and I like it a lot
so far.

My problem I''ve stumbled upon is that I don''t know how to do what I
want. I want to do a loop in a loop. I think.

I''ve got two arrays with some random stuff in, like this.

array1 = [''one'',''two'',''three'',''four'']
array2 = [''a'',''b'',''c'',''d'']

I want to loop through array1 and add elements from array2 at the end,
so it looks like this:

one a
two b
three c
four c

I''m stuck. I know how to loop through the arrays separatly and print
them, but both at the same time? Hmmm.

A push in the right direction, anyone?

R,
SH

解决方案

On Jan 17, 1:21 pm, Sacred Heart <scrd...@gmail.comwrote:

Hi,
I''m new to Python and have come across a problem I don''t know how to
solve, enter com.lang.python :)

I''m writing some small apps to learn the language, and I like it a lot
so far.

My problem I''ve stumbled upon is that I don''t know how to do what I
want. I want to do a loop in a loop. I think.

I''ve got two arrays with some random stuff in, like this.

array1 = [''one'',''two'',''three'',''four'']
array2 = [''a'',''b'',''c'',''d'']

I want to loop through array1 and add elements from array2 at the end,
so it looks like this:

one a
two b
three c
four c

I''m stuck. I know how to loop through the arrays separatly and print
them, but both at the same time? Hmmm.

A push in the right direction, anyone?

R,
SH

for i in zip(array1, array2):
print i

Although I take it you meant four d, the issue with this method is
that once you hit the end of one array the rest of the other one is
ignored.


On Jan 17, 1:35 pm, cokofree...@gmail.com wrote:

for i in zip(array1, array2):
print i

Although I take it you meant four d, the issue with this method is
that once you hit the end of one array the rest of the other one is
ignored.

Yes, small typo there.

Okey, so if my array1 is has 4 elements, and array2 has 6, it won''t
loop trough the last 2 in array2? How do I make it do that?

R,
SH


On Jan 17, 2:35 pm, cokofree...@gmail.com wrote:

On Jan 17, 1:21 pm, Sacred Heart <scrd...@gmail.comwrote:

Hi,
I''m new to Python and have come across a problem I don''t know how to
solve, enter com.lang.python :)

I''m writing some small apps to learn the language, and I like it a lot
so far.

My problem I''ve stumbled upon is that I don''t know how to do what I
want. I want to do a loop in a loop. I think.

I''ve got two arrays with some random stuff in, like this.

array1 = [''one'',''two'',''three'',''four'']
array2 = [''a'',''b'',''c'',''d'']

I want to loop through array1 and add elements from array2 at the end,
so it looks like this:

one a
two b
three c
four c

I''m stuck. I know how to loop through the arrays separatly and print
them, but both at the same time? Hmmm.

A push in the right direction, anyone?

R,
SH


for i in zip(array1, array2):
print i

Although I take it you meant four d, the issue with this method is
that once you hit the end of one array the rest of the other one is
ignored.

You could always pre-pad the lists you are using before using the zip
function, kinda like

def pad(*iterables):
max_length = 0
for each_iterable in iterables:
if len(each_iterable) max_length: max_length =
len(each_iterable)
for each_iterable in iterables:
each_iterable.extend([None for i in xrange(0,max_length-
len(each_iterable))])

pad(array1, array2, array3)
for i in zip(array1, array2, array3):
print i

What you could also do is create an index to use for it.

for i in xrange(0, length_of_longest_list):
try: print array1[i]
except IndexError: pass
try: print array2[i]
except IndexError: pass


这篇关于循环循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆