hi i'm new to python and i was working on a mini project but i have this problem with some of my list output where i and array instead of float in my list tried to convert it using astype(float) but still nothing changed here is my code :
`
import numpy as np
import scipy.stats as st
import numpy.random as rd
from IPython.display import Markdown, display
import pandas as pd
service=['essence','gasoil','lavage','vidange'] #list of services
prob=[0.5, 0.25, 0.16, 0.09] #probability of the services (50%,25%,16%,9%)
λ = 2 #avrege cars per minute
n=1440 #simulation for 1440 minute
model=st.poisson(λ)
#essence and gasoil simulation
e ,g , σ = 2.12,1.99, 0.1 #'e'/'g' is the avrege time of pumping essence and gasoil / 'σ' is the gap
m=1
model_e=st.norm(e, σ)
model_g=st.norm(g, σ)
#code
J=30 #simulation for 1 mounth
d=[] # a list for conversion
#the list of different servvices
essence=[]
gasoil=[]
vidange=[]
lavage=[]
for j in range(J):
c=0 #the number of cars in a day
simulation=model.rvs(n)
for i in range(n):
c =simulation[i] #calculating the number of cars in eache day
commandes=rd.choice(service, c, p=prob)
d= commandes.tolist() #commandes is ndarray i need to transform it to list in order to use "count"
simulation_e=model_e.rvs(m)
simulation_e=simulation_e.astype(float) # treid to use astype(float) to convert array to list but did't work
simulation_e=simulation_e*d.count('essence')
essence.append(simulation_e)
gasoil.append(d.count('gasoil')*model_g.rvs(m))
vidange.append(d.count('vidange'))
lavage.append(d.count('lavage'))
print(essence)
print("\n")
print(gasoil)
print("\n")
print(vidange)
`
this is the result that i get whene runing my code the array tag is always there in my list
`
[array([3049.02567421]), array([3115.46971158]), array([3057.74798456]), array([3169.46760693]), array([2993.79610725]), array([3075.71865925]), array([3204.53370577]), array([3129.65493394]), array([2975.22631282]), array([2945.63018474]), array([2843.09430445]), array([3314.12357151]), array([2796.23558937]), array([3123.59352839]), array([2983.00360539]), array([2883.79955281]), array([3056.7536885]), array([2556.95916304]), array([3050.10908716]), array([3226.86445445]), array([3282.64925171]), array([2922.09414665]), array([3127.7556254]), array([2901.03020042]), array([3186.59201801]), array([3100.92830043]), array([2920.10972545]), array([3279.21261218]), array([3189.59323404]), array([3120.11085555])]
[array([1339.12703414]), array([1354.32216511]), array([1467.59247591]), array([1331.76259858]), array([1397.63279282]), array([1452.76958164]), array([1444.76437058]), array([1301.58913082]), array([1361.35320908]), array([1467.51667652]), array([1572.44383252]), array([1252.75929698]), array([1434.85771546]), array([1487.10124071]), array([1334.69536144]), array([1499.65478204]), array([1513.6470695]), array([1531.20406829]), array([1402.24883398]), array([1464.77013383]), array([1566.69506967]), array([1341.01313426]), array([1364.15290992]), array([1477.4313931]), array([1564.68352222]), array([1622.30631325]), array([1340.63426348]), array([1423.24463625]), array([1577.83964284]), array([1533.9487886])]
[268, 247, 260, 246, 276, 263, 284, 270, 265, 280, 279, 244, 281, 247, 283, 257, 248, 241, 259, 240, 250, 237, 273, 273, 288, 256, 272, 254, 259, 279]
`
CodePudding user response:
If I understand correctly, you want to perform an operation with each item of a standard Python list. For that you can either use list comprehension (eager evaluation) or map function (lazy evaluation).
# input list
l = [1.1, 2.2, 3.3]
# list comprehension, evaluated immediately (eager)
l1 = [round(i) for i in l]
# map, evaluated on demand (lazy)
l2 = map(round, l)
# you can turn this eager by casting to list
l2 = list(map(round, l))
Replace the round()
function by whatever your function is.