-2

I have a list that looks like this:

list = ['EDZ8 comdtyEDM1 comdtyEDU1 comdty', 'EDZ8 comdtyEDM1 comdtyEDZ1 comdty', 'EDZ8 comdtyEDM1 comdtyEDH2 comdty']

which i'd like to split into a list of lists

split_list = [['EDZ8 comdty', 'EDM1 comdty', 'EDU1 comdty'],['EDZ8 comdty', 'EDM1 comdty', 'EDZ1 comdty'], ['EDZ8 comdty', 'EDM1 comdty', 'EDH2 comdty']]

any thoughts on how to do this? each string is the same length and the components into which it needs to be split are each 11 characters long.

user3483203
  • 50,081
  • 9
  • 65
  • 94
S.Peters
  • 185
  • 1
  • 1
  • 9
  • 1
    Can you at least create a list that we can copy paste into a terminal without syntax errors? – user3483203 Jul 16 '18 at 16:57
  • There are many Python tutorials to help you learn the `split` method and string slicing. You should be able to take it from there. Among other things, you should be able to properly format your desired input and output before making your coding attempt. What you posted is not legal Python. – Prune Jul 16 '18 at 17:01
  • Amended as requested – S.Peters Jul 16 '18 at 17:02
  • 2
    You have named your list `list`, shadowing the definition of the type. You can get away with that sometimes, but it’s not good practice. – Tom Zych Jul 16 '18 at 17:02

2 Answers2

2

Since the string length is a constant, you can use a list comprehension to split each string into chunks with length eleven:

n = 11
x = ['EDZ8 comdtyEDM1 comdtyEDU1 comdty', 'EDZ8 comdtyEDM1 comdtyEDZ1 comdty', 'EDZ8 comdtyEDM1 comdtyEDH2 comdty']
res = [[s[i:i+n] for i in range(0, len(s), n)] for s in x]

Output:

[['EDZ8 comdty', 'EDM1 comdty', 'EDU1 comdty'],
 ['EDZ8 comdty', 'EDM1 comdty', 'EDZ1 comdty'],
 ['EDZ8 comdty', 'EDM1 comdty', 'EDH2 comdty']]
user3483203
  • 50,081
  • 9
  • 65
  • 94
  • and how about if i wanted to create a list of tuples rather than a list of lists, so [('EDZ8 comdty', 'EDM1 comdty', 'EDU1 comdty'), ('EDZ8 comdty', 'EDM1 comdty', 'EDZ1 comdty'), ('EDZ8 comdty', 'EDM1 comdty', 'EDH2 comdty')] – S.Peters Jul 17 '18 at 08:35
  • Then put `tuple()` around the inner list – user3483203 Jul 17 '18 at 14:53
0

Using list comprehension and regex (assuming all your datapoints start with ED):

    my_list=['EDZ8 comdtyEDM1 comdtyEDU1 comdty, EDZ8 comdtyEDM1 comdtyEDZ1 comdty, EDZ8 comdtyEDM1 comdtyEDH2 comdty'] 

    string_lists = [x.split(',') for x in my_list]
    final_list = [[re.findall(r'ED\w+ comdty', string) for string in string_list] for string_list in string_lists]

This way it will perform this process for how ever many elements are in 'my_list'. In this case, since there is only one, the output will be

[[['EDZ8 comdty', 'EDM1 comdty', 'EDU1 comdty'], ['EDZ8 comdty', 'EDM1 comdty', 'EDZ1 comdty'], ['EDZ8 comdty', 'EDM1 comdty', 'EDH2 comdty']]]

Which is a list of list of lists. final_list[0] is the first string in the list my_list split into the list of lists you want, so if you have in the future a list had 2 strings in it, you would have elements final_list[0] and final_list[1]

The difference between this and The other answer here is that this will work without an existing knowledge of the length of the each string in your list. The other answer is also valid, but this will protect against variable lengths in the string

Richard Stoeffel
  • 695
  • 8
  • 13