This topic created in 1903 days ago, the information mentioned may be changed or developed.
源代码:
[
{"userid":"Change","tags":[{"type":1,"tag_id":"e" }],},
{"userid":"andy.lei","tags":[{"type":1,"tag_id":"eg" },{"type":1,"tag_id":"ti"}],}
]
希望用 python 处理后变成:
[
{"userid":"change","tagid":"e"},{"userid":"andy.lei","tagid":"eg"},{"userid":"andy.lei","tagid":"ti"}
]
求实现方法
6 replies • 2021-03-10 12:27:29 +08:00
 |
|
1
wuwukai007 Mar 8, 2021 4
import pandas as pd from pandas.io.json import json_normalize json_normalize(a,'tags',['userid']).drop('type',axis=1).to_dict('records') 如果觉得有用,请务必回复我,不然我会伤心的😥
|
 |
|
3
DGideas Mar 9, 2021
上边的方法都不太好哇。。。
```python a = [ {"userid":"Change","tags":[{"type":1,"tag_id":"e" }],}, {"userid":"andy.lei","tags":[{"type":1,"tag_id":"eg" },{"type":1,"tag_id":"ti"}],} ]
result = [] [*map(lambda x: result.extend([{"userid": x["userid"], "tagid": tag["tag_id"]} for tag in x["tags"]]), a)] print(result) ```
|
 |
|
5
dll30 Mar 9, 2021
我愣是没看懂你想怎么转,给个说明呀
|
 |
|
6
cbiqih Mar 10, 2021
```python users = [ {"userid": "Change", "tags": [{"type": 1, "tag_id": "e"}], }, {"userid": "andy.lei", "tags": [{"type": 1, "tag_id": "eg"}, {"type": 1, "tag_id": "ti"}], } ]
result = [{'userid': user['userid'], 'tagid': tag['tag_id']} for user in users for tag in user['tags']] print(result) ```
|