python列表转json格式
天蓬老师
天蓬老师 2017-04-18 09:22:45
0
4
1380

例:list=['a','b'],['c','d'],...
例输出:{{'aa':'a','bb':'b'},{'cc':'c','dd':'d'},...}
需要输出为json格式,以及给value一个key。
怎么写好,谢谢了。

我猜测的提问
例如:

l = ['a', 'b'], ['c', 'd']

输出:

{
    "k1": [
        {"a": "a"},
        {"b": "b"}
    ],
    "k2": [
        {"c": "c"},
        {"d": "d"}
    ]
}
天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

membalas semua(4)
迷茫

Adakah ini kesan yang anda mahukan?

l=[['a','b'],['c','d']]
ret = []
for i in l:
    item = {}
    for j in i:
        item[j*2] = j
    ret.append(item)
        
print ret
import json
print json.dumps(ret)

[{'aa': 'a', 'bb': 'b'}, {'cc': 'c', 'dd': 'd'}]
[{"aa": " a", "bb": "b"}, {"cc": "c", "dd": "d"}]

Ty80
import json
list=['a','b'],['c','d']
result = [{value*2:value for value in i} for i in list]
print(json.dumps(result))  # '[{"aa": "a", "bb": "b"}, {"cc": "c", "dd": "d"}]'
伊谢尔伦
import json

def main(list_):
    return json.dumps([{'{key}{key}'.format(key=k): k for k in lst} for lst in (i for i in list_)])


if __name__ == '__main__':
    list_=['a','b'],['c','d']
    print main(list_)     # '[{"aa": "a", "bb": "b"}, {"cc": "c", "dd": "d"}]'
洪涛

Saya tidak faham niat anda, sila huraikan masalah dengan jelas

Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan