-1

Suppose I have a list like this:

my_list = ['a_norm', 'a_std', 'a_min', 'a_max', 'a_flag', 'b_norm', 'b_std', 'b_min', 'b_max', 'b_flag', 'c_norm', 'c_std', 'c_min', 'c_max', 'c_flag']

I want to parse this list and create nested lists within separate lists like this. It is very important that I retain the order of the :

a_list = [[1, "a_norm"], [2, "a_std"], [3, "a_min"], [4, "a_max"], [5, "a_flag"]]
b_list = [[1, "b_norm"], [2, "b_std"], [3, "b_min"], [4, "b_max"], [5, "b_flag"]]
c_list = [[1, "c_norm"], [2, "c_std"], [3, "c_min"], [4, "c_max"], [5, "c_flag"]]

I tried the below which, in addition to being convoluted, did not work (endless loop). Any suggestions on how to accomplish my end goal?

a_list = []
b_list = []
c_list = []
i = 1
j = 1
k = 1
while (i < 6) and (j < 6) and (k < 6):
    for item in my_list:
        if 'a' in item:
            a_list.append(i)
            a_list.append(item)
            i + 1
        elif 'b' in my_list:
            b_list.append(j)
            b_list.append(item)
            j + 1
        elif 'c' in my_list:
            c_list.append(k)
            c_list.append(item)
            k + 1
6
  • 1
    depend on what you order the values like (a_norm , a_std) , why a_std is not 1 for example ? Commented Aug 8, 2024 at 15:52
  • @Mohammedalmalki - The order must remain fixed. Assume that the order should match the desired outcome for this question. Commented Aug 8, 2024 at 16:36
  • "retain the order of the"...? Commented Aug 8, 2024 at 16:43
  • Desired outcome. Essentially: order = {"norm":1, "std":2,"min":3,"max":4,"flag":5} Commented Aug 8, 2024 at 16:45
  • Stack Overflow is meant for specific questions, so "suggestions" is too broad. What exactly do you need help with? Like to start, do you realize that the while loop is unnecessary, set is not what you want, and i + 1 has no effect? Check out How to Ask and you can edit to clarify if needed. Commented Aug 8, 2024 at 16:47

3 Answers 3

2

This will do the parsing you want; you can then assign the items in parsed_lists as you wish.

parsed_lists = {'a':[], 'b':[], 'c':[]}
for x in my_list:
    v = parsed_lists[x[0]]
    v.append([len(v)+1,x])

which results in:

{'a': [[1, 'a_norm'], [2, 'a_std'], [3, 'a_min'], [4, 'a_max'], [5, 'a_flag']], 
 'b': [[1, 'b_norm'], [2, 'b_std'], [3, 'b_min'], [4, 'b_max'], [5, 'b_flag']], 
 'c': [[1, 'c_norm'], [2, 'c_std'], [3, 'c_min'], [4, 'c_max'], [5, 'c_flag']]}
Sign up to request clarification or add additional context in comments.

1 Comment

Great answer. I went with this because it was the most succinct. Appreciate all the answers and feedback.
1

You can use itertools.groupby to group your original list into subsequences based on the first letter of each list element.

>>> from itertools import groupby
>>> from operator import itemgetter
>>> f = itemgetter(0)
>>> my_list = ['a_norm', 'a_std', 'a_min', 'a_max', 'a_flag', 'b_norm', 'b_std', 'b_min', 'b_max', 'b_flag', 'c_norm', 'c_std', 'c_min', 'c_max', 'c_flag']
>>> d = {k: list(map(list, enumerate(v, start=1))) for k, v in groupby(sorted(my_list, key=f), f)}
>>> d['a']
[[1, 'a_norm'], [2, 'a_std'], [3, 'a_min'], [4, 'a_max'], [5, 'a_flag']]

Comments

1

this code does your jobs that you want,

but I do not know depends on what you order the values like (a_norm , a_std) , why a_std is not 1 for example ?

but this is the code:

my_list = ['a_norm', 'a_std', 'a_min', 'a_max', 'a_flag', 'b_norm', 'b_std', 'b_min', 'b_max', 'b_flag', 'c_norm', 'c_std', 'c_min', 'c_max', 'c_flag']

letters = {}

order = {"norm":1,
         "std":2,
         "min":3,
         "max":4,
         "flag":5}

for i in my_list:
    letter = i.split("_")[0]
    value = i.split("_")[1]

    to_append = [order.get(value), i]

    if letters.get(letter):
        letters.get(letter).append(to_append)
    else:
        letters.update({letter : [to_append]})

print(letters)

a_list = letters.get("a")
b_list = letters.get("b")
c_list = letters.get("c")

print(a_list)
print(b_list)
print(c_list)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.