これは奇妙なものです。私が何かダムをやっていない限り。私は次のコードを持っています:別のもののコピーである変数もオリジナルを変更しています
gps_markers = [ ['Example text', 34.4563,-118.1241], [34.5235,-118.1245], [34.6432,-118.1554] ]
new_gps = gps_markers
def make_points(coords):
if(coords):
# Firstly we don't want the hover text.
for x in range(0, len(coords)):
if(len(coords[x]) == 3):
coords[x].pop(0)
new_list = [{'lat': d[0], 'lng': d[1]} for d in coords]
# Return the new list after the list comprehension.
return new_list
print '\nbefore newgps ->', gps_markers
new_gps = make_points(new_gps)
print '\nafter newgps ->', gps_markers
次に、スクリプトを実行すると、次のような出力が得られます。
before newgps -> [['Example text', 34.4563, -118.1241], [34.5235, -118.1245], [34.6432, -118.1554]]
after newgps -> [[34.4563, -118.1241], [34.5235, -118.1245], [34.6432, -118.1554]]
なぜ私には意味がないのですか。最初の変数gps_markers
は、このスクリプトのどの時点でも実行されていないはずです。私はnew_gps
と呼ばれるgps_markers
のコピーを作る。私はnew_gps
から私のmake_points
メソッドを実行します。それは前記変数に影響を与える。でもそれはgps_markers
に影響しますか?私がちょうどこれをあまりにも長く見つめていたのか、私が紛失しているものがあるかどうかはわかりません。両方のビットのように感じる。
[こちら](http://stackoverflow.com/questions/17707742/changing-a-value-in-one-list-changes-the-values-in-another-list-with-a-different ) – roganjosh
2行目では 'new_gps = gps_markers [:]'にする必要があります。それは最初のリストのコピーを作成します。 – Nf4r
'a = b'はbの新しいコピーを作成しません。 aとbはSAMEオブジェクトの異なる名前です。 –