0

For a simulation scenario I'm generating 2 random lists.

For example:

my_list_1 = [17, 4, 22, 10, 18, 19, 23, 12]

my_list_2 = [82]

Just double checked the type of my_list_2 and the result is <class 'list'>

I need to create a new_list_3 with the result of 82*17, 82*4, 82*22, etc.

I've tried this way:

result_list_3 = []
for i in (my_list):
    result_list_3.append(i * my_list_2)

And I'm getting the error:

TypeError: can't multiply sequence by non-int of type 'list'

Then I made a dictionary and a pd.DataFrame to try the following:

df['result_list_3'] = df['my_list_1'] * df['my_list_2'] 

...and I get an giant list of integers, which obviously is not the result I'm looking for.

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Foldhy
  • 47
  • 1
  • 7

3 Answers3

1

Define my_list_1, my_list_2 as np.array as follows:

my_list_1 = np.array([17, 4, 22, 10, 18, 19, 23, 12])
my_list_2 = np.array([82])

Then, we can use the broadcasting feature of numpy array like below:

my_list_3 = my_list_1 * my_list_2

You will get:

print(my_list_3)

array([1394,  328, 1804,  820, 1476, 1558, 1886,  984])
SeaBean
  • 22,547
  • 3
  • 13
  • 25
  • I'm generating the random lists through for loops. So I've defined the arrays as you indicated, but into the for loops. Still getting the error when I broadcast, really not sure why is not working for me. – Foldhy Mar 29 '21 at 21:14
  • @Foldhy For 2 linear np.array to broadcast, either they have same size or one of them has only one element, like your sample `my_list_2` above. Otherwise, you can't broadcast them. Are your arrays in compatible sizes ? – SeaBean Mar 29 '21 at 21:26
  • Well, my list_2 has always just 1 element as the example exposed. While my_list_1 changes the size since the list is generated by a loop of random integers. Is that preventing numpy arrays to work properly? – Foldhy Mar 29 '21 at 21:36
  • @Foldhy What's your function used to generate random integers ? Is it a numpy function ? – SeaBean Mar 29 '21 at 21:40
  • my_list_1 it's a while loop with 'if' and 'else' conditions into a def variable. Then I store the result in a list. The integers are generated by np.random.randint() function. For my_list_2 the only integer is generated by a for loop wich makes a division between 2 variables. – Foldhy Mar 29 '21 at 21:50
  • @Foldhy Are you sure the types of elements in your lists are all integers ? In particular, your element generated in my_list_2 by division will probably be a float type. Also, seen your logics to generate `result_list_3` above. Seems this `result_list_3` is a list of array (i.e. 2 dimensional) instead of 1 dimensional array. – SeaBean Mar 29 '21 at 22:22
  • @Foldhy The error TypeError: can't multiply sequence by `non-int of type 'list'` tells that your object used to multiply is a list instead of the expected integer. – SeaBean Mar 29 '21 at 22:24
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/230519/discussion-between-foldhy-and-seabean). – Foldhy Mar 29 '21 at 22:28
0

This can be solved easily with for loops:

result = []
for i in list1:
    for l in list2:
        result.append(i*l)
Fredericka
  • 296
  • 1
  • 7
0

You can use list comprehension:

result = [a*b for a in my_list_1 for b in my_list_2]
#[1394, 328, 1804, 820, 1476, 1558, 1886, 984]

It will work for lists of any length.

Pablo C
  • 4,661
  • 2
  • 8
  • 24
  • In my case with this solution the loop is not breaking. Something not working properly in the code? – Foldhy Mar 29 '21 at 21:15
  • @Foldhy sorry if this question is somehow offensive, but I don't know how long you've been programming ... are you printing `result`? – Pablo C Mar 29 '21 at 21:19
  • yes sure I print. However it took long to execute within my code the first time. In the second attempt is repeating the same result many times in the same row. Not sure if it's something related to the rest of my code. – Foldhy Mar 29 '21 at 21:34
  • @Foldhy maybe you're modifying the lists before – Pablo C Mar 29 '21 at 21:36
  • Consider the lists are being generated by loops of random integers. So my_list_1 changes the size constantly while my_list_2 always stick with just 1 random integer. – Foldhy Mar 29 '21 at 21:41