Home > Blockchain >  Subwords of a string in Python
Subwords of a string in Python

Time:01-04

I am trying to create a list of every possible version of a string in a fast way. I don't really mean specifically subwords - for example from a string "ABC", I want to get:

['C', 'B', 'BC', 'A', 'AB', 'ABC']
(without "AC" which is a subword)

Same goes for "123":
I want to get ['3', '2', '23', '1', '12', '123'] instead of ['3', '2', '23', '1', '13', '12', '123']

CodePudding user response:

Here is a simple loop and slice based generator function:

def subs(s):
    for i in range(len(s)):
        for j in range(i 1, len(s) 1):
            yield s[i:j]

>>> list(subs("ABC"))
['A', 'AB', 'ABC', 'B', 'BC', 'C']

CodePudding user response:

For ABC you can just get ['C', 'B', 'BC', 'A', 'AB', 'ABC', 'AC'] then use remove() to remove the subword from your list. E.i:

abc_list = ['C', 'B', 'BC', 'A', 'AB', 'ABC', 'AC']
abc_list.remove('AC')

Output: ['C', 'B', 'BC', 'A', 'AB', 'ABC']

There is a lack of context to the question to give you a full answer. Do all of your strings have 3 characters or more? how do you define what you don't need? If all the strings are 3 characters in length, then you can use this:

def subwording(word: str):
    subword = word[0] word[2]
    return subword

Then you can remove subword from your list.

  • Related