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_List - Fatal编程技术网

我有一个字符串形式的python列表,我想按间隔和顺序打印出来

我有一个字符串形式的python列表,我想按间隔和顺序打印出来,python,python-3.x,list,Python,Python 3.x,List,我有一个python列表,我想按顺序打印它们,并在它们之间隔开 x = "hello world" >>> x[2:10] # this prints from 'l' to 'd' allway >>> x[2:10:2] # this prints from 'l' to 'd' in the order of two, 'lowr' 我怎样才能像“lowr”一样打印,在它们之间放置一个空格字符而不使用循环?所以它看起来像“l-o-w-r”只要用一

我有一个python列表,我想按顺序打印它们,并在它们之间隔开

x = "hello world"
>>> x[2:10]  # this prints from 'l' to 'd' allway

>>> x[2:10:2]  # this prints from 'l' to 'd' in the order of two,
'lowr'

我怎样才能像“lowr”一样打印,在它们之间放置一个空格字符而不使用循环?所以它看起来像“l-o-w-r”

只要用一个空格或一个-,将列表中的字符连接起来就可以了。它使用一个字符串,该字符串通过给定的分隔符将列表中的元素缝合在一起

x = "hello world"
print(' '.join(x[2:10:2]))
#l o w r
print('-'.join(x[2:10:2]))
#l-o-w-r

只要用一个空格或一个-,将列表中的字符连接起来即可。它使用一个字符串,该字符串通过给定的分隔符将列表中的元素缝合在一起

x = "hello world"
print(' '.join(x[2:10:2]))
#l o w r
print('-'.join(x[2:10:2]))
#l-o-w-r

字符串有一种方法,它可以将迭代中的元素与中间给出的字符串结合在一起。比如说

>>>x=‘你好,世界’ >>>sep=''分隔符是一个空格 >>>9月joinx[2:10:2] “l o w r”

字符串有一种方法,它可以将迭代中的元素与中间给出的字符串结合在一起。比如说

>>>x=‘你好,世界’ >>>sep=''分隔符是一个空格 >>>9月joinx[2:10:2] “l o w r”
不确定没有循环是什么意思,但可以执行以下操作:

x=你好,世界 ''x中y的联合[2:10:2]
不确定没有循环是什么意思,但可以执行以下操作:

x=你好,世界 ''x中y的联合[2:10:2] 查看Python的函数。它可以让你加入任何iterable,同时在.join中的引号之间插入任何内容

像这样:

x = "hello world"

" ".join(x[2:10:2])
查看Python的函数。它可以让你加入任何iterable,同时在.join中的引号之间插入任何内容

像这样:

x = "hello world"

" ".join(x[2:10:2])

“-”.连接[2:10:2]”.连接[2:10:2]”-“.连接[2:10:2]”.连接[2:10:2]