0

2つのアレイがあります。1つ目はSQLServerからのもので、救急車の処分(稼働中、利用可能など)です。2つ目は、これらの救急車のxy座標です。救急車の名前に基づいて、これら2つの配列を結合したいと思います。例えば:

[M01, working], [M02, free]
[M01, 446, 267], [M02, 452, 176]

次のように結合する必要があります。

[M01, working, 446, 267]
[M02, free, 452, 176]

私はとても近くにいますが、そこにたどり着くことができません。私が受け取っているエラーメッセージは次のとおりです。トレースバック(最後の最後の呼び出し):

File "C:\Documents and Settings\kulpandm\My 
Documents\EclipseCode\PyLearning\FireStations4.py", line 83, in <module>
res = [r1 + r2 for r1 in t1 for r2 in t2 if r1[1] == r2[0]]
IndexError: string index out of range

以下に私のコードを示します:

import pyodbc

cnxn = pyodbc.connect('DRIVER={SQL Server}; SERVER=xxxxxx; DATABASE=xx; UID=xx; PWD=xx')
cursor = cnxn.cursor()
AmbulanceStatus = """
WITH Maxtimes AS
(
SELECT
  MAX(M_Manpower_PK) AS [FirstRecord]
, PUN_UnitID
FROM MManpower
INNER JOIN PUnit ON M_kUnit = PUN_Unit_PK
WHERE PUN_UnitID LIKE 'M__'
AND M_tTime > DATEADD(HOUR, -24, GETDATE())
GROUP BY PUN_UnitID
)
,
MaxAttributes AS
(
SELECT
  M_Manpower_PK
, M_tTime
, M_Code
, CASE 
  WHEN M_Code IN ('USTA', 'USER', 'USOL', 'USAR', 'USIZ', 'UF', 'USTR', 'USDP') THEN 'Working'
  WHEN M_Code IN ('USAQ', 'USRQ') THEN 'Free'
  WHEN M_Code IN ('USES', 'USSB', 'USAS', 'USRS', 'USLC') THEN 'Standby'
  WHEN M_Code IN ('USOS') THEN 'OutofService'
  ELSE 'NA'
  END AS [Disposition]
FROM MManpower
INNER JOIN PUnit ON M_kUnit = PUN_Unit_PK
WHERE PUN_UnitID LIKE 'M__'
AND M_tTime > DATEADD(HOUR, -24, GETDATE())
)

SELECT
  MaxTimes.firstRecord AS [RecordNumber]
, MaxTimes.PUN_UnitID AS [Ambulance]
, CONVERT(VARCHAR(20), MaxAttributes.M_tTime, 108) AS [FinalTimeStamp]
, MaxAttributes.M_Code AS [UnitStatus]
, MaxAttributes.Disposition AS [Disposition]
FROM Maxtimes
LEFT OUTER JOIN MaxAttributes ON Maxtimes.FirstRecord=MaxAttributes.M_Manpower_PK
"""

cursor.execute(AmbulanceStatus)
t1 = []
t1 = str(cursor.fetchall())
cursor.close()

print t1

# These are hard-coded values if you want to use this as an experiment. This data is output from SQL Server query
#===============================================================================
# t1 = [['M01', 'Working'],
#               ['M02', 'NA'],
#               ['M03', 'Free'],
#               ['M04', 'Working'],
#               ['M05', 'Free'],
#               ['M06', 'Free'],
#               ['M07', 'Free'],
#               ['M08', 'Working'],
#               ['M09', 'Free'],
#               ['M10', 'Free']]
#===============================================================================
# grid coordinates of ambulance stations
t2 = [['M01', 446, 267], ['M16', 28, 45], ['M37', 504, 546], ['M13', 514, 491], ['M06', 589, 538],
                ['M04', 353, 264], ['M30', 372, 216], ['M14', 359, 125], ['M28', 319, 164], ['M26', 519, 138],
                ['M02', 452, 176], ['M03', 474, 155], ['M23', 536, 221], ['M18', 478, 212], ['M11', 546, 112],
                ['M21', 534, 431], ['M31', 430, 95], ['M15', 347, 341], ['M08', 419, 331], ['M20', 409, 278],
                ['M27', 375, 327], ['M12', 387, 161], ['M22', 338, 190], ['M40', 446, 245], ['M10', 179, 207],
                ['M09', 497, 381], ['M33', 459, 346], ['M25', 543, 394], ['M05', 224, 417], ['M19', 520, 85],
                ['M24', 359, 125], ['MT01', 370, 178], ['FBA31', 370, 178],
                ['FBA32', 363, 365], ['FBA33', 415, 444], ['D1', 442, 191],
                ['M07', 431, 145], ['M17', 404, 63], ['LAR104', 659, 216],
                ['LAR103', 612, 204], ['LAR102', 700, 231], ['LAR101', 738, 170]]

# loop through to join the two data sets like a SQL LEFT OUTER JOIN. It works with an array t1 commented out above,
# but it will not work with database output from my sql query.
res = [r1 + r2 for r1 in t1 for r2 in t2 if r1[1] == r2[0]]

for ambulance in res:
    print ambulance

質問:SQLServerのINNERJOINのように、これら2つの配列を結合するにはどうすればよいですか?

4

1 に答える 1

1

インデックスが混同されたと思います。

>>>res = [r1 + r2 for r1 in t1 for r2 in t2 if r1[0] == r2[0]]
>>>print res
[['M01', 'Working', 'M01', 446, 267], ['M02', 'NA', 'M02', 452, 176], ['M03', 'Free', 'M03', 474, 155], ['M04', 'Working', 'M04', 353, 264], ['M05', 'Free', 'M05', 224, 417], ['M06', 'Free', 'M06', 589, 538], ['M07', 'Free', 'M07', 431, 145], ['M08', 'Working', 'M08', 419, 331], ['M09', 'Free', 'M09', 497, 381], ['M10', 'Free', 'M10', 179, 207]]

また、繰り返されるIDを削除したい場合は、2番目のリストをスライスするだけです。

>>>res = [r1 + r2[1:] for r1 in t1 for r2 in t2 if r1[0] == r2[0]]
>>>print res
[['M01', 'Working', 446, 267], ['M02', 'NA', 452, 176], ['M03', 'Free', 474, 155], ['M04', 'Working', 353, 264], ['M05', 'Free', 224, 417], ['M06', 'Free', 589, 538], ['M07', 'Free', 431, 145], ['M08', 'Working', 419, 331], ['M09', 'Free', 497, 381], ['M10', 'Free', 179, 207]]

編集:Bartlomiejが私を打ち負かしたようです。

于 2013-03-20T22:02:50.963 に答える