我需要使用python json解析将我的2个列表组合成另一个1
我有2个json文件: rooms.json students.json 我需要通过students.json中的room id和属性room将它们划分到另一个列表中 rooms.json:我需要使用python json解析将我的2个列表组合成另一个1,python,json,parsing,Python,Json,Parsing,我有2个json文件: rooms.json students.json 我需要通过students.json中的room id和属性room将它们划分到另一个列表中 rooms.json: [ { "id": 0, "name": "Room #0" }, { "id": 1, "name": "Room #1" }, { "id": 2, "name": "Room #2" } ] students.json: [ { "id": 0
[
{
"id": 0,
"name": "Room #0"
},
{
"id": 1,
"name": "Room #1"
},
{
"id": 2,
"name": "Room #2"
}
]
students.json:
[
{
"id": 0,
"name": "Ryan Keller",
"room": 1
},
{
"id": 1,
"name": "Brooke Ferrell",
"room": 1
},
{
"id": 2,
"name": "Travis Tran",
"room": 0
}
]
我在用这个:
import json
rooms_file = 'rooms.json'
with open(rooms_file) as f:
rooms_new = json.load(f)
students_file = 'students.json'
with open(students_file) as file:
student_new = json.load(file)
relocation_file = 'relocation.json'
for i in students_file:
if "room" == id in rooms_file:
with open(relocation_file) as fl:
relocation_new = json.dump(relocation_file,'w')
我做错了什么,请帮帮我。
它不返回任何内容,但可能应该创建一个文件“relocation.json”
其中包含如下列表:
[
{
"id": 0,
"name": "Room #0"
"name": "Travis Tran",
},
{
"id": 1,
"name": "Room #1"
"name": "Brooke Ferrell",
"name": "Ryan Keller",
},
]
你能试试下面的代码吗?我想你期望的答案不应该有相同的键名,所以我将其重命名为
学生名
,作为键(如果需要,你可以将名称
更改为房间名
)。我希望这对你也很好
import json
with open('files/rooms.json') as f:
room_list = json.load(f)
with open('files/students.json') as file:
student_list = json.load(file)
relocation_list = []
for student_dict in student_list:
for room_dict in room_list:
new_dict = {}
if student_dict['room'] == room_dict['id']:
new_dict["student_name"] = student_dict['name']
new_dict.update(room_dict)
relocation_list.append(new_dict)
with open('relocation.json', 'w') as f:
f.write(str(relocation_list))
print(relocation_list)
输出:
[{'student_name': 'Ryan Keller', 'id': 2, 'name': 'Room #2'}, {'student_name': 'Brooke Ferrell', 'id': 1, 'name': 'Room #1'}, {'student_name': 'Travis Tran', 'id': 0, 'name': 'Room #0'}]
使用列出全面的方式:
import json
with open('files/rooms.json') as f:
room_list = json.load(f)
with open('files/students.json') as file:
student_list = json.load(file)
print([{**room_dict, "student_name": student_dict['name']} for room_dict in room_list for student_dict in student_list if student_dict['room'] == room_dict['id']])
# output
# [{'id': 0, 'name': 'Room #0', 'student_name': 'Travis Tran'}, {'id': 1, 'name': 'Room #1', 'student_name': 'Brooke Ferrell'}, {'id': 2, 'name': 'Room #2', 'student_name': 'Ryan Keller'}]
编辑 对于评论中提到的新要求
- 避免在
中重复ID-这里我们将重新定位\u列表
的值合并到学生列表中学生名
- 按值对dict列表进行排序-Ref:检查此项还有许多其他方法
您的输出似乎与您尝试执行的操作不匹配。Ryan Keller在顶部的房间值为2,但在输出中的房间值为1。这是需要的吗?请清理您的输出文件,或者更清楚地了解您想要的逻辑。抱歉,现在它需要尝试,并且它输出以下内容:[{'id':5,'name':'Room#5','student#name':'Cassandra Wilson'},{'id':5,'name':'Room#5','student#name':'Cassandra Wilson'},我需要为10000名学生和1000个房间做这个,但是现在我尝试了10名学生和10个房间,尝试你的代码更简单很好,谢谢你的帮助,但效果不是很好)很抱歉,你能现在检查一下吗?我已经更正了代码。谢谢你,你帮了我这么多,我能请你帮我把它按1 t排序吗例如,离开3号房间的学生在一本字典中,比如[{'student_name':'Ryan Keller','student_name':'Brooke Ferrell','id':0,'name':'room#0'}当然,所以你的第一个请求是你想要最终结果(
重新定位列表
)要按房间编号排序吗?第二部分我不太明白,你能不能用另一个例子再解释一遍,这样我现在就可以快速尝试了?
import json
with open('files/rooms.json') as f:
room_list = json.load(f)
with open('files/students.json') as file:
student_list = json.load(file)
relocation_list = []
# We arent considering about duplicate item as it is created by same loop
def check_room_id_in_relocation_list(id_to_check):
is_exist, index, item = False, None, {}
for index, item in enumerate(relocation_list):
if id_to_check == item['id']:
is_exist, index, item = True, index, item
return is_exist, index, item
for student_dict in student_list:
for room_dict in room_list:
new_dict = {}
if student_dict['room'] == room_dict['id']:
is_room_exist, index, item = check_room_id_in_relocation_list(room_dict['id'])
if is_room_exist:
# To merge same ids
# since it already exist so update list's item with new student_name
# instead of new key we are assigning list of names to key "student_name" to
relocation_list[index]['student_name'] = [relocation_list[index]['student_name'], 'new nameeeee']
else:
new_dict["student_name"] = student_dict['name']
new_dict.update(room_dict)
relocation_list.append(new_dict)
print("student list:", student_list)
print("room list: ", room_list)
print("relocation_list:", sorted(relocation_list, key=lambda k: k['id']))
with open('relocation.json', 'w') as f:
f.write(str(sorted(relocation_list, key=lambda k: k['id'])))
# output
# student list: [{'id': 0, 'name': 'Ryan Keller', 'room': 2}, {'id': 1, 'name': 'Brooke Ferrell', 'room': 1}, {'id': 2, 'name': 'Travis Tran', 'room': 0}, {'id': 3, 'name': 'New User', 'room': 2}]
# room list: [{'id': 0, 'name': 'Room #0'}, {'id': 1, 'name': 'Room #1'}, {'id': 2, 'name': 'Room #2'}]
# relocation_list: [{'student_name': ['Travis Tran', 'new nameeeee'], 'id': 0, 'name': 'Room #0'}, {'student_name': 'Brooke Ferrell', 'id': 1, 'name': 'Room #1'}, {'student_name': 'Ryan Keller', 'id': 2, 'name': 'Room #2'}]