3 回答
TA贡献1806条经验 获得超5个赞
你已经结合了几个半解决方案;这是不需要的。您只需查找路线和日期索引:
route_pos = Route.index(Bus)
day_pos = Days.index(Day)
BusData.append(BusData[route_pos][day_pos]
但是,我完全不确定这是您想要的。这会将单个整数附加到 2D 列表,例如:
[[0,0,0,2,2,4,0,3,4,-2,-5,0,0,3,4,-1,8,1,1,-2], #Bus A
[0,1,0,0,1,2,0,0,0,0,1,0,0,0,2,0,0,1,0,0], #Bus B
[2,0,-1,-1,-2,-2,-3,-1,0,0,-2,0,1,1,1,1,-1,-1,2,-1], #Bus C
[1,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0], #Bus D
[-1,-1,-1,-2,-4,-10,-2,0,0,0,0,1,2,-3,1,1,3,-1,0,0], #Bus E
[0,-5,-5,-5,-4,-3,-5,0,0,0,0,-2,-3,1,1,1,0,0,-2,-5], #Bus F
2 # New element
]
TA贡献1808条经验 获得超4个赞
首先python使用列表而不是数组。要附加到列表,请使用 .append 函数,例如:
List_name.append(Insert_data_to_append_here)
在你的情况下:
BusData.append(BusData[route_pos][day_pos])
代替:
DataPos = BusData[RoutePos][DayPos] BusData.append(DataPos)
所以它合并成一行,因为它使它更干净。
TA贡献1816条经验 获得超4个赞
我不确定这是否合适,因为也许您需要列表形式的数据。但如果没有,Pandas 会让这件事变得微不足道。
import pandas as pd
# the fourth row had an extra digit, I deleted a zero
df = pd.DataFrame(data=BusData, index=Route, columns=Days)
df=df.T
df.loc['Mon 1', 'Bus B'] = 25
df.loc['Wed 1'] = 100
df.loc[:,'Bus F'] = 999
df
Bus A Bus B Bus C Bus D Bus E Bus F
Mon 1 0 25 2 1 -1 999
Tue 1 0 1 0 0 -1 999
Wed 1 100 100 100 100 100 999
Thu 1 2 0 -1 0 -2 999
Fri 1 2 1 -2 0 -4 999
Mon 2 4 2 -2 0 -10 999
Tue 2 0 0 -3 0 -2 999
Wed 2 3 0 -1 0 0 999
Thu 2 4 0 0 0 0 999
Fri 2 -2 0 0 0 0 999
Mon 3 -5 1 -2 2 0 999
Tue 3 0 0 0 0 1 999
Wed 3 0 0 1 0 2 999
Thu 3 3 0 1 0 -3 999
Fri 3 4 2 1 0 1 999
Mon 4 -1 0 1 0 1 999
Tue 4 8 0 -1 0 3 999
Wed 4 1 1 -1 0 -1 999
Thu 4 1 0 2 0 0 999
Fri 4 -2 0 -1 0 0 999
添加回答
举报