0

Input is given in ONE stretch as:

'[[F1,S1],[F2,S2],[F3,S3],[F1,S2],[F2,S3],[F3,S2],[F2,S1],[F4,S1],[F4,S3],[F5,S1]]'

and I want to convert the "string of a list of lists" into a "list of lists with all individual elements as strings"

[['F1','S1'],['F2','S2'],['F3','S3'],['F1','S2'],['F2','S3'],['F3','S2'],['F2','S1'],['F4','S1'],['F4','S3'],['F5','S1']]

How to?

limoragni
  • 2,578
  • 1
  • 28
  • 49
  • 1
    Possible duplicate of [Convert string representation of list to list](https://stackoverflow.com/questions/1894269/convert-string-representation-of-list-to-list) – Lone Lunatic Nov 01 '19 at 14:20
  • Essentially, the answer to your question is up there plus you need to convert every element in the lists to strings: `for li in input: for elm in li: elm=str(elm)` – Lone Lunatic Nov 01 '19 at 14:22
  • 1
    That's not going to work, because F1 probably isn't defined, so the initial conversion from string to list will therefore fail. Even if F1 does happen to exist, it is doubtful that it is defined in such a way that `str(F1)` will produce the string `'F1'`. – kaya3 Nov 01 '19 at 14:27
  • None of the elements are defined. – Manoj Kashyap Nov 01 '19 at 14:32
  • Yes, they don't need to be; they should be interpreted as strings, just without quotes around them in the input. – kaya3 Nov 01 '19 at 14:33
  • Yeah, realized that too. Was assuming something I shouldn't have. – Lone Lunatic Nov 01 '19 at 14:33
  • I'd probably recommend a regex. – Lone Lunatic Nov 01 '19 at 14:34
  • Where does the input come from? Could you modify the source so that you get something easier to work with, that is handled by built-in tools? In particular, having quotes around the would-be "strings" in the input would help a lot, since then you would have something that looks like a valid Python literal, and also something that looks like a valid JSON array. – Karl Knechtel Nov 02 '19 at 05:22

2 Answers2

1

I'm going to make the assumption that the input string is always formatted without any whitespace around the characters [, , or ]. This can be achieved without anything fancy or dangerous like eval:

  1. Remove the [[ and ]] from the start and end with a string slice.
  2. Then, split on ],[ which separates the inner lists from each other.
  3. Then, split each inner list on , which separates the elements from each other.

There are two special cases to deal with. First, if the outer list is empty, then the string doesn't begin or end with [[ and ]]. Second, if one of the inner lists is empty, the result of split will produce a list containing a single empty string, when the correct output should be an empty list.

def parse_2d_list(s):
    if s == '[]':
        return []
    else:
        parts = s[2:-2].split('],[')
        return [p.split(',') if p else [] for p in parts]

Output:

>>> parse_2d_list('[[F1,S1],[F2,S2],[F3,S3]]')
[['F1', 'S1'], ['F2', 'S2'], ['F3', 'S3']]
kaya3
  • 31,244
  • 3
  • 32
  • 61
0

This will be better

def parse_2d_list(s):
    parts = s[2:-2].split('],[')
    return [p.split(',') for p in parts]