Home > OS >  Dynamically Concatenating String from a List in Python
Dynamically Concatenating String from a List in Python

Time:11-27

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

CodePudding user response:

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']
  • Related