0

I am trying to generate a list of strings, and I am looking for a simple expression to do so but can't find out.

What I have:

aScanListNames = ["AIN0", "AIN1", "AIN2", "AIN3"]
[[chan+"_NEGATIVE_CH", chan+"_RANGE", chan+"_RESOLUTION_INDEX", chan+"_EF_CONFIG_D", chan+"_EF_CONFIG_E"] for chan in aScanListNames]

Gives :

[['AIN0_NEGATIVE_CH',   'AIN0_RANGE',   'AIN0_RESOLUTION_INDEX',   'AIN0_EF_CONFIG_D',   'AIN0_EF_CONFIG_E'],  ['AIN1_NEGATIVE_CH',   'AIN1_RANGE',   'AIN1_RESOLUTION_INDEX',   'AIN1_EF_CONFIG_D',   'AIN1_EF_CONFIG_E'],  ['AIN2_NEGATIVE_CH',   'AIN2_RANGE',   'AIN2_RESOLUTION_INDEX',   'AIN2_EF_CONFIG_D',   'AIN2_EF_CONFIG_E'],  ['AIN3_NEGATIVE_CH',   'AIN3_RANGE',   'AIN3_RESOLUTION_INDEX',   'AIN3_EF_CONFIG_D',   'AIN3_EF_CONFIG_E']]

which is , as expected, a list of lists. I would like to obtain a simple list, like this :

['AIN0_NEGATIVE_CH','AIN0_RANGE','AIN0_RESOLUTION_INDEX','AIN0_EF_CONFIG_D','AIN0_EF_CONFIG_E','AIN1_NEGATIVE_CH','AIN1_RANGE','AIN1_RESOLUTION_INDEX','AIN1_EF_CONFIG_D','AIN1_EF_CONFIG_E','AIN2_NEGATIVE_CH','AIN2_RANGE','AIN2_RESOLUTION_INDEX','AIN2_EF_CONFIG_D','AIN2_EF_CONFIG_E','AIN3_NEGATIVE_CH','AIN3_RANGE','AIN3_RESOLUTION_INDEX','AIN3_EF_CONFIG_D','AIN3_EF_CONFIG_E']

For my personnal knowledge, I would like to know if there a way to obtain this directly using list comprehension? If not, what would be a pythonic way to do so?

EDIT: I know I can flatten my list of list, but I want to know if there is a solution not involving creating a list of lists to flatten it after.

CoMartel
  • 3,521
  • 4
  • 25
  • 48
  • 2
    I'd say that the pythonic way is **not** to try to put everything on a single line of code. It's already somewhat of a mess and you shouldn't need to worsen it by putting it on a single line. – skyking Apr 05 '16 at 09:28
  • 1
    https://docs.python.org/dev/library/itertools.html#itertools.chain – Andrea Corbellini Apr 05 '16 at 09:28
  • 1
    aScanListNames = ["AIN0", "AIN1", "AIN2", "AIN3"] another_list = ["_NEGATIVE_CH", "_RANGE", "_RESOLUTION_INDEX", "_EF_CONFIG_D", "_EF_CONFIG_E"] required_list = [i + j for i in aScanListNames for j in another_list ] This will give you required list. – Hassan Mehmood Apr 05 '16 at 09:45
  • I know I can flatten a list, my question was to know if there is a way to avoid creating a list of list in the first place. – CoMartel Apr 05 '16 at 11:08
  • 2
    `channels = ["AIN0", "AIN1", "AIN2", "AIN3"]; suffixes = ["_NEGATIVE_CH", "_RANGE", "_RESOLUTION_INDEX", "_EF_CONFIG_D", "_EF_CONFIG_E"]; values = [chan+s for chan in channels for s in suffixes]` – Łukasz Rogalski Apr 05 '16 at 11:16
  • Rogalski This is exactly what I was looking for. If the question re-opens, post your answer please – CoMartel Apr 05 '16 at 11:19

2 Answers2

2

You were almost there. No need for itertools

aScanListNames = ["AIN0", "AIN1", "AIN2", "AIN3"]
suffixes = ["_NEGATIVE_CH", "_RANGE", "_RESOLUTION_INDEX", "_EF_CONFIG_D", "_EF_CONFIG_E"]
result = [name+suffix for name in aScanListNames for suffix in suffixes]
C Panda
  • 3,297
  • 2
  • 11
  • 11
0

I would say this one-liner is intuitive enough:

import itertools

aScanListNames = ["AIN0", "AIN1", "AIN2", "AIN3"]
suffixes = ["_NEGATIVE_CH", "_RANGE", "_RESOLUTION_INDEX", "_EF_CONFIG_D", "_EF_CONFIG_E"]

[i[0] + i[1] for i in itertools.product(aScanListNames, suffixes)]

Output:

['AIN0_NEGATIVE_CH', 'AIN0_RANGE', 'AIN0_RESOLUTION_INDEX', 'AIN0_EF_CONFIG_D', 'AIN0_EF_CONFIG_E', 'AIN1_NEGATIVE_CH', 'AIN1_RANGE', 'AIN1_RESOLUTION_INDEX', 'AIN1_EF_CONFIG_D', 'AIN1_EF_CONFIG_E', 'AIN2_NEGATIVE_CH', 'AIN2_RANGE', 'AIN2_RESOLUTION_INDEX', 'AIN2_EF_CONFIG_D', 'AIN2_EF_CONFIG_E', 'AIN3_NEGATIVE_CH', 'AIN3_RANGE', 'AIN3_RESOLUTION_INDEX', 'AIN3_EF_CONFIG_D', 'AIN3_EF_CONFIG_E']

If you really want a one-liner you can of course provide suffixes list inline, but that's just messy.

Pelle Nilsson
  • 970
  • 8
  • 10