更新python词典元素会给出[]

时间:2016-03-09 12:58:21

标签: python mongodb list python-2.7 dictionary

我的代码就像这样,我想更新列表中的一些项目:

coordinates = mongo_query.get_items({})
for json in coordinates:
     json["person"] = "test"
     json["location"] = "test"
     for j in range(0, len(json["types"])):
         json["types"][j] =  "test"
new_coordinates = coordinates

这时我调试变量new_coordinates是空的,如下所示:'[]'

坐标的结果如果我json_util.dumps(coordinates)它给出了这个:

   coordinates= [{"name": "my name", "timestamp": {"$date": 1459002562091}, "longitude": 20.169550966746304, "location": "Work", "victim": {"language": "English", "locality": "Bern", "gender": "Other", "region": "Gabon", "birthday": {"$date": 506736000000}, "nationality": "United States", "ethnicity": "Bosnian"}, "person": "Stranger", "latitude": 43.05529651674635, "personGender": "Male", "types": ["Shouting"]}, {"name": "my name", "timestamp": {"$date": 1455632962091}, "longitude": 21.292620354706038, "location": "Public Space", "victim": {"language": "English", "locality": "Ferizaj", "gender": "Other", "region": "Kosovo", "birthday": {"$date": 601516800000}, "nationality": "Canada", "ethnicity": "Turkish"}, "person": "Waiter", "latitude": 42.81558228232729, "personGender": "Male", "types": ["Comments", "Whistling"]}]

为什么会发生这种情况我不明白为什么我无法更新坐标列表上的元素,以及为什么它会给出一个空列表?有人能帮助我吗?

编辑:

请在此处更好地查看坐标:Jsonblob link

1 个答案:

答案 0 :(得分:1)

我重复了您的代码,导入$(".menuapp a").click(function(e){ var showthis = $(".menuapp a").attr("href") e.preventDefault(); alert(showthis); var url = "app.html"; $(location).attr('href',url); $("#dynamic").load(showthis)); }); 并且我能够修改坐标 - 请注意您拼错的“人物”为“peron”。

我会调查:

  • 你确定你毕竟没有对坐标做任何事情吗? 请注意,coordinates = <your .json file>something = other_something
  • 的说法不同
  • 您在列表[坐标]中打印的坐标变量。
  • 我建议你在python shell上重复我的程序:something = copy(other_something)并尝试其中一个元素。这对我有用:

    coordinates = <copy the content that you've output through that link>

    输出:coordinates = # copy the stuff here [ {} ] for json in coordinates: json['person'] = 'bla' new_coordinates = coordinates new_coordinates