Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python,在每个循环中交换变量_Python_Python 3.x_Swap - Fatal编程技术网

Python,在每个循环中交换变量

Python,在每个循环中交换变量,python,python-3.x,swap,Python,Python 3.x,Swap,我有一部分代码如下所示: for i in [0,1]: ... print('{} used {} words that {} did not use.'.format(a[i], 50 , a[i+1])) a = ['first', 'second'] x, y = a print('{} used {} words that {} did not use.'.format(x, 50 , y)) print('{} used {} words that {} did n

我有一部分代码如下所示:

for i in [0,1]:
    ...
    print('{} used {} words that {} did not use.'.format(a[i], 50 , a[i+1]))
a = ['first', 'second']
x, y = a
print('{} used {} words that {} did not use.'.format(x, 50 , y))
print('{} used {} words that {} did not use.'.format(y, 50 , x))
对于第一次迭代,我希望它

print('{} used {} words that {} did not use.'.format(a[0], 50 , a[1]))
但在第二次迭代中,我希望:

print('{} used {} words that {} did not use.'.format(a[1], 50 , a[0]))

如何做到这一点?

如果这正是您想要的,您可以:

for i in [0,1]:
    ...
    print('{} used {} words that {} did not use.'.format(a[i], 50 , a[(i+1)%2]))

如果这正是你想要的,你可以:

for i in [0,1]:
    ...
    print('{} used {} words that {} did not use.'.format(a[i], 50 , a[(i+1)%2]))

您可以使用指示符模2(
%2
):

输出: 或者,如果只有两个项目: 这样做可能更容易阅读和维护:

for i in [0,1]:
    ...
    print('{} used {} words that {} did not use.'.format(a[i], 50 , a[i+1]))
a = ['first', 'second']
x, y = a
print('{} used {} words that {} did not use.'.format(x, 50 , y))
print('{} used {} words that {} did not use.'.format(y, 50 , x))

您可以使用指示符模2(
%2
):

输出: 或者,如果只有两个项目: 这样做可能更容易阅读和维护:

for i in [0,1]:
    ...
    print('{} used {} words that {} did not use.'.format(a[i], 50 , a[i+1]))
a = ['first', 'second']
x, y = a
print('{} used {} words that {} did not use.'.format(x, 50 , y))
print('{} used {} words that {} did not use.'.format(y, 50 , x))

您可以使用模数运算符
%

for i in [0,1]:
    ...
    print('{} used {} words that {} did not use.'.format(a[i % 2], 50 , a[(i + 1) % 2]))
在第一次迭代中,
i=0

i % 2       == 0 % 2 == 0
(i + 1) % 2 == 1 % 2 == 1
在第二次迭代中,
i=1

i % 2       == 1 % 2 == 1
(i + 1) % 2 == 2 % 2 == 0

请注意,第一个
i%2==i
用于您的问题的这个特定实例。

您可以使用模运算符
%

for i in [0,1]:
    ...
    print('{} used {} words that {} did not use.'.format(a[i % 2], 50 , a[(i + 1) % 2]))
在第一次迭代中,
i=0

i % 2       == 0 % 2 == 0
(i + 1) % 2 == 1 % 2 == 1
在第二次迭代中,
i=1

i % 2       == 1 % 2 == 1
(i + 1) % 2 == 2 % 2 == 0
请注意,对于您的问题的这个特定实例,第一个
i%2==i

这是我的解决方案:

a = ['John','Doe']
amount = 50

# Use index to create strings to be formatted
s1 = '{0} used {2} words that {1} did not use.'
s2 = '{1} used {2} words that {0} did not use.'

print(s1.format(*a,amount))
print(s2.format(*a,amount))
返回:

John used 50 words that Doe did not use.
Doe used 50 words that John did not use.
John used 50 words that Doe did not use.
Doe used 50 words that John did not use.
或:

返回:

John used 50 words that Doe did not use.
Doe used 50 words that John did not use.
John used 50 words that Doe did not use.
Doe used 50 words that John did not use.
这是我的解决方案:

a = ['John','Doe']
amount = 50

# Use index to create strings to be formatted
s1 = '{0} used {2} words that {1} did not use.'
s2 = '{1} used {2} words that {0} did not use.'

print(s1.format(*a,amount))
print(s2.format(*a,amount))
返回:

John used 50 words that Doe did not use.
Doe used 50 words that John did not use.
John used 50 words that Doe did not use.
Doe used 50 words that John did not use.
或:

返回:

John used 50 words that Doe did not use.
Doe used 50 words that John did not use.
John used 50 words that Doe did not use.
Doe used 50 words that John did not use.

1-i
是一个选项。
(i+1)%2
是另一个
i^1
也有效。我认为这里的问题并不清楚。理想的解决方案应该如何推广到更长的循环,例如迭代
[0,1,2,3,4]
?或者,Eric,
[0,1]
是您想要处理的唯一案例吗?如果是这样的话,为什么不写两个
print()
语句并完成它呢?我想,如果只有两个项目,那么为了可读性,就删除循环。
1-I
是一个选项。
(I+1)%2
是另一个
I^1
也有效。我不认为这里问的问题真的很清楚。理想的解决方案应该如何推广到更长的循环,例如迭代
[0,1,2,3,4]
?或者,Eric,
[0,1]
是您想要处理的唯一案例吗?如果是这样的话,为什么不写两个
print()
语句并完成它呢?我想说,如果只有两个项,那么为了可读性,就删除循环。