user309575
user309575

Reputation: 41

Creating list in a for loop

Wondering if someone can help me with a small problem bit of code I have.

list1 = [1,2,4]
dz = 0.5
k = max(list1)/dz
print('k =',k)
list1_diff = np.insert(np.diff(list1),0,1)
for item in list1_diff:
    #Number of times to repeat number
    repeats = item/dz
    vec = [np.random.normal(0,1)]*int(repeats)
    print(vec)

this produces

[-0.7014481088047718, -0.7014481088047718]
[3.1264015795601274, 3.1264015795601274]
[0.44017976879654314, 0.44017976879654314, 0.44017976879654314, 0.44017976879654314]

Which is doing sort of the right thing, but I want the output of the loop to be these three lists as one single list. So it should be

[-0.7014481088047718, -0.7014481088047718, 3.1264015795601274, 3.1264015795601274, 0.44017976879654314, 0.44017976879654314, 0.44017976879654314, 0.44017976879654314]

Upvotes: 1

Views: 361

Answers (4)

Jatin Chauhan
Jatin Chauhan

Reputation: 325

Pythons lists have an inbuilt function for this:

list_name.extend([])

For example,

list_name = [1, 2, 3]
list_name.extend([4, 5, 6])

print(sample_list)

[1, 2, 3, 4, 5, 6]

So python extend function extends the list with another list. It does not appends it!

Upvotes: 0

MOHAMED BOUZIANE
MOHAMED BOUZIANE

Reputation: 41

try this one :

import numpy as np

list1 = [1,2,4]
dz = 0.5
k = max(list1)/dz
print('k =',k)
list1_diff = np.insert(np.diff(list1),0,1)
final_list=[]
for item in list1_diff:
    #Number of times to repeat number
    repeats = item/dz
    vec = [np.random.normal(0,1)]*int(repeats)
    final_list += vec
print(final_list)

Upvotes: 1

Ironkey
Ironkey

Reputation: 2607

this should do the job:

import numpy as np

list1 = [1,2,4]
dz = 0.5
k = max(list1)/dz
print(f'k = {k}')

lst = [[np.random.normal(0,1)]*int(item/dz) for item in np.insert(np.diff(list1),0,1)]
flatten = lambda l: [item for sublist in l for item in sublist]

print(flatten(lst))

output:

k = 8.0
[-1.1762058341816053, -1.1762058341816053, -1.0599923573472354, -1.0599923573472354, 0.6374252888036466, 0.6374252888036466, 0.6374252888036466, 0.6374252888036466]

Heres what happened

  • list comprehension that creates a list full of output (mainly what you did)
  • flatten function applied to list

note that you can also use extend(), i was going to add it in but another answer beat me to it

Upvotes: 0

jottbe
jottbe

Reputation: 4521

you're not dumb. If I got you right, you want to have one list with repeated random numbers according to your formula. You would just have to modify your code a small bit, like this:

list1 = [1,2,4]
dz = 0.5
k = max(list1)/dz
print('k =',k)
list1_diff = np.insert(np.diff(list1),0,1)
# create your empty result list, where you add the random numbers in the loop
vec= list()
for item in list1_diff:
    # Number of times to repeat number
    repeats = item/dz
    # add the new random number with it's repeats to the vec list
    # using extend
    vec.extend([np.random.normal(0,1)]*int(repeats))
# print the result outside the loop, after it has been constructed
print(vec)

Upvotes: 1

Related Questions