【问题标题】:python list of (str,int) tuple dictionaries [closed](str,int)元组字典的python列表[关闭]
【发布时间】:2014-03-31 05:00:12
【问题描述】:

我正在尝试返回(str, int) 元组的列表,它是元组列表中给定人的朋友推荐,其中每个元组的第一个元素是潜在朋友的姓名(与字典键的格式相同),第二个元素是潜在朋友的分数。只有分数不为零的潜在朋友才应包含在列表中。

这里是这个函数的返回值格式示例:

[('Gloria Pritchett', 2),
 ('Manny Delgado', 1),
 ('Cameron Tucker', 1),
 ('Luke Dunphy', 3)]

对于每个人,社交网络中他们当前不是朋友的所有人都是潜在朋友。对于特定的人,每个潜在的朋友都使用以下积分系统进行评分: 对于此人和潜在朋友的每个共同朋友,潜在朋友的分数加 1 分 对于该人和潜在朋友都属于的每个网络,在潜在朋友的分数上加 1 分 如果此人与潜在朋友的姓氏相同,则在潜在朋友的分数上加 1 分,但前提是他们有其他共同点(共同朋友、共同网络或两者兼有)。 这就是我所做的:

这是我的两个工作正常的功能: 第一个函数返回 key:names value:friends'names 的字典 第二个函数返回键的字典:名称值:网络

def make_recommendations 出现错误。我不知道是什么问题..请帮助我。

【问题讨论】:

  • 上一行的开括号太多?
  • person_dict=person_to_friends(profiles_file) 五个错误..
  • 我很困惑。您有一个名为 person_to_friends 的函数,但您也将它作为参数传递给 make_recommendations。我感觉你的错误在于这些名字的冲突。
  • 与其删除信息,不如提供more?您收到的确切错误消息是什么?

标签: python dictionary tuples


【解决方案1】:

我不确定这是否正在做你认为它正在做的事情:

for key in person_to_friends or person_to_networks:

你可以试试这个看看它到底在做什么:

for x in [1,2,3] or [4,5,6]:
    print x

这实际上是在说:

for value in (first list if it's not empty otherwise second list)

如果你想使用 both 列表中的值,你应该使用itertools.chain

import itertools
for x in itertools.chain([1,2,3], [4,5,6]):
    print x

你犯了类似的错误:

if freind in person_to_friends[profiles_file] or person_to_networks[profiles_file]:

请注意freind中的拼写错误。这可能会给您带来错误。此外,profiles_file 未在此函数中的任何位置定义,它是否在全局范围内?)您可能意思是:

if friend in person_to_friends[profiles_file] or friend in person_to_networks[profiles_file]:

这由 Python 评估为:

if (value in first sequence) OR (second sequence is not empty)

另外值得注意的是,在person_to_friends,你有:

name.update({lst[0]:lst[1:]})

虽然这在技术上是正确的,但它比传统的开销(在理解和处理方面)要多得多:

name[lst[0]] = lst[1:]

