简体   繁体   English

如何在python中向左或向右移动东西?

[英]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.

相关问题 如何将二维数组的特定部分向左向右向上或向下移动? - how to move specific part of a 2d array to either left right up or down? python中证明字符串正确的函数(向左,居中或向右) - Function in python that justifies string (to either left, center or right) 如何使用 python 和 openpyxl 将整个 excel 列移动到其当前 position 的左侧或右侧 - How do I move an entire excel column to the left or right of its current position using python and openpyxl 如何在python中渲染\\left(\\right) - How to render \left( \right) in python 将所有元素向右/向左移动/让用户选择(python) - Move all elements to right/left/let user choose (python) 在 python 中从右到左和从左到右 - right both right to left and left to right in python 如何使用 .grid() 或 .pack() 将某些内容与 Tkinter 的左下角对齐? - How do I align something to the bottom left in Tkinter using either .grid() or .pack()? Python重新拆分并将匹配的组附加到拆分的右侧或左侧 - Python re.split and attaching matched group to either right or left side of the split Python - 如何将输出文本移动到左侧/上方? - Python - How to move output text to the left / up? 如何证明参数评估在Python中“从左到右”? - How to prove that parameter evaluation is “left to right” in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM