python - 打印两个列表中的值对

标签 python python-3.x list for-loop printing

在这个程序中,除了一件事之外,一切都对我有用;我需要输出将名称和值分组到 print(names, "\n"value) 部分中。

例如:Pat:99 欧元,Kev:55 欧元,Dermot:100 欧元

我的代码如下:

salesperson = int(input("How many salesperson's do you wish to account 
for:"))
names = []
values = []
counter = 0
total_sales = 0
total_value = 0
if salesperson > counter:
    while salesperson != counter:
        name = input("Please enter in the salesperson's name:")
        value = float(input("Please enter in the value of sales for the 
        salesperson:"))
        salesperson -= 1
        names.append(name)
        values.append(value)
        total_value += value
        from statistics import mean
        average_values = mean(values)
        minimum = min(values)
        maximum = max(values)
    print(names,"\n",values)
    print(average_values)
    print(total_value)
    print(minimum)
    print(maximum)

最佳答案

您可以使用 zip() 来执行此操作,和f-strings :

print([f"{x}: £{y}" for x, y in zip(names,values)])

输出:

['Pat: £1.0', 'Dermot: £2.0', 'Kev: £3.0', 'Boris: £4.0', 'Jeremy: £5.0']

关于python - 打印两个列表中的值对,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59268711/

相关文章:

python-3.x - 如何在运行 pytest 测试之前安装 python 插件?

python - 将 "None"替换为返回 NameError 的对象

xml - Odoo 路由给出 "Internal Server Error"错误页面

list - Pythonic方法返回更大列表中每个第n个项目的列表

python - 如何在Pytorch中形成一个连续的数字序列?

python - 如果满足特定条件,则将字符串与列表列表连接起来

python - 使用正则表达式查找子字符串

python - 在将数据传递给序列化器 django api 时添加额外的上下文

python - 关于 python 列表初始化的有趣的事情

python - 使用列表扩展 Pandas 列