“for循环”有两个变量? [英] "for loop" with two variables?

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

问题描述

 <$ c $ 如何在同一个中包含两个变量? c> t1 = [整数,字符串和列表] 
t2 = [整数,字符串和列表的另一个列表]

def f(t):#a将读取的函数列出t1和t2并返回所有在范围(len(t1))和范围(len(t2))中为i的相同
的元素:
...


解决方案

如果你想要嵌套for循环的效果,使用:

  import itertools 
for itertools.product(range(x),range(y))中的i,j:
#Stuff ...

如果您只想同时循环, ($)
$ b $ pre $ $ $ $ $ $ $ $ $ $ $

请注意,如果 x y 长度不一样, zip 会截短到最短的列表。 @abarnert指出,如果你不想截短到最短的列表,你可以使用 itertools.zip_longest

UPDATE



基于将读取列表t1和t2的函数并返回所有元素这是相同的,我不认为该OP需要 zip 产品。我认为他们想要一个 set

  def equal_elements(t1,t2 ):
return list(set(t1).intersection(set(t2)))
#你也可以做
#return list(set(t1)& set(t2))



交点方法 set 将会返回所有通用的元素和另一个集合(注意,如果你的列表包含其他的 list s,你可能想要首先将内部的 list s转换为元组,以使它们可以被哈希;否则调用设置将失败。)。然后 list 函数将设置变回列表。



更新2



或者,OP可能需要在列表中的相同位置中相同的元素。在这种情况下, zip 将是最合适的,它被截断到最短列表的事实是你想要的(因为不可能有相同的元素在索引9时,其中一个列表只有5个元素长)。如果这是你想要的,去这个:

  def equal_elements(t1,t2):
return [x如果x == y]

这将返回一个x只包含列表中相同位置的元素。


How can I include two variables in the same for loop?

t1 = [a list of integers, strings and lists]
t2 = [another list of integers, strings and lists]

def f(t):  #a function that will read lists "t1" and "t2" and return all elements that are identical
    for i in range(len(t1)) and for j in range(len(t2)):
        ...

解决方案

If you want the effect of a nested for loop, use:

import itertools
for i, j in itertools.product(range(x), range(y)):
    # Stuff...

If you just want to loop simultaneously, use:

for i, j in zip(range(x), range(y)):
    # Stuff...

Note that if x and y are not the same length, zip will truncate to the shortest list. As @abarnert pointed out, if you don't want to truncate to the shortest list, you could use itertools.zip_longest.

UPDATE

Based on the request for "a function that will read lists "t1" and "t2" and return all elements that are identical", I don't think the OP wants zip or product. I think they want a set:

def equal_elements(t1, t2):
    return list(set(t1).intersection(set(t2)))
    # You could also do
    # return list(set(t1) & set(t2))

The intersection method of a set will return all the elements common to it and another set (Note that if your lists contains other lists, you might want to convert the inner lists to tuples first so that they are hashable; otherwise the call to set will fail.). The list function then turns the set back into a list.

UPDATE 2

OR, the OP might want elements that are identical in the same position in the lists. In this case, zip would be most appropriate, and the fact that it truncates to the shortest list is what you would want (since it is impossible for there to be the same element at index 9 when one of the lists is only 5 elements long). If that is what you want, go with this:

def equal_elements(t1, t2):
    return [x for x, y in zip(t1, t2) if x == y]

This will return a list containing only the elements that are the same and in the same position in the lists.

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

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