0

I have a count of integer frequencies that I am trying to get into an array. L1 are the integers from 1 to 9, but only if they occur, I want to use this as the array index. L2 is the frequency of the integer and I want that to be entered in the array.

L1 = [1,3,4,5,6,7,8,9] #no twos occurred in the data so 2 is not in L1

L2 = [6,7,1,2,8,4,2,1]

The out put I want to get is: A1 = [[6,0,7],[1,2,8],[4,2,1]] I feel like I'm missing something but this is my last attempt:

for num in L1 and count in L2:
    a1[:num] = L2[:count]

1 Answer 1

1

Make the list arrays for ease of use:

In [286]: L1 = np.array([1,3,4,5,6,7,8,9])
     ...: L2 = np.array([6,7,1,2,8,4,2,1])

Make a place to put values:

In [287]: res = np.zeros(10,int)
In [288]: res[L1]
Out[288]: array([0, 0, 0, 0, 0, 0, 0, 0])
In [289]: res[L1]=L2
In [290]: res
Out[290]: array([0, 6, 0, 7, 1, 2, 8, 4, 2, 1])

oops, offset a bit.

In [291]: res = np.zeros(10,int)
In [292]: res[L1-1]=L2
In [293]: res
Out[293]: array([6, 0, 7, 1, 2, 8, 4, 2, 1, 0])

correct the initial size, and reshape:

In [294]: res = np.zeros(9,int)
In [295]: res[L1-1]=L2
In [296]: res.reshape(3,3)
Out[296]: 
array([[6, 0, 7],
       [1, 2, 8],
       [4, 2, 1]])
Sign up to request clarification or add additional context in comments.

1 Comment

Awesome! thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.