简体   繁体   English

为什么我的列表不能并排打印?

[英]Why doesn't my list print side by side?

I'm writing a program where I want the dice to print side by side. 我正在写一个程序,我希望骰子并排打印。 However, the actual output only places the top base of the second die next to the bottom base of the first die. 然而,实际输出仅将第二管芯的顶部基座放置在第一管芯的底部基座旁边。 Can anyone help me understand why this is happening? 任何人都可以帮助我理解为什么会这样吗?

die1 = ('+-------+\n|       |\n|   *   |\n|       |\n+-------+')
die2 = ('+-------+\n| *     |\n|       |\n|     * |\n+-------+')
die3 = ('+-------+\n| *     |\n|   *   |\n|     * |\n+-------+')
die4 = ('+-------+\n| *   * |\n|       |\n| *   * |\n+-------+')
die5 = ('+-------+\n| *   * |\n|   *   |\n| *   * |\n+-------+')
die6 = ('+-------+\n| * * * |\n|       |\n| * * * |\n+-------+')

list_die = ['0', die1, die2, die3, die4, die5, die6]

x = list_die[1]+'   ' + list_die[2]
print(x)

my output
+-------+
|       |
|   *   |
|       |
+-------+   +-------+
| *     |
|       |
|     * |
+-------+

Try using zip and lists 尝试使用zip和列表

To have them print side by side you will need to concat them with zip 要让它们并排打印,您需要用拉链连接它们

die1 = ('+-------+','|       |','|   *   |','|       |','+-------+')
die2 = ('+-------+','| *     |','|       |','|     * |','+-------+')
die3 = ('+-------+','| *     |','|   *   |','|     * |','+-------+')
die4 = ('+-------+','| *   * |','|       |','| *   * |','+-------+')
die5 = ('+-------+','| *   * |','|   *   |','| *   * |','+-------+')
die6 = ('+-------+','| * * * |','|       |','| * * * |','+-------+')

list_die = ['0',die1,die2,die3,die4,die5,die6];

die_a = list_die[1];
die_b = list_die[2];

for line in zip(die_a,die_b):
    print line;

output:
('+-------+', '+-------+')
('|       |', '| *     |')
('|   *   |', '|       |')
('|       |', '|     * |')
('+-------+', '+-------+')

Take apart to get the individual lines, concatenate those lines, and then rejoin. 拆开以获取各行,连接这些行,然后重新加入。 Note that the final result has fewer \\n than a simple concatenation of the original strings: 请注意,最终结果比原始字符串的简单连接更少\\n

def pair(i,j):
    linesI = list_die[i].split('\n')
    linesJ = list_die[j].split('\n')
    return '\n'.join(x+y for x,y in zip(linesI,linesJ))

For example, 例如,

>>> print(pair(1,2))
+-------++-------+
|       || *     |
|   *   ||       |
|       ||     * |
+-------++-------+

On Edit; 在编辑; It is inefficient to repeatedly split the same strings again and again. 反复重复分割相同的字符串是低效的。 Keeping your original definitions, you could use the following lines: 保留原始定义,您可以使用以下行:

list_die = ['0',die1,die2,die3,die4,die5,die6]
list_die = [d.split('\n') for d in list_die]

Now list_die contains the splits. 现在list_die包含拆分。

You could now simply use: 你现在可以简单地使用:

def pair(i,j):
    return '\n'.join(x+y for x,y in zip(list_die[i],list_die[j]))

Which is functionally the same as above. 其功能与上述相同。

At the risk of being a little too cute, you can make a more flexible version which will print any number of dice on a single line (useful for eg coding the game of Risk or Yahtzee, with often involve three or more dice): 冒着有点太可爱的风险,你可以制作一个更灵活的版本,可以在一行上打印任意数量的骰子(例如编码风险游戏或Yahtzee,通常涉及三个或更多骰子):

def dice(*args):
    return '\n'.join(''.join(t) for t in zip(*(list_die[i] for i in args)))

So, for example: 所以,例如:

>>> print(dice(1))
+-------+
|       |
|   *   |
|       |
+-------+
>>> print(dice(1,2))
+-------++-------+
|       || *     |
|   *   ||       |
|       ||     * |
+-------++-------+
>>> print(dice(3,1,4))
+-------++-------++-------+
| *     ||       || *   * |
|   *   ||   *   ||       |
|     * ||       || *   * |
+-------++-------++-------+

On Further Edit: You can do this without zip , although at the cost of making the code longer. 进一步编辑:您可以在没有zip情况下执行此操作,但代价是使代码更长。 zip is designed for parallel iteration . zip是为并行迭代而设计的。 For example, you assemble the pair of side-by-side dice but iterating down the two dice face in parallel. 例如,你组装一对并排的骰子,但是平行地向下迭代两个骰子面。 Without using zip , you would need to use list indices to keep the iteration parallel. 不使用zip ,您需要使用列表索引来保持迭代并行。 For example, the second version of pair() (where list_die contains the dice faces already split into lines) would look like: 例如, pair()的第二个版本(其中list_die包含已经分割list_die的骰子面)将如下所示:

def pair(i,j):
    lines = []
    for k in range(len(list_die[i])):
        lines.append(list_die[i][k] + list_die[j][k])
    return '\n'.join(lines)

This works, but is over 200% longer (5 lines rather than 2). 这有效,但超过200%(5行而不是2行)。

To answer the question of why this behavior happens, note that your calls to print and explicit \\n s insert a newline in the text, which forces the output to go down a line. 要回答为什么这种行为发生的问题,请注意您调用print和明确的\\n s插入文本,强制输出往下走线换行。

Unless you use code that manipulates the console, once you go down a line, you can't go back up. 除非你使用操作控制台的代码,否则一旦你走了一条线,你就无法恢复。 You print left to right, top to down, like a printer. 您可以像打印机一样从左到右,从上到下打印。 You can't unprint. 你无法打印。

If you want this functionality, look into graphics libraries . 如果您需要此功能,请查看图形库。 Curses is the easy goto library for C++, and may have a python equivalent. Curses是C ++的简易goto库,可能有python等价物。

Of course, as the other answers mention, you can just format the text side-by-side, which for you current purpose is probably the better option. 当然,正如其他答案所提到的,您可以将文本并排格式化,这对您来说当前目的可能是更好的选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM