-2

I have values in a list of lists.

I would like to send the whole block to a conversion function which then returns all the converted values in the same structure.

my_list = [sensor1...sensor4] = [hum1...hum3] = [value1, value2, value3, value4]

So several nested lists

def conversion(my_list): dictionaries
    for sensor in my_list:
        for hum in sensor:
            for value in hum:
                map(function, value)

Is there a way to do a list comprehension as a one liner? I'm not sure how to use the map function in comprehensions especially when you have several nested iterations.

Gab
  • 5,604
  • 6
  • 36
  • 52
cc6g11
  • 477
  • 2
  • 10
  • 24

2 Answers2

4
map(function, value)

Since you are just mapping a function on each value, without collecting the return value in a list, using a list comprehension is not a good idea. You could do it, but you would be collecting list items that have no value, for the sole purpose of throwing them away later—just so you can save a few lines that actually serve a much better purpose: Clearly telling what’s going on, without being in a single, long, and complicated line.

So my advice would be to keep it as it is. It makes more sense like that and clearly shows what’s going on.

I am however collecting the values. They all need to be converted and saved in the same structure as they were.

In that case, you still don’t want a list comprehension as that would mean that you created a new list (for no real reason). Instead, just update the most-inner list. To do that, you need to change the way you’re iterating though:

for sensor in my_list:
    for hum in sensor:
        for i, value in enumerate(hum):
            hum[i] = map(function, value)

This will update the inner list.

Alternatively, since value is actually a list of values, you can also replace the value list’s contents using the slicing syntax:

for sensor in my_list:
    for hum in sensor:
        for value in hum:
            value[:] = map(function, value)

Also one final note: If you are using Python 3, remember that map returns a generator, so you need to convert it to a list first using list(map(function, value)); or use a list comprehension for that part with [function(v) for v in value].

poke
  • 369,085
  • 72
  • 557
  • 602
  • I am however collecting the values. They all need to be converted and saved in the same structure as they were. Which means in those nested for loops I need to pass everything back down the chain :S Efficient it ain't – cc6g11 Jul 21 '15 at 14:31
  • Value is not a list, they are the actual floats needing converted with the function – cc6g11 Jul 21 '15 at 14:42
  • @cc6g11 You should *really* get your question straight. It’s conflicting with what you are saying here. In your question, you seem to have a value list, and that also makes sense since you are calling `map` on it; you can’t call `map` on a single float. – poke Jul 21 '15 at 14:43
0

This is the right way to do it. You can use list comprehension to do that, but you shouldn't for code readability and because it's probably not faster.

FunkySayu
  • 7,641
  • 10
  • 38
  • 61