【问题标题】:The best suggestion for a structure to store multiple keys and values - dictionaries?存储多个键和值的结构的最佳建议 - 字典?
【发布时间】:2017-07-05 10:42:57
【问题描述】:

其目的是创建一个 facebook 类型程序的开头(用于教学目的),该程序存储个人姓名列表及其生物详细信息。

我有两个问题,一个接下一个:

问题 1:如何获取作为字典中键值对中值的一部分的列表值。例如,要找出 John 和 Mary 的朋友有哪些共同点,在本例中为 friend1 和friend3

问题 2: 创建存储姓名、性别、爱好和朋友的结构的最佳方法是什么?这会是一本字典吗?如果是,如何定义?如果没有,人们有什么建议?

#create a dictionary that stores names, and a list of friends
facebook_profile={"John":["friend1","friend2","friend3","friend4"],"Mary":["friend1","friend7","friend3","friend9"]}
print(facebook_profile)

需要存储并随后打印以下示例数据:

Name:John
Gender: Male
Hobbies: Chess
Friends: friend1,friend2,friend3,friend4

Name: Mary
Gender: Female
Hobbies: Chequers
Friends: friend1,friend2,friend3,friend4    

我知道最好的解决方案是数据库并使用某种文件处理来实现它但是,出于教学目的,我们尝试仅使用列表或字典。然后可以将这些字典/列表写入文件,但我正在寻找的解决方案/答案在理想情况下必须仅使用列表和字典结构。

【问题讨论】:

    标签: python facebook dictionary structure profile


    【解决方案1】:

    另一种方法是在数据库中存储表列和与该表具有多对多关系的对象。

    【讨论】:

    • 啊 - 没有数据库。如前所述,这是出于教学目的,必须仅使用基于列表和字典的解决方案(无文件)。不过谢谢你的建议
    • 啊 dict 的问题是你的 facebook 上只有一个同名的人 ;) 。因为 dict 不允许重复键。
    • 好的 - 那是键 - 字典不允许重复键。那么字典可以有一个链接到另一个对象的值(如列表)
    【解决方案2】:

    对于问题 1,集合是快速轻松地计算交点的好选择。 对于问题 2,字典效果很好。

    例如:

    facebook_profile={
        "John":{"friends":{"friend1","friend2","friend3","friend4"},"Gender": "Male"},
        "Mary":{"friends":{"friend1","friend7","friend3","friend9"},"Gender": "Female"}
    }
    mutual_friends = facebook_profile["John"]["friends"].intersection(facebook_profile["Mary"]["friends"])
    print (mutual_friends)
    

    给出输出:

    {'friend1', 'friend3'}
    

    【讨论】:

    • 在这种情况下,您能否简要解释一下集合和字典之间的区别是什么,以强调为什么一个比另一个更有用
    【解决方案3】:

    创建一个类:

    class Person:
    
      def __init__(self, name, gender, hobbies, friends):
        self.name = name
        self.gender = gender
        self.hobbies = hobbies
        self.friends = friends
    
      def getMutualFriends(self, personB):
        return list(set(personB.friends).intersection(self.friends))
    
    person1 = Person('John', 'male', ['Chess'], ['friend1', 'friend2'])
    person2 = Person('Anna', 'female', ['Soccer'], ['friend1', 'friend3'])
    
    print(person1.getMutualFriends(person2))
    

    【讨论】:

      【解决方案4】:

      编辑您编辑了答案以声明您只想要列表和字典,所以也许这不适合您,但是,类是实现有关类似重复对象的复杂信息存储的最佳方式很多功能,所以我就把它留在这里

      随着这变得越来越复杂,嵌套字典可能会让人头疼,我建议你定义一个class,如下所示:

      class Person()
          def __init__(self, name, gender):
              self.name = name
              self.gender = gender
      

      对于那个类,你可以定义各种方法,比如寻找共同的朋友:

      class Person()
          def __init__(self, name, gender):
              self.name = name
              self.gender = gender
              self.friends = []
              self.hobbies = []
          def add_friends(self, list_of_friends):
              self.friends += list_of_friends
          def add_hobbies(self, list_of_hobbies):
              self.hobbies += list_of_hobbies
          def mutual_friends(self, another_person):
              return list(set(another_person.friends).intersection(self.friends))
      

      然后你要做的就是初始化每个朋友并开始运行各种方法

      john = Person('John', 'Male')
      john.add_friends(['friend1', 'friend2', ...]
      mary = Person('Mary', 'Female')
      mary.add_friends(['friend3', 'friend7', ...]
      common_friends = john.mutual_friends(mary)
      print(common_friends) # Will print a list of mutual friends
      

      正如其他人所说,对于长列表的朋友,更有效的方法是使用setintersection

      【讨论】:

      • 我认为学生会发现这非常有用 - 讨论最佳前进方向(这是问题 2),所以谢谢 - 是否可以在适当的地方评论每一行(定义类和什么是“自我”等等......(这将有助于新手)
      猜你喜欢
      • 2013-12-27
      • 2015-09-24
      • 1970-01-01
      • 1970-01-01
      • 2013-01-14
      • 2015-02-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多