如何将变量附加到2D数组中?

时间:2019-02-07 19:33:46

标签: python arrays

我想将输入追加到2D数组中的特定位置。该任务是输入我自己的数据来替换数组中的位置。

#Array
Days = ["Mon 1","Tue 1","Wed 1","Thu 1","Fri 1",
        "Mon 2","Tue 2","Wed 2","Thu 2","Fri 2",
        "Mon 3","Tue 3","Wed 3" ,"Thu 3","Fri 3",
        "Mon 4","Tue 4","Wed 4","Thu 4","Fri 4"]

Route = ["Bus A","Bus B","Bus C","Bus D","Bus E","Bus F"]
            #The array in each bus is (Mon 1 - Fri 4 in each bus)
BusData = [[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

例如,我输入了日期和路线,并尝试使用索引位置将其放置在数组中。

我试图做这样的解决方案:

        #Input values in array
    for i in Route:    #Finding index of input in array 
        if Bus == i:
            RoutePos = Route.index(i)

    for i in Days:   #Finding index of input in array
        if Day == i:
            DayPos = Days.index(i)


    DataPos = BusData[RoutePos][DayPos]
    BusData.append(DataPos)

    print(BusData)

如果有人要我详细说明这个问题,请告诉我

4 个答案:

答案 0 :(得分:1)

您已经结合了几种解决方案;这不是必需的。您只需查找路线和日期索引:

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
]

答案 1 :(得分:0)

首先,python使用列表而不是数组。 要附加到列表,请使用.append函数 例如:

List_name.append(Insert_data_to_append_here)

在您的情况下:

BusData.append(BusData[route_pos][day_pos])

代替:

DataPos = BusData[RoutePos][DayPos]
BusData.append(DataPos)

因此它可以合并为一行,因为它使它更整洁。

答案 2 :(得分:0)

我不确定这是否合适,因为您可能需要列表形式的数据。但是如果没有的话,熊猫将变得无关紧要。

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

答案 3 :(得分:0)

由于尚不清楚您要寻找的是什么,因此,根据您的澄清,此答案假设以下三个条件:

  1. BusRoute
  2. 完全匹配的字符串输入
  3. DayDays
  4. 完全匹配的字符串输入
  5. BusData按照RouteDays相同顺序排序。

然后就这么简单:

route_pos = Route.index(Bus)
day_pos = Days.index(Day)
BusData[route_pos][day_pos] == some_data_to_replace

some_data_to_replace是您要替换的特定BusDay的路线信息。

例如:

Bus = input("Enter the exact bus info: ")                 # e.g. "Bus C"
Day = input("Enter the exact day info: ")                 # e.g. "Mon 2"
Data = int(input("Enter the route info to replace: "))     # e.g. 99

route_pos = Route.index(Bus)                              # position 2
day_pos = Days.index(Day)                                 # position 5 
BusData[route_pos][day_pos] == some_data_to_replace       # "row" 2, "col" 5 is replaced

BusData

# [[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,99,-3,-1,0,0,-2,0,1,1,1,1,-1,-1,2,-1],   # Bus C (pos 2) Mon 1 (pos 5) is replaced
#  [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