(str,int)元组词典的python列表 [英] python list of (str,int) tuple dictionaries
问题描述
我正在尝试返回(str,int)
元组的列表,这是朋友向列表中的给定人员推荐的每个元组的第一个元素元组是潜在的朋友的名字(与字典键相同的格式),第二个元素是潜在的朋友的得分。只有具有非零分数的潜在朋友才能包含在列表中。
I am trying to return list of (str, int)
tuple, which is the friend recommendations for the given person in a list of tuples where the first element of each tuple is a potential friend's name (in the same format as the dictionary keys) and the second element is that potential friend's score. Only potential friends with non-zero scores should be included in the list.
下面是此函数的返回值的格式示例:
Here is an example of the format of the return value for this function:
[('Gloria Pritchett', 2),
('Manny Delgado', 1),
('Cameron Tucker', 1),
('Luke Dunphy', 3)]
对于每个人社交网络中的所有人,他们当前不是朋友,都是潜在的朋友。对于特定的人,每个潜在的朋友使用以下点系统得分:
对于该人和潜在朋友拥有的每个共同的朋友,添加1点到潜在朋友的分数
对于每个网络,个人和潜在的朋友都属于,添加1点潜在的朋友的分数
如果该人与潜在朋友具有相同的姓氏,则添加1点到潜在朋友的得分,但只有当他们有其他东西时共同的(共同的朋友,互联网或两者)。
这就是我所做的:
For each person, all people in the social network who they are not currently friends with are potential friends. For a particular person, each potential friend is scored using the following point system: For every mutual friend that the person and the potential friend have, add 1 point to the potential friend's score For each network that the person and the potential friend both belong to, add 1 point to the potential friend's score If the person has the same last name as the potential friend, add 1 point to the potential friend's score, but only if they have something else in common (mutual friend(s), mutual network(s) or both). This is what I did:
这些是我的两个功能,工作正常:
第一个函数返回键的字典:名称值:朋友'name
第二个函数返回键的字典:名称值:网络
and these are my two functions which works fine: first function returns dictionary of key:names value:friends' names second function returns dictionary of key:names value: networks
我收到的错误def make_recommendations
。我没有任何线索是什么问题..请帮助我。
I get an error for def make_recommendations
. I don't have any clue what is the problem .. Please help me.
推荐答案
我不知道这是否在做你认为的事情:
I'm not sure if this is doing what you think it's doing:
for key in person_to_friends or person_to_networks:
您可以通过尝试以下方式了解其真正的作用:
You can see what it's really doing by trying this:
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)
如果要使用 列表中的值,您应该使用 itertools.chain
If you want to use the values in both lists, you should use 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
,是否在全局范围内?)您可能意味着:
(Note the typo in freind
. This is probably giving you your error. Also profiles_file
isn't defined anywhere in this function, is it in the global scope?) You probably mean:
if friend in person_to_friends[profiles_file] or friend in person_to_networks[profiles_file]:
这是由Python评估为:
This is evaluated by Python as:
if (value in first sequence) OR (second sequence is not empty)
另请注意,在 person_to_friends
,你有:
Also of note, in person_to_friends
, you have:
name.update({lst[0]:lst[1:]})
虽然这在技术上是正确的,但它的开销更多(在理解和处理中)比传统的:
While this is technically correct, it's a lot more overhead (in comprehension as well as in processing) than the traditional:
name[lst[0]] = lst[1:]
这篇关于(str,int)元组词典的python列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!