0

我有两个数组。第一个来自 SQL Server,是救护车配置(工作、可用等)。第二个是那些救护车的 xy 坐标。我想根据救护车的名称加入这两个阵列。例如:

[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

问题:如何让这两个数组像 SQL Server 中的 INNER JOIN 一样连接起来?

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 只需切片第二个列表:

>>>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 回答