Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.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 - Fatal编程技术网

如何一次使用列表中的多个项-python

如何一次使用列表中的多个项-python,python,python-3.x,Python,Python 3.x,我有一份清单: mylist = [ "hello", "my", "name", "is", "frank", "and", "it", "is", "great", "to", "be", "here", "in", "this", "great", "country" ] 我正在使用 for i in range(0,len(mylist)): print(mylist[i]) 打印出每个项目,它的工作原理与打印出新行中的每个单词一样 我想做的是通过更改变量k的值,将列表中的

我有一份清单:

mylist = [
"hello", "my", "name", "is", "frank", "and", "it", "is", "great", "to", "be", "here", "in", "this", "great", "country"

]
我正在使用

for i in range(0,len(mylist)):
    print(mylist[i])
打印出每个项目,它的工作原理与打印出新行中的每个单词一样

我想做的是通过更改变量
k
的值,将列表中的多个单词视为一个单词:

i、 我想用它来代替输出

hello                                                                                                                   
my                                                                                                                      
name                                                                                                                    
is                                                                                                                      
frank                                                                                                                   
and                                                                                                                     
it                                                                                                                      
is                                                                                                                      
great                                                                                                                   
to                                                                                                                      
be                                                                                                                      
here                                                                                                                    
in                                                                                                                      
this                                                                                                                    
great                                                                                                                   
country 
如果
k=2
,我想输出

hello my                                                                                                                      
name is                                                                                                                      
frank and                                                                                                                     
it is                                                                                                                      
great to                                                                                                                      
be here                                                                                                                    
in this                                                                                                                    
great country 
如果列表中的项目数为奇数,则最后一个单词应单独排在一行中

如果
k=3
,那么一行中应该有3个单词

有没有办法做到这一点

提前感谢

您可以使用:

然后使用以下参数的
sep
迭代或使用序列解包:

您可以使用:

然后使用以下参数的
sep
迭代或使用序列解包:

def chunks(L, n):
    """Yield successive n-sized chunks from L."""
    for i in range(0, len(L), n):
        yield L[i:i + n]
for i in chunks(mylist, 2):
    print(' '.join(i))
    # alternatively, unpack and use sep argument:
    # print(*i, sep=' ')

# hello my
# name is
# frank and
# it is
# great to
# be here
# in this
# great country