How to slice a list from an element n to the end in python?

前端 未结 7 1765
[愿得一人]
[愿得一人] 2020-12-29 01:24

I\'m having some trouble figuring out how to slice python lists, it is illustrated as follows:

>>> test = range(10)
>>> test
[0, 1, 2, 3, 4         


        
相关标签:
7条回答
  • 2020-12-29 01:50

    What you are looking for is to use something like:

    inputs = "ababbbaAab"
    for i in range(n):
        print(i, inputs[:i] + inputs[i:i+1])
    

    The output:

    0 a
    1 ab
    2 aba
    3 abab
    4 ababb
    5 ababbb
    6 ababbba
    7 ababbbaA
    8 ababbbaAa
    9 ababbbaAab
    

    See that if i == 0

    then inputs[:i] == [] and inputs[i:i+1] == a

    and if i == len(inputs) - 1

    then inputs[:i] == [ababbbaAa] and inputs[i:i+1] == b

    0 讨论(0)
提交回复
热议问题