辞書キーに基づいてグループを作成したいと思います。キーが複数の辞書にある場合は、後で使用できるグループを作成したいと思う。私はほとんどsuccededしかし、私は希望の出力を得ることができません。以下に示すように、2つの可能なグループがあります。これはdct1とdct3(同じキー18)とdct2とdct4(同じキー8)です。以下は私がこれまでに作ったものです。Pythonでdct.keysに基づいてグループを作成する方法
dct1 = {20: [(87, 6), (87, 7)],
21: [(68, 8)],
18: [(30, 7)],
11: [(27, 7), (28, 7)]}
dct2 = {8: [(41, 5), (41, 6), (41, 4)],
14: [(4, 7), (5, 7), (6, 7)],
16: [(58, 7), (56, 7), (57, 7)]}
dct3 = {4: [(41, 5), (41, 6), (41, 4)],
15: [(77, 7), (78, 7)],
18: [(29, 9), (29, 8)],
3: [(27, 7), (28, 7)]}
dct4 = {8: [(41, 5), (41, 6), (41, 4)],
30: [(6, 9), (5, 7), (7, 9)],
35: [(58, 7), (56, 7), (57, 7)]}
rwawl = [dct1, dct2, dct3, dct4]
def group_rooms(rectangles_with_adjacent_walls_list):
groups = []
for rectangle in rectangles_with_adjacent_walls_list:
adjacent_wall_list = rectangle.keys()
if not groups:
groups.append([adjacent_wall_list])
print adjacent_wall_list
new_group_threshold = len(adjacent_wall_list)
new_group = 0
for adjacent_wall in adjacent_wall_list:
for added_room in groups:
if adjacent_wall in added_room:
added_room.append(adjacent_wall_list)
break
else:
new_group += 1
if new_group == new_group_threshold:
groups.append([adjacent_wall_list])
print groups
return groups
created_groups = group_rooms(rwawl)
# MY OUTPUT:
# [[[18, 11, 20, 21]], [[18, 11, 20, 21]], [[18, 3, 4, 15]], [[8, 35, 30]]]
# DESIRED OUTPUT:
# [[[18, 11, 20, 21], [18, 3, 4, 15]], [[8, 16, 14], [8, 35, 30]]]
:これはのために、複数のdictsに発生するすべてのキーをグループを作成します。あなたの望む出力はどのように見えますか? – kofrasa
あなたのためにそれをチェックしました。それはリストの中に1つのリストを作成し、もう1つのリストに作成します。 – Hsin