0

I'm quite a newbie and I'm converting all my excel vba to python at this moment.

I have these lists.

s1a = [1, 2, 3]
s1b = [2, 3, 4]
s2a = [3, 4, 5]
s2b = [4, 5, 6]
s3a = [5, 6, 7]
s3b = [6, 7, 8]

and I've got another list,

result = ['s2as1a', 's1bs2a', 's1as3b', 's2as3a']

from the lists above, I'd like to make the following lists.

A = [3, 4, 5, 1, 2, 3] # s2a + s1a
B = [2, 3, 4, 3, 4, 5] # s1b + s2a
C = [1, 2, 3, 6, 7, 8] # s1a + s3b
D = [3, 4, 5, 5, 6, 7] # s2a + s3a

I tried to make this but I can only come up with some messy "if/elif" things.

How to make this in a nice and simple way? thanks all!

jpp
  • 159,742
  • 34
  • 281
  • 339
B.A.
  • 181
  • 1
  • 1
  • 8

1 Answers1

1

My advice is not to do this.

Use a dictionary for a variable number of variables.

result = ['s2as1a', 's1bs2a', 's1as3b', 's2as3a']

d = {'s1a': [1, 2, 3], 's1b': [2, 3, 4],
     's2a': [3, 4, 5], 's2b': [4, 5, 6],
     's3a': [5, 6, 7], 's3b': [6, 7, 8]}

res = {idx: d[i[:3]] + d[i[-3:]] for idx, i in enumerate(result)}

{0: [3, 4, 5, 1, 2, 3],
 1: [2, 3, 4, 3, 4, 5],
 2: [1, 2, 3, 6, 7, 8],
 3: [3, 4, 5, 5, 6, 7]}
jpp
  • 159,742
  • 34
  • 281
  • 339