Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/344.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_Python 3.x_For Loop - Fatal编程技术网

Python:同时打印列表中的所有字符串

Python:同时打印列表中的所有字符串,python,python-3.x,for-loop,Python,Python 3.x,For Loop,例如,我有一个列表a。当我在for循环中执行时,我可以打印它的所有元素。但当我在for循环之外执行时,它只打印最后一个。如何在for循环之外打印它们?提前谢谢 代码: 它打印: a is apple b is banana c is cherry ==================== c is cherry 我想要的结果是: a is apple b is banana c is cherry ==================== a is apple b is banana c is

例如,我有一个列表
a
。当我在for循环中执行时,我可以打印它的所有元素。但当我在for循环之外执行时,它只打印最后一个。如何在for循环之外打印它们?提前谢谢

代码:

它打印:

a is apple
b is banana
c is cherry
====================
c is cherry
我想要的结果是:

a is apple
b is banana
c is cherry
====================
a is apple
b is banana
c is cherry

您可以将
打印
*
和换行符分隔符一起使用:

print(*a, sep='\n')
print("====================")
print(*a, sep='\n')
输出:

a is apple
b is banana
c is cherry
====================
a is apple
b is banana
c is cherry
a is apple
b is banana
c is cherry
====================
a is apple
b is banana
c is cherry
print(*a)
相当于
print(a[0],a[1],a[2],…)
。 这会打印出来,中间有一个空白。 用
sep='\n'
覆盖此默认值将改为换行

如果要重用它,请编写自己的小助手函数:

def myprint(a):
    print(*a, sep='\n')

myprint(a)
一行的替代方案,但可以说可读性较差:

print(*(a + ["=" * 20] + a), sep='\n')
输出:

a is apple
b is banana
c is cherry
====================
a is apple
b is banana
c is cherry
a is apple
b is banana
c is cherry
====================
a is apple
b is banana
c is cherry
在Python 2中,使用以下命令“打开”Python-3样式的打印函数:

from __future__ import print_function

您可以将
打印
*
和换行符分隔符一起使用:

print(*a, sep='\n')
print("====================")
print(*a, sep='\n')
输出:

a is apple
b is banana
c is cherry
====================
a is apple
b is banana
c is cherry
a is apple
b is banana
c is cherry
====================
a is apple
b is banana
c is cherry
print(*a)
相当于
print(a[0],a[1],a[2],…)
。 这会打印出来,中间有一个空白。 用
sep='\n'
覆盖此默认值将改为换行

如果要重用它,请编写自己的小助手函数:

def myprint(a):
    print(*a, sep='\n')

myprint(a)
一行的替代方案,但可以说可读性较差:

print(*(a + ["=" * 20] + a), sep='\n')
输出:

a is apple
b is banana
c is cherry
====================
a is apple
b is banana
c is cherry
a is apple
b is banana
c is cherry
====================
a is apple
b is banana
c is cherry
在Python 2中,使用以下命令“打开”Python-3样式的打印函数:

from __future__ import print_function
您可以使用
join()
并按如下方式将列表传递给它:

a=['a is apple','b is banana','c is cherry']
print("\n".join(a))
print("====================")
print("\n".join(a))
输出

a is apple
b is banana
c is cherry
====================
a is apple
b is banana
c is cherry
您可以使用
join()
并按如下方式将列表传递给它:

a=['a is apple','b is banana','c is cherry']
print("\n".join(a))
print("====================")
print("\n".join(a))
输出

a is apple
b is banana
c is cherry
====================
a is apple
b is banana
c is cherry

试试这个。这会给你所需要的

x,y,z=a
print(x,y,z)

试试这个。这会给你所需要的

x,y,z=a
print(x,y,z)

您可以将列表乘以2,并在列表中间添加分隔符,然后使用
join
方法简单地打印列表

l = ['a is apple','b is banana','c is cherry']
l = l*2 
#['a is apple', 'b is banana', 'c is cherry', 'a is apple', 'b is banana', 'c is cherry']
l = l.insert(int(len(l)/2), '====================')
#['a is apple', 'b is banana', 'c is cherry', '====================','a is apple', 'b is banana', 'c is cherry']
print('\n'.join(l))
这将输出:

a is apple
b is banana
c is cherry
====================
a is apple 
b is banana
c is cherry

您可以将列表乘以2,并在列表中间添加分隔符,然后使用
join
方法简单地打印列表

l = ['a is apple','b is banana','c is cherry']
l = l*2 
#['a is apple', 'b is banana', 'c is cherry', 'a is apple', 'b is banana', 'c is cherry']
l = l.insert(int(len(l)/2), '====================')
#['a is apple', 'b is banana', 'c is cherry', '====================','a is apple', 'b is banana', 'c is cherry']
print('\n'.join(l))
这将输出:

a is apple
b is banana
c is cherry
====================
a is apple 
b is banana
c is cherry

这是最简单、最干净的方法(在Python2和Python3中通用):

这是仅适用于Python-3的版本:

print(*(a + ["===================="] + a), sep='\n')

这是最简单、最干净的方法(在Python2和Python3中通用):

这是仅适用于Python-3的版本:

print(*(a + ["===================="] + a), sep='\n')

令人惊叹的我将把这标记为一个答案。只是一个问题,我想将is设置为这样的变量b=(*a,sep='\n'),但它不起作用。谢谢
print()
retunrs
None
并导致副作用。因此,你不能用你想要的方式来绑定它。太棒了!我将把这标记为一个答案。只是一个问题,我想将is设置为这样的变量b=(*a,sep='\n'),但它不起作用。谢谢
print()
retunrs
None
并导致副作用。因此,您不能按您想要的方式绑定它。