我正在尝试合并两个json词典.到目前为止,我有一个带有内容的json文件(myjfile.json)
{"cars": 01, "houses": 02, "schools": 03, "stores": 04}
Run Code Online (Sandbox Code Playgroud)
我在python中有一个字典(mydict),如下所示:
{"Pens": 1, "Pencils": 2, "Paper": 3}
Run Code Online (Sandbox Code Playgroud)
当我把它们结合起来时,它们是两个不同的词典
with open('myfile.json' , 'a') as f:
json.dump(mydict, f)
Run Code Online (Sandbox Code Playgroud)
请注意,因为我要保持文件的内容,并开始新的一行中其被写入时间的myfile.json,正被写入"A",并在代码A/N.
我希望最终结果看起来像
{"cars": 01, "houses": 02, "schools": 03, "stores": 04, "Pens": 1, "Pencils": 2, "Paper": 3}
Run Code Online (Sandbox Code Playgroud) 我有一个看起来像这样的 python 字典
[{"hat": 91, "d1b": 2, "d1c": 1, "d1d": 5, "d1e": 7, "d1f": 77, "d1e": 999}
{"hat": 1, "d2b": 2, "d2c": 3, "d2d": 4, "d2e": 5, "d2f": 6, "d2e": 7}
{"hat": 1, "d3b": 2, "d3c": 3, "d3d": 4, "d3e": 5, "d3f": 6, "d3e": 7}]
Run Code Online (Sandbox Code Playgroud)
我将它作为字典对象 (mydict) 从 python 传递给 jinja
我要做的是遍历每个字典并打印出我搜索的键的值。并将其显示在 jquery 警报框中。
$(document).ready(function() {
{% for i in mydict %}
{{ loop.index0 }}, {{a,["hat"] }}
alert( {{ hat }} );
{% endfor %}
});
Run Code Online (Sandbox Code Playgroud)
当我转到我的网页时,它给了我一个错误
Uncaught SyntaxError: Unexpected …Run Code Online (Sandbox Code Playgroud) 我有两个字典,每个字典都有相同的键,但值不同.
food = {'fruit' : 5, 'vegetable' : 2, 'dairy' : 1, 'meat' : 3, 'grain' : 1}
fav = { 'fruit' : ['apple', 'orange', 'banana', 'grape', 'plum'],
'vegetable' : ['carrot', 'corn'],
'dairy' : ['milk'],
'meat' : ['chicken', 'egg', 'beef'],
'grain' : ['bread']
}
Run Code Online (Sandbox Code Playgroud)
我希望实现的结果将是这样的,
eats = { 'fruit' : 5, ['apple', 'orange', 'banana', 'grape', 'plum'],
'vegetable' : 2, ['carrot', 'corn'],
...
Run Code Online (Sandbox Code Playgroud)
等等
*****编辑********
例如,每个字典中的某些条目可能是空的
food = {'fruit' : 5, 'vegetable' : 2, 'dairy' : 1, 'meat' : 3, 'grain' …Run Code Online (Sandbox Code Playgroud) 我有一个带有字典的 Json 文件,看起来像
{"tvs": 92, "sofas": 31, "chairs": 27, "cpus": 007}
Run Code Online (Sandbox Code Playgroud)
我正在尝试遍历字典并使用其相应的值打印键,在我的代码中,我得到的值太多而无法解压错误。
with open('myfile.json', "r") as myfile:
json_data = json.load(myfile)
for e, v in json_data:
for key, value in e.iteritem():
print key, value
Run Code Online (Sandbox Code Playgroud) 我有一个字典列表(a),我正在尝试遍历字典搜索特定值,如果字典有该值,则将该字典添加到新的字典集合中.到目前为止我有
newList = {}
a = [{"one": "green", "two" : "blue", "three" : "red"},
{"two": "blue", "four" : "green", "five" : "yellow"},
{"two": "blue", "six": "white", "seven" : "black"}]
for index in range(len(a)):
if a[index][?] = ["blue"]:
newList.append(a[index])
Run Code Online (Sandbox Code Playgroud)