[英]How to move something to either left or right in python?
Having a problem, lets say I have this code (easiest possible): 有问题,可以说我有以下代码(最简单的方法):
n=5
j=3
for i in range(n):
print("kurt")
for x in range(j):
print("karl")
If I want this to be printed like a protocol, that is, I don't want them after each other, I want my second for-loop to be to the right of the first for-loop when I print it, like this: 如果我想像协议那样打印,也就是说,我不希望它们彼此紧挨着打印,那么我希望我的第二个for循环在打印时位于第一个for循环的右侧,如下所示:
kurt karl
kurt karl
kurt karl
kurt
kurt
Sure, just put them in one print statement. 当然,只需将它们放在一个打印语句中即可。
Here I've told it to loop for the maximum number (in this case 5
) and then if the loop has reached the minimum number ( 3
) to stop printing the second one. 在这里,我告诉它循环播放最大数量(在这种情况下为5
),然后如果循环达到最小数量( 3
)以停止打印第二个。
n = 5
j = 3
for i in range (max(n,j)):
if i < min(n,j):
print("kurt" + " " + "karl")
else:
print("kurt")
As I understood, you want to print the values of next loop in right of previous loop, you can achieve it by writing this: 据我了解,您想在上一个循环的右边打印下一个循环的值,可以通过编写以下代码来实现:
In python 2.7 在python 2.7中
for i, k in zip(range(n), range(j)):
print "kurt",
print "karl"
In python 3.x 在python 3.x中
for i, k in zip(range(n), range(j)):
print("kurt", end=" ")
print("karl")
from itertools import izip_longest
first = ['kurt']*j
second = ['karl']*n
for l,r in izip_longest(first ,second ):
print l,r
output: 输出:
kurt karl
kurt karl
kurt karl
None karl
None karl
if you want white space. 如果要空白。
from itertools import izip_longest
first = ['kurt']*j
second = ['karl']*n
spaceJ,spaceN = len(first [0]),len(second [0])
for l,r in izip_longest(first ,second ):
print l if l else ' '*spaceJ,
print r if r else ' '*spaceN
output 输出
kurt karl
kurt karl
kurt karl
karl
karl
Assuming that this printing of "kurt" and "karl" is just an example of a more complex problem you're trying to solve, a general strategy for this sort of problem is to use Python generators to run the two loops concurrently, and then combine the results for output. 假设打印“ kurt”和“ karl”只是您要解决的更复杂问题的一个示例,则此类问题的一般策略是使用Python 生成器同时运行两个循环,然后合并结果以输出。
For example: 例如:
from itertools import zip_longest
n = 5
j = 3
def kurt():
for i in range(n):
yield "kurt"
def karl():
for x in range(j):
yield "karl"
for kurt_result, karl_result in zip_longest(kurt(), karl(), fillvalue=""):
print(kurt_result, karl_result)
When a function body contains the yield
keyword, Python produces a generator rather than a regular function. 当函数主体包含yield
关键字时,Python会生成生成器,而不是常规函数。 A generator will, when called, return an iterator that produces a value each time yield
is called, and then waits until the next value is read before proceeding to the next yield
. 生成器在被调用时将返回一个迭代器,该迭代器每次调用yield
都会生成一个值,然后等待读取下一个值,然后再进行下一个yield
。 Once the generator function returns, the iteration ends. 生成器函数返回后,迭代结束。
The final for
loop here then uses zip_longest
, which is a utility function that takes two or more iterators and produces another iterator that yields the sequence of combinations of reading from all of the given iterators at once, allowing you to easily match up the corresponding results from each iterator. 然后,这里的最后一个for
循环使用zip_longest
,这是一个实用程序函数,它需要两个或多个迭代器,并生成另一个迭代器,该迭代器可立即产生从所有给定迭代器读取的组合序列,从而使您可以轻松地匹配相应的结果来自每个迭代器。
This code then produces the following output: 然后,此代码产生以下输出:
kurt karl
kurt karl
kurt karl
kurt
kurt
(The above assumes Python 3. If you were using Python 2, the zip_longest
function was then named izip_longest
, so you'd need to adjust the two references.) (以上假设使用zip_longest
。如果您使用的是Python 2,则zip_longest
函数名为izip_longest
,因此您需要调整两个引用。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.