0

I have a df with some variables as a str like these:

a = '[],[79, 82],[82],[]'
b = '[],[71, 85],[44],[], [], [], [78,99,120,33],[]'

how can i get to:

a_list = [79, 82, 82]
b_list = [71, 85, 44, 78, 99, 120, 33]
SimAzz
  • 138
  • 2
  • 14
  • Is the input one string or two string - one for `a` and one for `b`? – kosciej16 Sep 30 '22 at 18:15
  • Can you be more specific about how exactly are your variables defined? – Luis Alejandro Vargas Ramos Sep 30 '22 at 18:16
  • 2
    "I have tried so many function like" If I use that with the input that you show, I get the expected result. "with singol digit i can make it but with doulbe digits no way" There is absolutely no reason why the number of digits in the string representation of the numbers would matter. Also, integers **don't have** digits. "the string a and b are a join of different lists" `a` and `b` are **not strings** in the example, so the description makes no sense. "I have in one variable as a text this:" I have no idea what this is supposed to mean. – Karl Knechtel Sep 30 '22 at 18:29
  • Your `list(itertools.chain.from_iterable(a)))` works if you get rid of the final unmatched parenthesis. – Steven Rumbalski Sep 30 '22 at 18:34
  • @kosciej16 two strings. – SimAzz Oct 01 '22 at 09:26
  • @Karl Knechtel could you please clarify if you tried in python to run the code? If you give it a go you will see that each number, made of 2 digits will be devided on one digit. Hope this help! I have also edit my question so you can understand better what i mean. thanks – SimAzz Oct 01 '22 at 14:18
  • Yes, I used Python to run the code. I gave it a go, and I saw no such thing, and that does not help. The edit does not clarify anything, beyond the fact that you do not have a [mre] and do not appear to understand the linked duplicate. Flattening a simple list of lists works **the same way no matter what the elements are**. – Karl Knechtel Oct 01 '22 at 17:03
  • "as all the solution indicated here How do I make a flat list out of a list of lists? have different way for solving the issue when is only ONE DIGIT NUMBER." No, they don't. "You'll get a list of devided singol digit, so every element in the list is divided" No, they will not. "Here below and example of the existing solution indicated when my question has been closed:" No, that doesn't happen. The problem is that `[] [79, 82] [82] []` is **not actually your input**. Instead, your input is a **string**, `'[] [79, 82] [82] []'`. – Karl Knechtel Oct 01 '22 at 17:04
  • @Karl Knechtel exactly you are right, my input is '[] [79, 82] [82] []'. And if i create an array manually in python into a new file like this [[],[79, 82],[82],[]] i can get it sorted quickly and everything works fine. But if i go back to my code what ever i do i get ['[] [79, 82] [82] []'] and i can't get to a solution, but now i do understand the linked duplicate. Thanks! – SimAzz Oct 01 '22 at 17:56

2 Answers2

0

Start with converting variable to list

import json

correct_json = f"[{a}]" #correct_json is list of lists [[],[79, 82],[82],[]]
l = json.loads(correct_json)

Then flatten the list

a_list = [item for sublist in l for item in sublist]
print(a_list) # [79, 82, 82]
kosciej16
  • 6,294
  • 1
  • 18
  • 29
0

Quick & Dirty Solution:

def collect_list_from_lists_tuple(t: tuple[list]) -> list:
    _collection = []
    for _sub_collection in t:
        for elem in _sub_collection:
            _collection.append(elem)

    return _collection

I would rephrased it into list comprehensions later.

alphamu
  • 383
  • 1
  • 3
  • 9
  • `if _sub_collection` is not necessary. Also, why are you naming all your local variables with a leading, single underscore? – juanpa.arrivillaga Sep 30 '22 at 18:27
  • I guess `if _sub_collection` is good for checking that list is not empty so I shall not iterate over it. All right then, may check if that might be omitted. Leading underscores added up in order to have them protected for the faint possibility of them of some mix-up – alphamu Sep 30 '22 at 18:31
  • I'm sorry, what do you mean by "protected"? In any case, if the list is empty, the t, for loop will still work and there won't be any iterations – juanpa.arrivillaga Sep 30 '22 at 18:33
  • like, this really coudl just be `collection = []` and then `for sub_collection in t: collection.extend(sub_collection)` – juanpa.arrivillaga Sep 30 '22 at 18:35
  • do you use protected variables in your code any way? or do I have a misconception over how to use it? well, agree, `.extend` makes sense. what about converting it to list comprehension for the best of aesthetics? – alphamu Sep 30 '22 at 18:41
  • Python doesn't have "protected" variables. Python doesn't have any access modifiers at all. A single leading underscore is used to denote attribute that are not meant to be part of an API, but these are local variables so that convention doesn't really apply – juanpa.arrivillaga Sep 30 '22 at 18:44
  • I agree with you in over all, however, you need to be super careful while dealing with mutable data, `list` is certainly the one. Hence, using the leading underscores so no to mix-up. Any reference would be more than welcome, btw – alphamu Sep 30 '22 at 18:49
  • "Hence, using the leading underscores so no to mix-up." *what are you talking about*? What "mix-up" is prevented by using a single leading underscore in your local variable name? – juanpa.arrivillaga Sep 30 '22 at 18:50
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/248482/discussion-between-alphamu-and-juanpa-arrivillaga). – alphamu Sep 30 '22 at 19:05