简体   繁体   English

如何在 Python 中并排打印列表?

[英]How do I print lists side by side in Python?

New user practicing lists and loops.新用户练习列表和循环。 My goal is create two list from my own input (range is 2 for quick testing, but want to get to 10).我的目标是从我自己的输入中创建两个列表(范围为 2 以进行快速测试,但希望达到 10)。 I have hit a wall on two problems:我在两个问题上遇到了困难:

  1. I would like my lists to sit side by side ie.我希望我的清单并排放置,即。

First Name \t\t\t $77名字 \t\t\t $77

Next Name \t\t\t $16下一个名字 \t\t\t $16

  1. I don't know how to change my types so that int or float is not iterable error does not persists.我不知道如何更改我的类型,以便 int 或 float 不可迭代错误不会持续存在。 I believe it'll help with my coded out max/min statements我相信它会帮助我编写出最大/最小语句

My code:我的代码:

list1 = []
list2 = []
count = 0

for i in range(2):
    customers = input("Customers name? ")
    list1.append(customers)
    spent = float(input("Amount spent? "))
    list2.append(spent)
    count += spent
    averageSpent = count / 2 # change this when changing range

print("Name\t\t\tAmount")
# won't print side by side. how to use zip()?
print((*list1 + list2), sep = "\n") 
print("Total spending:\t\t $", count)
print("Average spending:\t $", averageSpent)

# keep getting 'object is not iterable'
#print("Most money spent:\t $", max(spent))
#print("Least money spent:\t $", min(spent))

My output is currently:我的 output 目前是:

Customers name?客户姓名? work工作

Amount spent?花费金额? 45 45

Customers name?客户姓名? please

Amount spent?花费金额? 65 65

Name Amount名称 金额

work工作

please

45.0 45.0

65.0 65.0

Total spending: $ 110.0总支出:$ 110.0

Average spending: $ 55.0平均消费:55.0 美元

Thank you!谢谢!

Printing things side by side is answered here: Print 2 lists side by side并排打印的东西在这里得到回答: 并排打印 2 个列表

The error about a float or int not being iterable is because you are calling max(spent) instead of max(list2) .关于 float 或 int 不可迭代的错误是因为您调用的是max(spent)而不是max(list2) The function max() expects a list or other iterable object. function max()需要一个列表或其他可迭代的 object。

Regarding the first issue that you have, the best way for you to print the output side by side would be to iterate over the list and print each value in the list.关于您遇到的第一个问题,并排打印 output 的最佳方法是遍历列表并打印列表中的每个值。 In addition to that, you can use f-strings which is a feature that we added with python 3.6 and it allows you to do stuff like this:除此之外,您还可以使用 f-strings,这是我们在 python 3.6 中添加的一项功能,它允许您执行以下操作:

x = 10
print(f'{x+20}')
>> 30

you can read more about f-strings here .您可以 在此处阅读有关 f 字符串的更多信息。

Regarding the second issue that you're facing.关于您面临的第二个问题。 You got this error because you were calling the max() function on a single float.您收到此错误是因为您在单个浮点数上调用 max() function。 the max function should be called for a list.应调用最大 function 以获得列表。

I've made some adjustments.我做了一些调整。 Here is what the final code looks like:这是最终代码的样子:

list1 , list2 = [] , []
max_range = 2
count = 0

for i in range(max_range):
    customer_name = input("Customer name: ")
    list1.append(customer_name)

    spent = float(input("Amount Spent: "))
    list2.append(spent)

    count += spent
    averageSpent = count / max_range

print("Name\t\t\tAmount")
for i in range(len(list1)):
    print(f'{list1[i]} \t\t\t$ {list2[i]}')
print("Total spending:\t\t $", count)
print("Average spending:\t $", averageSpent)

print("Most money spent:\t $", max(list2))
print("Least money spent:\t $", min(list2))

Edit: using the zip function might be another option for printing the two outputs side by side using tuples.编辑:使用 zip function 可能是使用元组并排打印两个输出的另一种选择。 However, since you've already said that you're new to these topics I think that you should stay away from zip (for the time being) until you feel comfortable with lists.但是,由于您已经说过您对这些主题不熟悉,我认为您应该远离 zip(暂时),直到您对列表感到满意为止。

bikemule is correct about the iterables. bikemule 关于迭代是正确的。 You need to call max with a list, not a single number.您需要使用列表而不是单个号码来调用max (What would max(101) return? It doesn't quite make sense.) max(101)会返回什么?这不太合理。)

To make the two lists appear side-by-side, you can use zip combined with a for loop.要使两个列表并排显示,您可以将zipfor循环结合使用。 It will turn the lists into sets of tuples that then will appear in an almost-tabular format when printed.它将列表转换为元组集,然后在打印时以几乎表格的格式出现。

list_a = [1, 2, 3]
list_b = [4, 5, 6]
for col_a, col_b in zip(list_a, list_b):
    print("A_Item: %s  |   B_Item: %d" %(col_a, col_b))

Returns:回报:

A_Item: 1  |   B_Item: 4
A_Item: 2  |   B_Item: 5
A_Item: 3  |   B_Item: 6

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

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