Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在一行中打印两个列表的输出?_Python - Fatal编程技术网

Python 如何在一行中打印两个列表的输出?

Python 如何在一行中打印两个列表的输出?,python,Python,我有两个列表A:包含从1到10的数字,B包含百分比值。 我想用这种方式打印出来 1:10%,2:40%,3:50% 我只写了一个数组,但找不到同时写两个数组的方法 print(' : '.join(str(x) for x in A)) 我测试了zip,但它只打印了A的第一个值和B的其余值 print(''.join(str(x) + " : " + str(y) for x, y in zip(A, B))) 0:2821:3422:2963:3024:3155:2496:3067:319

我有两个列表A:包含从1到10的数字,B包含百分比值。 我想用这种方式打印出来

1:10%,2:40%,3:50%

我只写了一个数组,但找不到同时写两个数组的方法

print(' : '.join(str(x) for x in A))
我测试了zip,但它只打印了A的第一个值和B的其余值

print(''.join(str(x) + " : " + str(y) for x, y in zip(A, B)))
0:2821:3422:2963:3024:3155:2496:3067:3198:2729:317

你知道如何在不使用for循环的情况下实现它吗

spam = [1, 2, 3]
eggs = [10, 20, 30]
print(', '.join(f'{n}:{prct}%' for n, prct in zip(spam, eggs)))
如果第一个列表只有数字,那么它可以是偶数

eggs = [10, 20, 30]
print(', '.join(f'{n}:{prct}%' for n, prct in enumerate(eggs, start=1)))
让,, a=[1,2,3] b=[4,5,6] 打印(*a,*b)


我希望它能帮上忙。

您喜欢这项工作吗

A=范围(1,11)
B=[10,40,50,30,70,20,45,80,20,35]
打印(','.join(['{}:{}%'.format(n,p)表示zip(A,B)]中的n,p)
# 1: 10%, 2: 40%, 3: 50%, 4: 30%, 5: 70%, 6: 20%, 7: 45%, 8: 80%, 9: 20%, 10: 35%

您可以像这样做:

idx = [1, 2, 3]
percent = [10,40,50]

print(', '.join(f'{idx[i]}:{percent[i]}%' for i in range(len(idx))))
输出:

1:10%, 2:40%, 3:50%

可以使用for循环:

a = [1, 2, 3]
b = [0.1, 0.2, 0.3] # percentage values

for i, j in zip(a, b):
    print(f'{i}:{j:.0%}', end =', ')
输出:

1:10%, 2:20%, 3:30%,

zip
的帮助下,您的操作是正确的,请尝试下面的代码,我对您的代码做了一些改动:-

a = [1,2,3]
b = [10,40,50]
print(', '.join(str(x) + " : " + str(y)+'%' for x, y in zip(a, b)))
输出

1 : 10%, 2 : 40%, 3 : 50%

发布你的输入列表你应该解释为什么“zip不起作用”@yatu如果“ip不起作用”,那么这也不起作用-而且它也会失去订单。为什么这个问题会被重新打开?尽管zip不起作用,但它仍然是正确的副本。@Sayse但考虑到不断堆积的低质量“答案”,我显然不应该重新打开它,因为op说“zip不起作用”,所以直到我们知道他说的“不起作用”,告诉他使用“zip”从编辑上看,似乎只有一个空格缺失op说“zip不起作用”,所以在我们知道他说的“不起作用”之前,告诉他使用“zip”是没有用的…@Brunodesshuilliers,很明显,“不起作用”是“我不知道如何正确使用它”,从他现在编辑的问题中可以明显看出……@buran你完全是对的。我想布鲁诺·德斯韦利尔斯应该冷静一点。很抱歉,我认为我没有正确阅读这个问题。谢谢你告诉我。