你好,我是Python新手,我正在做一个小项目,但我的一些list输出有这个问题,我和array而不是float在我的list中试图使用astype(float)转换它,但仍然没有任何变化,这是我的代码:
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)
这是我运行代码时得到的结果数组标记总是在我的列表中
[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]
2条答案
按热度按时间omqzjyyz1#
如果我没理解错的话,你想要对标准Python列表的每一项执行一次操作,为此你可以使用列表解析(渴望求值)或Map函数(懒惰求值)。
用您的函数替换
round()
函数。np8igboo2#
我想您指的是
rvs(1)
方法,它返回一个包含单个值的Numpy数组,例如:输出
[array([2.12212313])]
,因为你的Python列表中有一个Numpy向量。有几种方法可以解决这个问题,你可以不把1
传递给这个方法:它输出
[1.876287479148669]
,即没有嵌套向量。或者,您可以使用反结构化赋值:或者您可以显式索引该向量:
但我建议使用第一种变体。