How to make weighted list with list comprehension [duplicate] - python

This question already has an answer here:
flat list as a result of list comprehension [duplicate]
(1 answer)
Closed 12 months ago.
I have a list w where w[i] represents the number of times i should be present in the final result.
For example, if:
w = [1, 3, 4]
then the resulting list should be:
[0, 1, 1, 1, 2, 2, 2, 2]
Notice there is one 0, three 1's, and four 2's.
I have tried to accomplish this with the list comprehension:
w_list = [[i]*w[i] for i in range(len(w))]
but of course this doesn't quite work, giving me this:
[[0], [1, 1, 1], [2, 2, 2, 2]]
How can I write a list comprehension to get me the desired result, like in the example?

You could use a double loop to flatten w_list:
w_list = [x for num, i in enumerate(w) for x in [num] * i]
or
w_list = [x for i in range(len(w)) for x in [i]*w[i]]
Output:
[0, 1, 1, 1, 2, 2, 2, 2]

Here's an alternative solution you might like.
w = [1, 3, 4]
print([i for i in range(len(w)) for _ in range(w[i])])
results in [0, 1, 1, 1, 2, 2, 2, 2] as desired.
Alternatively, [i for i,n in enumerate(w) for _ in range(n)] accomplishes the same thing.

Related

Transform a python list to [[index] * value ] flattened

I have a list, and want to transform the list to [[index] * value] flattened.
For example if input is [1, 2, 3, 1], output should be [0, 1, 1, 2, 2, 2, 3]. I can imagine to do the following.
A = [1, 2, 3, 1]
result = []
for i,n in enumerate(A):
result += [i] * n
The result is the output I want. But as you can see, the solution is not very elegant. How to do it better?
You can use a nested list comprehension:
lst = [1, 2, 3, 1]
output = [i for i, x in enumerate(lst) for _ in range(x)]
print(output) # [0, 1, 1, 2, 2, 2, 3]
Some ways using itertools functions, though I think your loop is perfectly fine and j1-lee's is what I might've written if they hadn't already.
from itertools import chain, repeat, count, starmap
result = [*chain(*map(repeat, count(), A))]
result = list(chain.from_iterable(map(repeat, count(), A)))
result = list(chain.from_iterable(starmap(repeat, enumerate(A))))

Split one list of numbers into several based on a simple condition, lead and lag in lists [duplicate]

This question already has answers here:
Split List By Value and Keep Separators
(8 answers)
Closed 1 year ago.
Is there an easy way to split the list l below into 3 list. I want to cut the list when the sequence starts over. So every list should start with 1.
l= [1, 2, 3, 4, 5, 1, 2, 3, 4, 1, 2, 3, 4]
l1 = [1, 2, 3,4, 5]
l2=[1,2,3,4]
l3=[1,2,3,4]
My original thought was to look at the lead value and implement a condition inside a for loop that would cut the list when x.lead < x. But how do I use lead and lag when using lists in python?
NumPy solution
import numpy as np
l = [1, 2, 3, 4, 5, 1, 2, 3, 4, 1, 2, 3, 4]
parts = [list(i) for i in np.split(l,np.flatnonzero(np.diff(l)-1)+1)]
print(parts)
output
[[1, 2, 3, 4, 5], [1, 2, 3, 4], [1, 2, 3, 4]]
Explanation: I first find differences between adjacent elements using numpy.diff, then subtract 1 to be able to use numpy.flatnonzero to find where difference is other than 1, add 1 (note that numpy.diff output length is input length minus 1) to get indices for use in numpy.split, eventually convert it to list, as otherwise you would end with numpy.arrays
What about this:
l = [1, 2, 3, 4, 5, 1, 2, 3, 4, 1, 2, 3, 4]
one_indices = [i for i, e in enumerate(l) if e == 1]
slices = []
for count, item in enumerate(one_indices):
if count == len(one_indices) - 1:
slices.append((item, None))
else:
slices.append((item, one_indices[count + 1]))
sequences = [l[x[0] : x[1]] for x in slices]
print(sequences)
Out:
[[1, 2, 3, 4, 5], [1, 2, 3, 4], [1, 2, 3, 4]]
Another way without numpy,
l= [1, 2, 3, 4, 5, 1, 2, 3, 4, 1, 2, 3, 4]
start = 0
newlist = []
for i,v in enumerate(l):
if i!=0 and v==1:
newlist.append(l[start:i])
start = i
newlist.append(l[start:i+1])
print(newlist)
Working Demo: https://rextester.com/RYCV85570

