python loop_python - “for loop”有两个变量?

python - “for loop”有两个变量?

如何在同一个for循环中包含两个变量?

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)):

...

8个解决方案

134 votes

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

import itertools

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

# Stuff...

如果您只想同时循环,请使用:

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

# Stuff...

请注意,如果zip和set的长度不同,则list将截断为最短列表。 正如@abarnert所指出的,如果你不想截断到最短的列表,你可以使用list。

UPDATE

基于“将读取列表的函数”t1“和”t2“的请求并返回所有相同的元素”,我不认为OP想要zip或set.我认为他们想要list:

def equal_elements(t1, t2):

return list(set(t1).intersection(set(t2)))

# You could also do

# return list(set(t1) & set(t2))

zip的zip方法将返回它共有的所有元素和另一组(请注意,如果您的列表包含其他list,您可能希望首先将内部lists转换为tuples以便它们可以清洗;否则调用set将 失败。)。 list函数然后将该集合重新转换为列表。

更新2

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

def equal_elements(t1, t2):

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

这将返回一个列表,其中仅包含列表中相同且位置相同的元素。

SethMMorton answered 2019-05-17T01:51:05Z

18 votes

这里有两个可能的问题:如何同时迭代这些变量,或者如何循环它们的组合。

幸运的是,两者都有简单的答案。 第一种情况,你想使用zip。

x = [1, 2, 3]

y = [4, 5, 6]

for i, j in zip(x, y):

print i + " / " + j

将输出

1 / 4

2 / 5

3 / 6

请记住,您可以在zip中放置任何可迭代的内容,因此您可以轻松地编写类似于以下内容的示例:

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

# do work here.

实际上,只是意识到这是行不通的。 它只会迭代,直到较小的范围用完。 在这种情况下,听起来你想迭代循环组合。

在另一种情况下,您只需要一个嵌套循环。

for i in x:

for j in y:

print i + " / " + j

给你

1 / 4

1 / 5

1 / 6

2 / 4

2 / 5

...

您也可以将此作为列表理解。

[i + " / " + j for i in range(x) for j in range(y)]

希望有所帮助。

Morgan Harris answered 2019-05-17T01:52:10Z

17 votes

你有什么理由不能使用嵌套的for循环吗?

for i in range(x):

for j in range(y):

#code that uses i and j

Jags answered 2019-05-17T01:52:36Z

11 votes

for (i,j) in [(i,j) for i in range(x) for j in range(y)]

应该这样做。

qaphla answered 2019-05-17T01:52:57Z

7 votes

如果你真的只是在一个范围内进行锁步迭代,你可以通过以下几种方式之一进行:

for i in range(x):

j = i

# or

for i, j in enumerate(range(x)):

# or

for i, j in ((i,i) for i in range(x)):

以上所有相当于x,如果y。

如果你想要一个嵌套循环并且你只有两个迭代,那么只需使用一个嵌套循环:

for i in range(x):

for i in range(y):

如果您有两个以上的iterables,请使用x。

最后,如果您希望锁定步骤迭代达到x然后继续到y,则必须确定x值的其余部分应该是什么。

for i, j in itertools.zip_longest(range(x), range(y), fillvalue=float('nan')):

# or

for i in range(min(x,y)):

j = i

for i in range(min(x,y), max(x,y)):

j = float('nan')

kojiro answered 2019-05-17T01:53:52Z

3 votes

我认为你正在寻找嵌套循环。

示例(基于您的编辑):

t1=[1,2,'Hello',(1,2),999,1.23]

t2=[1,'Hello',(1,2),999]

t3=[]

for it1, e1 in enumerate(t1):

for it2, e2 in enumerate(t2):

if e1==e2:

t3.append((it1,it2,e1))

# t3=[(0, 0, 1), (2, 1, 'Hello'), (3, 2, (1, 2)), (4, 3, 999)]

这可以简化为单一理解:

[(it1,it2,e1) for it1, e1 in enumerate(t1) for it2, e2 in enumerate(t2) if e1==e2]

但要找到共同的元素,你可以这样做:

print set(t1) & set(t2)

# set([(1, 2), 1, 'Hello', 999])

如果您的列表包含不可清除的对象(如其他列表,dicts),请使用冻结集:

from collections import Iterable

s1=set(frozenset(e1) if isinstance(e1,Iterable) else e1 for e1 in t1)

s2=set(frozenset(e2) if isinstance(e2,Iterable) else e2 for e2 in t2)

print s1 & s2

dawg answered 2019-05-17T01:54:45Z

3 votes

“Python 3.”

使用zip和range添加2个带for循环的变量; 返回列表。

注意:只会运行到最小范围结束。

>>>a=[g+h for g,h in zip(range(10), range(10))]

>>>a

>>>[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]

Open-Business answered 2019-05-17T01:55:25Z

2 votes

对于您的用例,使用while循环可能更容易。

t1 = [137, 42]

t2 = ["Hello", "world"]

i = 0

j = 0

while i < len(t1) and j < len(t2):

print t1[i], t2[j]

i += 1

j += 1

# 137 Hello

# 42 world

作为一个警告,这种方法将截断到最短列表的长度。

Daniel answered 2019-05-17T01:55:59Z

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值