Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
enumeration code help
#1
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))
Reply
#2
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]
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
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]]
Reply
#4
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
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#5
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!
Reply
#6
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.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020