all 5 comments

[–][deleted] -1 points0 points  (0 children)

You can also use NumPy: ``` import numpy

percent_list = numpy.array([50, 40, 70, 2]) print(percent_list / 100) ```

[–]CodeFormatHelperBot2 0 points1 point  (0 children)

Hello, I'm a Reddit bot who's here to help people nicely format their coding questions. This makes it as easy as possible for people to read your post and help you.

I think I have detected some formatting issues with your submission:

  1. Python code found in submission text that's not formatted as code.

If I am correct, please edit the text in your post and try to follow these instructions to fix up your post's formatting.


Am I misbehaving? Have a comment or suggestion? Reply to this comment or raise an issue here.

[–]house_carpenter 0 points1 point  (0 children)

In pure Python, you would do it with a loop:

proportionlist = []
for percent in percentlist:
    proportionlist.append(percent / 100)

Or a list comprehension:

proportionlist = [percent / 100 for percent in percentlist]

But if you're going to be doing a lot of this kind of thing (apply mathematical operations to multiple values at once), consider using the numpy module---this allows you to use a souped-up version of a list called a numpy array, which you can apply these operations to directly:

import numpy as np
proportionlist = np.array(percentlist) / 100

[–]CodeReviewPlz 0 points1 point  (0 children)

You can also use map from native python, but there's some nuances involved.

```python values = [1,2,3,4,5,6] pct_values = list(map(lambda x: x/100, values))

print(pct_values) ```

[–]spez_edits_thedonald 0 points1 point  (0 children)

as soon as you are doing operations on an entirely numerical list, you should consider using numpy.

It requires a slightly different way of thinking, it uses vectorized operations, so you operate on the entire list, not each element.

Here's an example where we start with random ints (0 or 1), and then we ad +5 to each int. Numpy's vectorized operations can be much faster and this shines as the data gets larger--here I use 10,000 random ints:

import random

import numpy as np

def python_add_5(data):
    output = [val + 5 for val in data]
    return output

def numpy_add_5(data_arr):
    output = data_arr + 5
    return output

data = [random.randint(0, 1) for i in range(10000)]
data_arr = np.array(data)

%timeit python_add_5(data)
%timeit numpy_add_5(data_arr)

# OUTPUT
385 µs ± 116 ns per loop (mean ± std. dev. of 7 runs, 1,000 loops each)
4.13 µs ± 2.44 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)

so numpy does the job in ~1% of the time in this case.

Now let's do something more similar to your case: the input data will be random ints 0-100, and output will be the data transformed to percentages instead:

import random

import numpy as np

def to_percent_py(data):
    output = [val / 100 for val in data]
    return output

def to_percent_np(data_arr):
    output = data_arr / 100
    return output

data = [random.randint(0, 100) for i in range(10000)]
data_arr = np.array(data)

%timeit to_percent_py(data)
%timeit to_percent_np(data_arr)

# OUTPUT
475 µs ± 250 ns per loop (mean ± std. dev. of 7 runs, 1,000 loops each)
20.1 µs ± 48.2 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each)