-1

I am trying to combine all the elements in a list on the basis of some delimiters; I am facing difficulty when the delimiter pair is more than 1.

Say this is the list :

['{','k0c','k1b','k2b','k3b','}','{','\\g0','\\g1','\\g2','\\g3','}']

12 items in this list

Whenever it finds '{' and '}' I want all of the elements within those indexes to be concatenated into one so that it is:

['{ k0c, k1b, k2b, k3b }' , '{\\g0 , \\g1, \\g2, \\g3 }' ]

2 items in this list are what I want with all the elements inside the delimiters turned into one element of the list.

blhsing
  • 70,627
  • 6
  • 41
  • 76
zaki
  • 27
  • 1
  • 7

2 Answers2

0

Something like this oughta do the trick:

input_data = [
    "{",
    "k0c",
    "k1b",
    "k2b",
    "k3b",
    "}",
    "{",
    "\\g0",
    "\\g1",
    "\\g2",
    "\\g3",
    "}",
]
lists = []
current_list = None

for atom in input_data:
    if atom == "{":
        assert current_list is None, "nested lists not supported"
        current_list = []
        lists.append(current_list)
    elif atom == "}":
        current_list.append(atom)
        current_list = None
        continue
    assert current_list is not None, (
        "attempting to add item when no list active: %s" % atom
    )
    current_list.append(atom)

for lst in lists:
    print(" ".join(lst))

The output is

{ k0c k1b k2b k3b }
{ \g0 \g1 \g2 \g3 }

but you can do whatever you like with the lists of strings.

AKX
  • 93,995
  • 11
  • 81
  • 98
  • The output should be: { k0c, k1b, k2b, k3b } { \g0, \g1, \g2, \g3 } – zaki Mar 30 '19 at 10:29
  • As said, you can do anything you like with the lists, for instance `', '.join(lst)` if you need commas. – AKX Mar 31 '19 at 07:30
0

Assuming your data does not have any degenerate cases, we will always expect a '}','{' to separate your groups.

Therefore a simple way to get your desired output would be to join the strings together, split on } and then format the resulting list elements.

l = ['{','k0c','k1b','k2b','k3b','}','{','\\g0','\\g1','\\g2','\\g3','}']
out = [x.replace("{,", "{").strip(", ") + " }" for x in ", ".join(l).split("}") if x]
print(out)
['{ k0c, k1b, k2b, k3b }', '{ \\g0, \\g1, \\g2, \\g3 }']
pault
  • 32,557
  • 9
  • 66
  • 110
  • Can I ask for a little elaboration on what the 'for x in' and 'if x' parts are doing in this line? @pault – zaki Mar 28 '19 at 23:23
  • @zaki that is called a list comprehension. [What does “list comprehension” mean? How does it work and how can I use it?](https://stackoverflow.com/questions/34835951/what-does-list-comprehension-mean-how-does-it-work-and-how-can-i-use-it) – pault Mar 28 '19 at 23:42
  • if there are multiple { } { } { } { } will this code do it for all of them ? I still can't figure out why the if is there. – zaki Mar 30 '19 at 10:35
  • @zaki the `if` is there to handle the final `}`. The best way to understand is to try it with and without. Try it piece by piece- first the `join`, then the `split`, etc... – pault Mar 30 '19 at 13:28