【讨论】:

    【解决方案2】:

    我看到这个问题有点复杂,包含很多交叉点,建议你把解决方案简化,分步:

    例如:

    1. 查找好友score的函数(返回元组列表):

      def friendsMu(person):
          result = []
          friends = person_to_friends[person]
          for key in person_to_friends:
              if key != person:
                  friends2 = person_to_friends[key]
                  intersect = list(set(friends) & set(friends2))
                  p = len(intersect)
                  if p != 0:
                      t = (key, p)
                      result.append(t)
          return result
      
    2. 查找人的网络score的函数(返回元组列表):

      def networkMu(person):
          result = []
          friends = person_to_networks[person]
          for key in person_to_networks:
              if key != person:
                  friends2 = person_to_networks[key]
                  intersect = list(set(friends) & set(friends2))
                  p = len(intersect)
                  if p != 0:
                      t = (key, p)
                      result.append(t)
          return result
      
    3. 函数为all persons计算前面函数的结果(返回字典:key=namevalue=list of tuples):

      def allf();
          ddict = {}
          for key in person_to_friends:
              d1 = friendsMu(key)
              if d1 != ():
                  ddict[key] = d1
          return ddict
      
      def alln():
          ndict = {}
          for key in person_to_networks:
              d1 = networkMu(key)
              if d1 != ():
                  ndict[key] = d1
          return ndict
      
    4. 函数merge最终结果:

      from collections import Counter
      def mrg(ddict, ndict):
          merged = Counter(ddict)
          merged.update(ndict)
          return merged
      

    输出如下所示:

     print allf()
     allf() =  {'Jay Pritchett': [('Manny Delgado', 1), ('Cameron Tucker', 1), ('Gloria Pritchett', 1), ('Luke Dunphy', 1)], 'Claire Dunphy': [('Gloria Pritchett', 1), ('Luke Dunphy', 1)], 'Manny Delgado': [('Jay Pritchett', 1), ('Mitchell Pritchett', 1), ('Alex Dunphy', 1), ('Cameron Tucker', 1)], 'Mitchell Pritchett': [('Manny Delgado', 1), ('Phil Dunphy', 1), ('Alex Dunphy', 1)], 'Alex Dunphy': [('Manny Delgado', 1), ('Mitchell Pritchett', 1)], 'Cameron Tucker': [('Jay Pritchett', 1), ('Manny Delgado', 1), ('Luke Dunphy', 1)], 'Haley Gwendolyn Dunphy': [], 'Phil Dunphy': [('Mitchell Pritchett', 1)], 'Dylan D-Money': [], 'Gloria Pritchett': [('Jay Pritchett', 1), ('Claire Dunphy', 1), ('Luke Dunphy', 1)], 'Luke Dunphy': [('Jay Pritchett', 1), ('Claire Dunphy', 1), ('Cameron Tucker', 1), ('Gloria Pritchett', 1)]}
    
     print alln()
     alln() =  {'Phil Dunphy': [], 'Claire Dunphy': [('Gloria Pritchett', 1)], 'Manny Delgado': [('Alex Dunphy', 1)], 'Mitchell Pritchett': [], 'Alex Dunphy': [('Manny Delgado', 1)], 'Cameron Tucker': [], 'Gloria Pritchett': [('Claire Dunphy', 1)]}
    
    merged = mrg(allf(),alln())
    for i in merged:                                                                 
        print i, merged[i]
    merged = 
    Jay Pritchett [('Manny Delgado', 1), ('Cameron Tucker', 1), ('Gloria Pritchett', 1), ('Luke Dunphy', 1)]
    Claire Dunphy [('Gloria Pritchett', 1), ('Luke Dunphy', 1), ('Gloria Pritchett', 1)]
    Manny Delgado [('Jay Pritchett', 1), ('Mitchell Pritchett', 1), ('Alex Dunphy', 1), ('Cameron Tucker', 1), ('Alex Dunphy', 1)]
    Mitchell Pritchett [('Manny Delgado', 1), ('Phil Dunphy', 1), ('Alex Dunphy', 1)]
    Alex Dunphy [('Manny Delgado', 1), ('Mitchell Pritchett', 1), ('Manny Delgado', 1)]
    Cameron Tucker [('Jay Pritchett', 1), ('Manny Delgado', 1), ('Luke Dunphy', 1)]
    Haley Gwendolyn Dunphy []
    Phil Dunphy [('Mitchell Pritchett', 1)]
    Dylan D-Money []
    Gloria Pritchett [('Jay Pritchett', 1), ('Claire Dunphy', 1), ('Luke Dunphy', 1), ('Claire Dunphy', 1)]
    Luke Dunphy [('Jay Pritchett', 1), ('Claire Dunphy', 1), ('Cameron Tucker', 1), ('Gloria Pritchett', 1)]
    

    希望这会有所帮助,如果不能准确地为您提供您所要求的东西,那么可以为您提供一些指导。 祝你好运。

    【讨论】:

      猜你喜欢
      • 2013-04-08
      • 2016-03-15
      • 2015-07-27
      • 2021-11-12
      • 2014-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多