ポイントのリストをto/fromタプルのリストに変換するコードを書きましたが、あまり効率的ではありません。ポイントのリストをPythonのタプルのリストに変換する
誰かがこれをより簡潔にするための提案があったのだろうかと思いましたか?
from_point, to_point = None, None
point_list = []
for p in [1, 5, 2, 4, 7, 9]:
to_point = p
if from_point and to_point:
point_list.append((from_point, to_point))
from_point = to_point
print(point_list)
入力:[1, 5, 2, 4, 7, 9]
出力:[(1, 5), (5, 2), (2, 4), (4, 7), (7, 9)]
編集:変更されたポイントは、非シーケンシャルことにします。
は、なぜあなたはfrom_pointとto_point'場合 'テストするのですか? –
@WillemVanOnsem point_listに追加する前にポイントを有効/無効にするにはこれがなければ、最初のポイントは(なし、5) – desiguel