【问题标题】:how to get a list of non repeating substring from a string?如何从字符串中获取非重复子字符串的列表?
【发布时间】:2021-07-20 16:51:12
【问题描述】:
s="abcabcabc"
S=list(s)
li=[]
l=[]
for i in S:
    if i not in l:
       l.append(i)
    else:
        li.append(l)
        l=[]
        l.append(i)
print(li)

输出是

[['a', 'b', 'c'], ['a', 'b', 'c']]

只得到两个子字符串而不是三个

我想要的输出是

[['a','b','c'],['a','b','c'],['a','b','c']]

【问题讨论】:

  • 预期输出是什么?你是说[['a', 'b'], ['a', 'b']] 吗?
  • 我的意思是 [['a','b','c'],['a','b','c',],['a','b',' c']]

标签: arrays non-repetitive


【解决方案1】:

循环结束后,你还必须检查 l 是否为空。如果它不为空,那么您必须将其附加到最终答案中。

所以,你的循环后代码应该是,

if len(l) > 0:
    li.append(l)

print(li)

【讨论】:

    猜你喜欢
    • 2015-11-04
    • 1970-01-01
    • 2014-07-01
    • 2012-01-12
    • 1970-01-01
    • 2013-08-12
    • 1970-01-01
    • 1970-01-01
    • 2019-03-16
    相关资源
    最近更新 更多