0

I want to dynamically concat strings contained in a list dynamically using python but i've run into an error with my logic.

The goal is to concat the strings until an occurence of a string that starts with a digit is found, then isolating this digit string into its own variable and then isolating the remaining strings into a third variable.

For example:

stringList = ["One", "Two", "Three", "456", "Seven", "Eight", "Nine"]
resultOne = "OneTwoThree"
resultTwo = "456"
resultThree = "SevenEightNine"

Here's what i've tried:

stringList = ["One", "Two", "Three", "456", "Seven", "Eight", "Nine"]

i = 0

stringOne = ""
stringTwo = ""
stringThree = ""
refStart = 1

for item in stringList:
    if stringList[i].isdigit() == False:
        stringOne += stringList[i]
        i += 1
        print(stringOne)
    elif stringList[i].isdigit == True:
        stringTwo += stringList[i]
        i += 1
        print(stringTwo)
        refStart += i
    else:
        for stringList[refStart] in stringList:
            stringThree += stringList[refStart]
            refStart + 1 += i
        print(stringThree)

It errors out with the following message:

File "c:\folder\Python\Scripts\test.py", line 19
    refStart + 1 += i
    ^
SyntaxError: 'operator' is an illegal expression for augmented assignment
2
  • I don't see "refStart + 1 += i" part of the code Commented Nov 26, 2021 at 14:45
  • oh, my bad. I forgot i had edited it before submitting it Commented Nov 26, 2021 at 17:02

1 Answer 1

3

You can use itertools.groupby, a comprehension, and str.join:

stringList = ["One", "Two", "Three", "456", "Seven", "Eight", "Nine"]

from itertools import groupby
[''.join(g) for k,g in groupby(stringList, lambda x: x[0].isdigit())]

output:

['OneTwoThree', '456', 'SevenEightNine']
how it works:

groupby will group the consecutive values, here I used a test on the first character to detect if it is a digit. So all consecutive strings are joined together.

As a dictionary if the format better suits you:

dict(enumerate(''.join(g) for k,g in groupby(stringList, 
                                             lambda x: x[0].isdigit())))

output:

{0: 'OneTwoThree', 1: '456', 2: 'SevenEightNine'}

I don't want to join consecutive numbers!

Then you can combine the above with a test on the group identity (True if the string starts with a digit) and use itertools.chain to chain the output:

stringList = ["One", "Two", "Three", "456", "789", "Seven", "Eight", "Nine"]

from itertools import groupby, chain
list(chain(*(list(g) if k else [''.join(g)]
             for k,g in groupby(stringList, lambda x: x[0].isdigit()))))

output:

['OneTwoThree', '456', '789', 'SevenEightNine']
Sign up to request clarification or add additional context in comments.

3 Comments

Though i have one more question to add. How could i ignore any occurence of digits after the first one? So i'd have, in that last example: ['OneTwoThree', '456', '789SevenEightNine']
@Matheus do you have numbers repeatedly? Or once in the list? The more details you give on the real data, the easier to give you a good answer ;)
It's hard to say how many can be in the list, all i'm sure of is that the first occurence needs to be isolated. It's a huge 400k lines long dataset

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.