我有一个子串:
a="this is a really big long string which i am going to print substrings from"我想打印它,然后打印a0:-6;a0:-12;a0:-18;依此类推,直到打印的子字符串为空。我如此喜欢python的部分原因是,你通常可以让你的循环看起来比其他语言中的更好,但我想不出一种方法来做到这一点。
发布于 2020-11-13 01:10:19
由于这里的所有答案在核心上都是相等的,让我就所做的步骤给出一些简短的解释。当谈到pythonian式的做事方式时,大多数时候(当然不是完全)指的是list comprehension。在您的例子中,您可以使用它,如下所示:
[a[i] for i in range(0,len(a),1)]您可以遍历字符串中的每个字符。如果您希望始终具有长度为6的子字符串,那么它将是
[a[i:i+6] for i in range(0,len(a)-6,1)]注意:您需要关注字符串len(a)-6`的上限
Since you want your substring to always start at position 0 we fixate it with ```
a[0:...]最后,我们使用i通过-6的步骤从end len(a)转到start 0
a="this is a really big long string which i am going to print substrings from"
[a[0:i] for i in range(len(a),0,-6)]
-->
['this is a really big long string which i am going to print substrings from',
'this is a really big long string which i am going to print substring',
'this is a really big long string which i am going to print sub',
'this is a really big long string which i am going to pri',
'this is a really big long string which i am going ',
'this is a really big long string which i am ',
'this is a really big long string which',
'this is a really big long string',
'this is a really big long ',
'this is a really big',
'this is a real',
'this is ',
'th']现在,它返回子字符串的列表。如果你想直接打印它们,我们使用另一个技巧,那就是
def func(a, b, c):
[...]
l = [a, b, c]
func(*l) == func(a, b, c)我们可以使用asterix *解压一个列表,以便将参数传递给函数,因此我们还可以将列表中的每个子字符串传递给print函数。然而,在默认情况下,打印的分隔符是一个空格,所以我们需要用sep=\n"来改变它
print(*[a[0:i] for i in range(len(a),0,-6)],sep="\n")print(*[a[0:i] for i in range(len(a),0,-6)],sep="\n")
this is a really big long string which i am going to print substrings from
this is a really big long string which i am going to print substring
this is a really big long string which i am going to print sub
this is a really big long string which i am going to pri
this is a really big long string which i am going
this is a really big long string which i am
this is a really big long string which
this is a really big long string
this is a really big long
this is a really big
this is a real
this is
th发布于 2020-11-13 01:10:38
使用for循环:
a = "this is a really big long string which i am going to print substrings from"
for i in range(0, -len(a), -6):
b = a[:i]
print(b)输出:
this is a really big long string which i am going to print substring
this is a really big long string which i am going to print sub
this is a really big long string which i am going to pri
this is a really big long string which i am going
this is a really big long string which i am
this is a really big long string which
this is a really big long string
this is a really big long
this is a really big
this is a real
this is
th下面是一个列表理解方法:
a = "this is a really big long string which i am going to print substrings from"
print('\n'.join(a[:i] for i in range(0, -len(a), -6)))发布于 2020-11-13 01:11:46
试着这样做:
print(*reversed([a[0:x] for x in range(0,len(a)-6,6)]),sep="\n")输出:
this is a really big long string which i am going to print substri
this is a really big long string which i am going to print s
this is a really big long string which i am going to p
this is a really big long string which i am goin
this is a really big long string which i a
this is a really big long string whi
this is a really big long stri
this is a really big lon
this is a really b
this is a re
this ihttps://stackoverflow.com/questions/64808432
复制相似问题