以更优雅的方式打印循环字典

Print circular dictionary in more elegant way

我正在阅读 Jeol Grus 的 "Data Science from Scratch",这是我大学数据结构课程 20 多年后的事。被他编写代码的优雅方式所吸引。在第 1 章的示例中,我对将自己的字典附加到自身的数据结构感兴趣 - 'users'。当我试图通过打印 'users' 的内容来了解​​它在字典中的工作方式时,它会打印出该字典的无限循环。更神奇的是,Python 没有错误。但是我还是不知道里面的内容是什么

搜索了一些文章,在 Quora 上找到了一篇 How do I nicely print a nested dictionary in Python?,看懂了内容。尽管我开始考虑是否可以用更简洁的样式打印出内容,如下所示,使用指向字典或类似内容的指针,而不是重复循环。可能吗?

{"id": 0, "name": "Hero", "friend": [*users[1], *users[2]] }
{"id": 1, "name": "Dunn", "friend": [*users[2], *users[3]] }

Data Science from Scratch 示例

users = [
        { "id": 0, "name": "Hero"},
        { "id": 1, "name": "Dunn"},
        { "id": 2, "name": "Sue"},
        { "id": 3, "name": "Chi"},
        { "id": 4, "name": "Thor"},
        { "id": 5, "name": "Clive"},
        { "id": 6, "name": "Hicks"},
        { "id": 7, "name": "Devin"},
        { "id": 8, "name": "Kate"},
        { "id": 9, "name": "Klein"}
]
friendship = [(0,1), (0,2), (1,2), (1,3), (2,3), (3,4),
              (4,5), (5,6), (5,7), (6,8), (7,8), (8,9)]

for user in users :
    user["friends"] = []

for i, j in friendship :
    users[i]["friends"].append(users[j])
    users[j]["friends"].append(users[i])

def number_of_friends(user) :
    """ how many friends does _user_ have """
    return(len(user["friends"]))

total_connection = sum(number_of_friends(user)
                       for user in users)
num_users = len(users)
avg_connection = total_connection / num_users

# create a list (user_id, number_of_friends)
num_friends_by_id = [(user['id'], number_of_friends(user))
                      for user in users]

打印出来

{'id': 0, 'name': 'Hero', 'friends': [{'id': 1, 'name': 'Dunn', 'friends': [{...}, {'id': 2, 'name': 'Sue', 'friends': [{...}, {...}, {'id': 3, 'name': 'Chi', 'friends': [{...}, {...}, {'id': 4, 'name': 'Thor', 'friends': [{...}, {'id': 5, 'name': 'Clive', 'friends': [{...}, {'id': 6, 'name': 'Hicks', 'friends': [{...}, {'id': 8, 'name': 'Kate', 'friends': [{...}, {'id': 7, 'name': 'Devin', 'friends': [{...}, {...}]}, {'id': 9, 'name': 'Klein', 'friends': [{...}]}]}]}, {'id': 7, 'name': 'Devin', 'friends': [{...}, {'id': 8, 'name': 'Kate', 'friends': [{'id': 6, 'name': 'Hicks', 'friends': [{...}, {...}]}, {...}, {'id': 9, 'name': 'Klein', 'friends': [{...}]}]}]}]}]}]}]}, {'id': 3, 'name': 'Chi', 'friends': [{...}, {'id': 2, 'name': 'Sue', 'friends': [{...}, {...}, {...}]}, {'id': 4, 'name': 'Thor', 'friends': [{...}, {'id': 5, 'name': 'Clive', 'friends': [{...}, {'id': 6, 'name': 'Hicks', 'friends': [{...}, {'id': 8, 'name': 'Kate', 'friends': [{...}, {'id': 7, 'name': 'Devin', 'friends': [{...}, {...}]}, {'id': 9, 'name': 'Klein', 'friends': [{...}]}]}]}, {'id': 7, 'name': 'Devin', 'friends': [{...}, {'id': 8, 'name': 'Kate', 'friends': [{'id': 6, 'name': 'Hicks', 'friends': [{...}, {...}]}, {...}, {'id': 9, 'name': 'Klein', 'friends': [{...}]}]}]}]}]}]}]}, {'id': 2, 'name': 'Sue', 'friends': [{...}, {'id': 1, 'name': 'Dunn', 'friends': [{...}, {...}, {'id': 3, 'name': 'Chi', 'friends': [{...}, {...}, {'id': 4, 'name': 'Thor', 'friends': [{...}, {'id': 5, 'name': 'Clive', 'friends': [{...}, {'id': 6, 'name': 'Hicks', 'friends': [{...}, {'id': 8, 'name': 'Kate', 'friends': [{...}, {'id': 7, 'name': 'Devin', 'friends': [{...}, {...}]}, {'id': 9, 'name': 'Klein', 'friends': [{...}]}]}]}, {'id': 7, 'name': 'Devin', 'friends': [{...}, {'id': 8, 'name': 'Kate', 'friends': [{'id': 6, 'name': 'Hicks', 'friends': [{...}, {...}]}, {...}, {'id': 9, 'name': 'Klein', 'friends': [{...}]}]}]}]}]}]}]}, {'id': 3, 'name': 'Chi', 'friends': [{'id': 1, 'name': 'Dunn', 'friends': [{...}, {...}, {...}]}, {...}, {'id': 4, 'name': 'Thor', 'friends': [{...}, {'id': 5, 'name': 'Clive', 'friends': [{...}, {'id': 6, 'name': 'Hicks', 'friends': [{...}, {'id': 8, 'name': 'Kate', 'friends': [{...}, {'id': 7, 'name': 'Devin', 'friends': [{...}, {...}]}, {'id': 9, 'name': 'Klein', 'friends': [{...}]}]}]}, {'id': 7, 'name': 'Devin', 'friends': [{...}, {'id': 8, 'name': 'Kate', 'friends': [{'id': 6, 'name': 'Hicks', 'friends': [{...}, {...}]}, {...}, {'id': 9, 'name': 'Klein', 'friends': [{...}]}]}]}]}]}]}]}]}

尝试使用 类 而不是听写。

class Person:
    def __init__(self, id, name):
        self.id = id
        self.name = name
        self.friends = []

    def __str__(self):
        return "*users[" + str(id) + "]"