首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通过子字符串进行类型化循环

通过子字符串进行类型化循环
EN

Stack Overflow用户
提问于 2020-11-13 01:05:40
回答 3查看 43关注 0票数 0

我有一个子串:

代码语言:javascript
复制
a="this is a really big long string which i am going to print substrings from"

我想打印它,然后打印a0:-6;a0:-12;a0:-18;依此类推,直到打印的子字符串为空。我如此喜欢python的部分原因是,你通常可以让你的循环看起来比其他语言中的更好,但我想不出一种方法来做到这一点。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2020-11-13 01:10:19

由于这里的所有答案在核心上都是相等的,让我就所做的步骤给出一些简短的解释。当谈到pythonian式的做事方式时,大多数时候(当然不是完全)指的是list comprehension。在您的例子中,您可以使用它,如下所示:

代码语言:javascript
复制
[a[i] for i in range(0,len(a),1)]

您可以遍历字符串中的每个字符。如果您希望始终具有长度为6的子字符串,那么它将是

代码语言:javascript
复制
[a[i:i+6] for i in range(0,len(a)-6,1)]

注意:您需要关注字符串len(a)-6`的上限

代码语言:javascript
复制
Since you want your substring to always start at position 0 we fixate it with ```
a[0:...]

最后,我们使用i通过-6的步骤从end len(a)转到start 0

代码语言:javascript
复制
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']

现在,它返回子字符串的列表。如果你想直接打印它们,我们使用另一个技巧,那就是

代码语言:javascript
复制
def func(a, b, c):
  [...]

l = [a, b, c]

func(*l) == func(a, b, c)

我们可以使用asterix *解压一个列表,以便将参数传递给函数,因此我们还可以将列表中的每个子字符串传递给print函数。然而,在默认情况下,打印的分隔符是一个空格,所以我们需要用sep=\n"来改变它

代码语言:javascript
复制
print(*[a[0:i] for i in range(len(a),0,-6)],sep="\n")
代码语言:javascript
复制
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
票数 0
EN

Stack Overflow用户

发布于 2020-11-13 01:10:38

使用for循环:

代码语言:javascript
复制
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)

输出:

代码语言:javascript
复制
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

下面是一个列表理解方法:

代码语言:javascript
复制
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)))
票数 0
EN

Stack Overflow用户

发布于 2020-11-13 01:11:46

试着这样做:

代码语言:javascript
复制
print(*reversed([a[0:x] for x in range(0,len(a)-6,6)]),sep="\n")

输出:

代码语言:javascript
复制
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 i
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64808432

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档