简体   繁体   English

如何并排打印列表中的字符串?

[英]How do I print strings in a list side-by-side?

I have an input that is a list of strings, and I'm not sure how to print every string in the list side-by-side, and add hyphens underneath each string.我有一个字符串列表的输入,我不确定如何并排打印列表中的每个字符串,并在每个字符串下方添加连字符。

Example input:示例输入:

['13 + 11', '12 - 4', '36 + 18']

Desired output:所需的 output:

  13     12      36

+ 11    - 4    + 18

What I've tried:我试过的:

I Tried using a for loop to loop over the strings in the input list, and splitting them up on a space:我尝试使用for循环遍历输入列表中的字符串,并将它们拆分为一个空格:

for i in problems:
    j = i.split(' ')
    print(j)

Output from trying this: Output 从尝试这个:

['13', '+', '11']
['12', '-', '4']
['36', '+', '18']

I've tried combining split and join methods to change the output, but I'm missing a few key pieces of knowledge.我尝试结合splitjoin方法来更改 output,但我缺少一些关键知识。 I'm lost as to how to go about printing the strings side-by-side.我不知道如何并排打印字符串。 I have a feeling it's something to do with including a newline character, but beyond that I'm having trouble.我觉得这与包含换行符有关,但除此之外我遇到了麻烦。

As you discovered, usingsplit will get you something like this:正如您所发现的,使用split会得到这样的结果:

>>> l = ['13 + 11', '12 - 4', '36 + 18']
>>> list(map(str.split, l))
[['13', '+', '11'], ['12', '-', '4'], ['36', '+', '18']]

Now you can use zip() to take index-matching elements:现在您可以使用zip()来获取索引匹配元素:

>>> list(zip(*map(str.split, l)))
[('13', '12', '36'), ('+', '-', '+'), ('11', '4', '18')]

And now just print it in a loop:现在只需循环打印它:

>>> for row in zip(*map(str.split, l)):
...     print(*row, sep='\t')
    
13  12  36
+   -   +
11  4   18

To get something closer to what you asked, you cansplit only once (using the maxsplit argument) and format the output a bit:为了更接近您的要求,您只能split一次(使用maxsplit参数)并稍微格式化 output :

l = ['13 + 11', '12 - 4', '36 + 18']
for row in zip(*[s.split(' ', 1) for s in l]):
    print(*[f"{num:>6}" for num in row])

Will give:会给:

    13     12     36
  + 11    - 4   + 18

to get your desired output, I tried this:为了得到你想要的 output,我试过这个:

join_spaces = '    '
mylist = ['13 + 11', '12 - 4', '36 + 18']
top = []
bottom = []

for each in mylist:
    number1, number2 = each.split(' ', 1)
    bottom.append(number2)
    top.append(number1)

for t in range(len(top)):
    length = " " * (len(bottom[t]) - len(top[t]))
    top[t] = length + top[t]

print(join_spaces.join(top))
print(join_spaces.join(bottom))

not the cleanest method (written quickly)不是最干净的方法(写得很快)

Explanation: for each value in the list, use split(' ', 1) to split at the first occurrence of a space;解释:对于列表中的每个值,使用 split(' ', 1) 在第一次出现空格处进行拆分; this would be the first number (13, 12, 36).这将是第一个数字(13、12、36)。 Then add this to the top list, and append the rest ("+ 11", "- 4", "+ 18") to the bottom list.然后将此添加到顶部列表,并将 append rest(“+ 11”,“- 4”,“+ 18”)添加到底部列表。 Then, for each value in the list top, subtract the length of the bottom ("+ 11", "- 4", "+ 18") from the top (13, 12, 36), and multiply that number by a space.然后,对于列表顶部的每个值,从顶部 (13, 12, 36) 中减去底部的长度 (“+ 11”、“- 4”、“+ 18”),然后将该数字乘以一个空格. Then, add the spaces to the beginning of every value in the top list, and print out the lists as strings (join)然后,将空格添加到顶部列表中每个值的开头,并将列表打印为字符串(连接)

outputs:输出:

  13     12      36
+ 11    - 4    + 18

Assuming your expressions are always of the form "a 'operation' b":假设您的表达式始终采用“a '操作' b”的形式:

problems = ['13 + 11', '12 - 4', '36 + 18']

split_problems = []
for i in problems:
    j = i.split(' ')
    split_problems.append(j)

result = ""

for i in range(len(split_problems)):
    result += split_problems[i][0] + " "

result += '\n'

for i in range(len(split_problems)):
    result += split_problems[i][1] + "" + split_problems[i][2] + " "

print(result)

Solution explained:解决方案说明:

First split the whole thing into parts just like you did, resulting in a array like [['13', '+', '11'], ['12', '-', '4'], ['36', '+', '18']]首先像你一样把整个东西分成几部分,得到一个像[['13', '+', '11'], ['12', '-', '4'], ['36', '+', '18']]

Then loop over the outer elements and print every first entry, or in this case append them to a result string.然后遍历外部元素并打印每个第一个条目,或者在这种情况下 append 将它们打印到结果字符串。 Then loop over the outer elements again and print every 2nd and 3rd element per iteration or append them to a result string.然后再次循环外部元素并在每次迭代或 append 将它们打印到结果字符串中的每个第 2 和第 3 个元素。

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

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