Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.3k views
in Technique[技术] by (71.8m points)

python 3.x - TypeError: list indices must be integers or slices, not list Can't access list objects

Edit: Changed code to

for x in range(len(testdata1)):
    AC[0][2] = testdata1[x][0]
    AC[0][3] = testdata1[x][1]
    AC[1][2] = testdata2[x][0]
    AC[1][3] = testdata2[x][1]

and it works now

I'm new to python/programming generally but I'm making an ADSB tracking/live map page and one half will be a python websocket server. Right now I'm just trying to simulate two aircraft on the python end changing their positions and sending the object "AC" over websocket to my webserver and practice mapping these changes. Messing around in the IDLE typing AC[1][2] gives the predicted information.

I searched this error on google and read a bunch of stackoverflow posts and I can see it's been answered a million times but I'm not really understanding. I get that this is a fundamentals of python thing that I should really understand so it's frustrating that I'm not really comprehending the problem. Anyway here's my code.

    testdata1 = [
[40.56110168947857, -74.40852169605436],
[40.593622375749725, -74.36014373838901],
[40.62433548099761, -74.33424392932443],
[40.6178050185933, -74.30074366890673],
[40.69442898455197, -74.34597613867759],
[40.61042772408954, -74.48896675019992],
[40.58667743030954, -74.51715474507256],
[40.53301752497771, -74.42400278126973],
[40.50195614778431, -74.397374403848],
[40.52450443209101, -74.2648301163816],
[40.584134885841266, -74.19971147318574],
[40.61130210468242, -74.19337314612869],
[40.70725613044289, -74.14253803439688]
]
testdata2 = [
[40.56110168947857, -74.7],
[40.593622375749725, -74.4],
[40.62433548099761, -74.6],
[40.6178050185933, -74.1],
[40.69442898455197, -74.2],
[40.61042772408954, -74.4],
[40.58667743030954, -74.8],
[40.53301752497771, -74.1],
[40.50195614778431, -74.15],
[40.52450443209101, -74.29],
[40.584134885841266, -74.62],
[40.61130210468242, -74.81],
[40.70725613044289, -74.46]
]
AC = []
newAC = ["12AE45D","UA445",40.6, -74.2,34650,432,452,266,0.78,-896,293.84,290.039,"0s"]
newAC2 = ["17bE65D","UA315",40.6, -74.2,26650,415,433,254,0.68,-200,285.32,287.039,"0s"]
AC.append (newAC)
AC.append (newAC2)

async def hello(websocket, path):
    name = await websocket.recv()
    print("Connected")
    for x in testdata1:
        AC[0][2] = x[0]
        AC[0][3] = x[1]
        AC[1][2] = testdata2[x][0]
        AC[1][3] = testdata2[x][1]
        json_AC=json.dumps(AC)
        await websocket.send(json_AC)
        time.sleep(1)
        print(AC[0][2])

Also I think it may be better to make each aircraft organized like

 AC = [
{"ICAO" : "12AE45D","CALL SIGN" : "UA445","LAT" : 40.9, "LON" : -74.6, "ALT" : 34650,"GS" : 432,"TAS" : 452,"IAS" : 266,"MACH" : 0.78,"ROC" : -896,"TRK" : 293.84,"HDG" : 290.039,"LIVE" : "0s"}
{"ICAO" : "17bE65D","CALL SIGN" : "UA315","LAT" : 40.6, "LON" : -74.2, "ALT" : 34650,"GS" : 432,"TAS" : 452,"IAS" : 266,"MACH" : 0.78,"ROC" : -896,"TRK" : 293.84,"HDG" : 290.039,"LIVE" : "0s"}
]

This way instead of remembering that AC[0][2] means lat of the first aircraft I can just write AC[0]["lat"]. Would I run into any issues turning this into a JSON object and receiving it on the javascript end?

Thanks


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...