複数のリストを統合してCSVへ格納する際に、行列を同じ大きさへ統合したいのですが、やり方がわかりません。
for文で子リストへ親リストのある要素を入れ込み、それに合わせて別の子リストへ親リストの要素を入れたいと考えています。
具体的には、親リスト「Airplane」にある「<simulatesource.hanedaapproach object at 0x105156cc0>」という要素がリスト「Airplane1」に格納された際、それに対応する要素を「Airplane2」へ入れ込みたいのです。どのようにすれば良いのかアドバイスを願います。
以下コードです:
Python
1csvfile = open('testdata.csv') 2for row in csv.reader(csvfile): 3 print(row) # 一行づつプリントしていきます 4 5 with open('testdata.csv') as fp: 6 lst = list(csv.reader(fp)) 7 print(lst) 8 i = 1 # カウンタを置いておきます 9 Airplane = {} # 空の辞書追加 10 11 for l in lst: 12 print (l) 13 print(type(l)) 14 for n in l: 15 time = l[0] 16 flightnumber = l[1] 17 position1 = l[2] 18 position2 = l[3] 19 altitude = l[4] 20 shipname = l[5] 21 22 Airplane[i] = hanedaapproach( 23 time, flightnumber, position1, position2, altitude, shipname 24 ) 25 # それぞれの値を指定して、空の辞書へ打ち込んでいきます 26 27 28 i = i + 1 29 30 31Airplane1 = [] 32Airplane2 = [] # 空のリストを作っていきます 33 34Airplane[i] = hanedaapproach( 35 time, flightnumber, position1, position2, altitude, shipname 36 ) 37 38for i in Airplane: 39 if float(Airplane[i].position1)>=30.4436125: 40 Airplane1.append(Airplane[i]) 41 42 for j in Airplane: 43 if Airplane[i].time == Airplane[j].time and \ 44 (Airplane[i].position1 != Airplane[j].position1 or 45 Airplane[i].position2 != Airplane[j].position2): 46 Airplane2.append(Airplane[j]) 47 break 48 else: 49 Airplane2.append('') 50 51print(Airplane1) 52print (Airplane2)
現在の出力
[<simulatesource.hanedaapproach object at 0x105156cc0>, <simulatesource.hanedaapproach object at 0x105156f28>, <simulatesource.hanedaapproach object at 0x105156cf8>, <simulatesource.hanedaapproach object at 0x105156da0>] ['', <simulatesource.hanedaapproach object at 0x105156f28>, <simulatesource.hanedaapproach object at 0x105156cc0>, '', '', '', '', '', '', '', '']
期待される出力
[<simulatesource.hanedaapproach object at 0x105156cc0>, <simulatesource.hanedaapproach object at 0x105156f28>, <simulatesource.hanedaapproach object at 0x105156cf8>, <simulatesource.hanedaapproach object at 0x105156da0>] [<simulatesource.hanedaapproach object at 0x105156f28>, <simulatesource.hanedaapproach object at 0x105156cc0>,'', '']
または
[<simulatesource.hanedaapproach object at 0x105156cc0>, <simulatesource.hanedaapproach object at 0x105156f28>] [<simulatesource.hanedaapproach object at 0x105156f28>, <simulatesource.hanedaapproach object at 0x105156cc0>] #削除したデータ [<simulatesource.hanedaapproach object at 0x105156cf8>, <simulatesource.hanedaapproach object at 0x105156da0>]
補足
hanedaapproachのコードは以下の通りです。
python
1 2heavylist = ['B77W','B747'] 3mediumlist = ['B763','A333','A332','B789','B788','B78W','A358'] 4lightlist = ['B738','B737','B735','B734','A321','A320','A319'] 5class hanedaapproach: 6 def __init__(self, time, flightnumber, position1, position2,altitude, shipname): 7 self.time = time 8 self.flightnumber = flightnumber 9 self.altitude = altitude 10 self.position1 = position1 11 self.position2 = position2 12 self.shipname = shipname 13 14 def info(self): 15 for i in heavylist: 16 if self.shipname == i: 17 return 'H' 18 for j in mediumlist: 19 if self.shipname == j: 20 return 'M' 21 for k in lightlist: 22 if self.shipname == k: 23 return 'L' 24