create matrix using regular for loop in python [duplicate]

This question already has answers here:
2D List Comprehension to For loop
(3 answers)
Closed 2 years ago.
I want to write the following list comprehension as standard for loop. The List comprehension creates a list of lists that can be printed as matrix.
matrix = [[col for col in range(5)] for row in range(5)]
But I can't get it right to achieve this result:
[0, 1, 2, 3, 4]
[0, 1, 2, 3, 4]
[0, 1, 2, 3, 4]
[0, 1, 2, 3, 4]
[0, 1, 2, 3, 4]
How would the regular for loop look like?
merci A
If you unroll all of those list comprehensions, you get something like
matrix = []
for row in range(5):
line = []
for col in range(5):
line.append(col)
matrix.append(line)

Repeat values in a list (having two list) in python

I'm coding in Python, I have an exercise like this :
long = [5, 2, 4]
number = [1, 2, 3]
ids = []
I want to have :
ids = [1, 1, 1, 1, 1, 2, 2, 3, 3, 3, 3]
I want to repeat 5 times 1, 2 times 2, 4 times 3.
I don't know how to do it.
from collections import Counter
long = [5, 2, 4]
number = [1, 2, 3]
ids = list(Counter(dict(zip(number, long))).elements())
print(ids)
You can do it with a simple loop which will iterate over (times-to-repeat, number) pairs and extend your output list with generated list of numbers :
for times, n in zip(long, number):
ids.extend([n] * times)
print(ids) # [1, 1, 1, 1, 1, 2, 2, 3, 3, 3, 3]
They've provided you with two very good solutions, but I'll leave the brute force approach (hardly ever the best one) in here, since it's the one you're likely more prone to understand:
long = [5, 2, 4]
number = [1, 2, 3]
ids = []
for i in range(len(long)):
aux = 0
while (aux < long[i]):
ids.append(number[i])
aux += 1
print(ids)

Python list comprehension mirroring values

I tried to figure out the following problem through list comprehension but I couldn't make it work. I will show you how I solved the problem using a loop and a list comprehension.
So, I have a list that can have 0 to 6 elements in a range(6) and when I apply my function on it I want to change the values, as shown here:
l = [0, 1, 2, 3, 4, 5]
mirror = [5, 4, 3, 2, 1, 0]
I don't want to just rotate the array by 180 degrees but I actually want to replace the values. For example, my list looks like this now:
l = [2, 5]
Then l_inverted list should look like this:
l_inverted = [3, 0]
I came up with a regular way to solve it but ever since I started learning Python I've preferred list comprehensions.
l = [0, 3, 5]
mirror = [5, 4, 3, 2, 1, 0]
i = 0
for element in l:
l[i] = mirror[element]
i += 1
This actually inverts the l list. Here's my approach using a list comprehension:
l = [3, 5]
mirror = [5, 4, 3, 2, 1, 0]
for element in l:
print(element)
l = [mirror[element] if x==element else x for x in l]
This works fine.
Until:
l = [0, 3, 5]
mirror = [5, 4, 3, 2, 1, 0]
for element in l:
print(element)
l = [mirror[element] if x==element else x for x in l]
So it will replace 5 with 0, 2 with 3 and both 5s (the new one too) become 0. Obviously, I don't want it like that.
Should I stick to the working solution or is there a smooth way to solve it with list comprehensions? I'm trying to practice list comprehensions at all times but it's not fully in my brain yet. Thanks a lot.
If you want it as a list comprehension:
>> l = [0, 3, 5]
>> mirror = [5, 4, 3, 2, 1, 0]
>> l_inverted = [mirror[x] for x in l]
>> l_inverted
[5, 2, 0]
You are drowning in a spoonful of water and trying to take us with you.
You are using bad naming conventions that make your simple problem complicated to comprehend.
orig = [0, 1, 2, 3, 4, 5]
orig_rev = l[::-1]
selector = [0, 3, 5]
result = [orig_rev[i] for i in selector]
print(result ) # [5, 2, 0]
Based on your first two examples, it seems you are looking for the complement of each list value according to some base, similar to ones' complement. You could either hard-code the base as 5 or whatever, or you could assume it's the maximum number in the list and calculate it. Here's a solution for the latter:
Concept
<this complement> = <max value in list> - <this value>
Code
values = [0, 3, 2, 4, 5]
max_value = max(values)
complements = [max_value - value for value in values]
print complements
Result
[5, 2, 3, 1, 0]

Categories

Resources