Home>

### how to multiply each element by a number in a python 2d list

The ones listed are part of the program.
I want to multiply each element of the list (light) that represents the amount of solar radiation by a value, but I get an error with solar1 but with solar2. Where can I change it to improve it?

light1 = [1,2,3,4]
solar1 = [0.7 * 1 * Powerpv, 0.7 * 2 * Powerpv, 0.7 * 3 * Powerpv, 0.7 * 4 * Powerpv]

light2 = [[1,2,3,4], [5,6,7,8]]
solar2 = [0.7 * 1 * Powerpv, 0.7 * 2 * Powerpv, 0.7 * 3 * Powerpv, 0.7 * 4 * Powerpv], [0.7 * 5 * Powerpv, 0.7 * 6 * Powerpv, 0.7 * 7 * Powerpv, 0.7 * 8 * Powerpv]]
It is an image that looks like.

``````code
from __future__ import division
from pulp import LpVariable, LpProblem, LpStatus, lpSum, value
import numpy as np
import random, pulp

#PV parameters
Powerpv1 = LpVariable ('Powerpv', 0, None)
Powerpv2 = LpVariable ('Powerpv', 0, None)
light1 = [random.uniform (0, 10) for i in range (24)] #Insolation
solar1 = [0.7 * q * Powerpv1 for q in light1] #pv Power generation
n = 3 # number of repetitions
for k in range (n):
light2 = [[np.random.uniform (0,10) for i in range (24)] for k in range (n)] #Insolation
solar2 = [0.7 * r * Powerpv2 for r in light2] #pv Power generation
print (solar1)
print (solar2)``````

error
TypeError Traceback (most recent call last)
in
15 for k in range (n):
16 light2 = [[np.random.uniform (0,10) for i in range (24)] for k in range (n)] #Insolation
--->17 solar2 = [0.7 * r * Powerpv2 for r in light2] #pv Power generation
18
19 print (solar1)

in(.0)
15 for k in range (n):
16 light2 = [[np.random.uniform (0,10) for i in range (24)] for k in range (n)] #Insolation
--->17 solar2 = [0.7 * r * Powerpv2 for r in light2] #pv Power generation
18
19 print (solar1)

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

• Answer # 1

I'm not sure what the Powerpv is, but it's probably because of the following error:
Since light2 has a list in the list, the list will come out just by extracting the element one step. And multiplying the list by an integer increments the list itself, not the numbers of the elements in the list.

``````light1 = [1,2,3,4]
print ([l * 2 for l in light1]) # Product of element (integer) and integer
# [2, 4, 6, 8]
light2 = [[1,2,3,4], [5,6,7,8]]
print ([l * 2 for l in light2]) # Product of element (list) and integer
# [[1, 2, 3, 4, 1, 2, 3, 4], [5, 6, 7, 8, 5, 6, 7, 8]]
light2 = [[1,2,3,4], [5,6,7,8]]
print ([l * 0.5 for l in light2]) # Product of element (list) and float
# TypeError: can't multiply sequence by non-int of type'float'``````

So, if you want to make it work for the time being, is it like increasing the nesting as shown below?

``````n = 3 # number of repetitions
for k in range (n):
light2 = [[np.random.uniform (0,10) for i in range (24)] for k in range (n)] #Insolation
solar2 = [[0.7 * s * Powerpv2 for s in r] for r in light2] #pv Power generation``````

• Answer # 2

Since light2 is a two-dimensional list,`solar2 = [0.7 * r * Powerpv2 for r in light2]`The r in is a list, and the multiplication has failed.

Amendment 1: Make light2 a one-dimensional list

``````for k in range (n):
light2 = [np.random.uniform (0,10) for i in range (24 * n)] #Insolation ★ Correction
solar2 = [0.7 * r * Powerpv2 for r in light2] #pv Power generation``````

Amendment 2: Broadcast calculation of r as ndarry type

``````for k in range (n):
light2 = [[np.random.uniform (0,10) for i in range (24)] for k in range (n)] #Insolation
solar2 = [np.array (r) * 0.7 * Powerpv2 for r in light2] #pv Power generation ★ Correction``````

Trends