Python Forum
enumeration code help - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: enumeration code help (/thread-15570.html)



enumeration code help - sar - Jan-22-2019

Hi there, I am a new user of python and just want to know if there is anyway to shorten by below code in a way that I can multiply all records with a single value. At the moment I am selecting every single rec and multiplying it individually.
newList = []
for idx,rec in enumerate(tmp):
    newList.append ((rec[0]*2, rec[1] *2, rec[3] *2,
    rec[4] *2))



RE: enumeration code help - ichabod801 - Jan-22-2019

Please use python and output tags when posting code and results. I put them in for you this time. Here are instructions for doing it yourself next time.

I'm not sure what you need enumerate for, you don't seem to be using idx. However, a list comprehension will handle the multiplication problem:

newList = []
for rec in tmp:
    newList.append([item * 2 for item in rec])
It's a quick way to write a loop that creates a list. In fact, your whole code could be one big list comprehension:

newList = [[item * 2 for item in rec] for rec in tmp]



RE: enumeration code help - nilamo - Jan-22-2019

Sure can! So we have a base of understanding, here's your current code:
>>> tmp = [
... [1, 2, 3, 4],
... [5, 6, 7, 8],
... [9, 10, 11, 12]
... ]
>>> newList = []
>>> for rec in tmp:
...   newList.append([rec[0]*2, rec[1]*2, rec[3]*2])
...
>>> newList
[[2, 4, 8], [10, 12, 16], [18, 20, 24]]
Here's a list comprehension that does the same:
>>> [[a*2, b*2, d*2] for a,b,c,d in tmp]
[[2, 4, 8], [10, 12, 16], [18, 20, 24]]



RE: enumeration code help - perfringo - Jan-22-2019

You can take advantage of list comprehension:

>>> lst = [1, 2, 3]
>>> [x * 2 for x in lst]
[2, 4, 6] 
It 'reads' something like that: give me element multiplied with 2 for for every element in lst


RE: enumeration code help - sar - Jan-22-2019

Thanks all. That worked for me. All solutions are fine and worked well for me. I applied ichabod801's second solution as a final solution as my list may get bigger or smaller depending on the data.

I am basically using it to extract some attribute values from shape and factorising it to reapply it back on to the shape.

Thanks again!


RE: enumeration code help - DeaD_EyE - Jan-22-2019

For later use, you should look for numpy.
This allows for example broadcasting.

import numpy as np


def gen_signal():
    amplification = 10.5
    time_base = np.linspace(0, np.pi * 2, 1_000_000)
    signal = np.sin(time_base * 1440) # 1440 Hz
    signal += np.sin(time_base * 440) # 440 Hz
    signal += np.sin(time_base * 20) # 20 Hz
    signal += np.sin(time_base * 44) # 44 Hz
    signal *= amplification
    return signal
But you should also know the Python primitives.
I see often, that people are using this libraries,
but they don't know how they can do this without this library.

The benefit of numpy is, that it's implementation is in C and highly optimized.
But for small lists it's ok to use a normal list comprehension or if you want to save memory, a generator expression.