2

我有一个字符串,字典形式为:

('(Laughter flower)',
 {'laughter': (8.5, 0.9313),
  'flower': (7.88, 1.1718),
  'the': (4.98, 0.9145),
  'puppy': (7.58, 1.4581),
  'died': (1.56, 1.198),
  'laugh': (9.5, 0.1),
  'flow': (2.3, 0.51)
 }
)

每个括号是对应于(分数,标准差)的元组。我只取每个元组中第一个整数的平均值。我试过这个:

def score(string, d):
    if len(string) == 0:
        return 0
    string = string.lower()
    included = [d[word][0]for word in d if word in string]
    return sum(included) / len(included)

当我运行时:

print score ('(Laughter flower)', {'laughter': (8.5, 0.9313), 'flower': 
(7.88, 1.1718), 'the':(4.98, 0.9145), 'puppy':(7.58, 1.4581), 
'died':(1.56, 1.198),'laugh': (9.5, 0.1),'flow': (2.3, 0.51)})

我应该只得到'laughter'and 'flower':8.5 + 7.88 / 2但这个运行函数还包括'laugh'and 'flow':的平均值8.5 + 7.88 + 9.5 + 2.3 /4

4

3 回答 3

2

@Ignaco 关于你为什么要包括“流”和“笑”是正确的......

您可以编写如下代码:

data = ('(Laughter flower)', {'laughter': (8.5, 0.9313), 'flower': (7.88, 1.1718), 
'the':(4.98, 0.9145), 'puppy':(7.58, 1.4581), 'died':(1.56, 1.198), 'laugh': 
(9.5, 0.1),'flow': (2.3, 0.51)})

# Unpack for naming
keys, vals = data
# Assume () and first and last
look_for = keys[1:-1].lower().split()
# Get relevant numbers
nums = [vals[k][0] for k in look_for]
# Print average
print sum(nums) / len(nums)

因此,您将函数概括为仅平均相关键的第一个元素:

def somefunc(keys, dct):
    vals = [dct[k][0] for k in keys]
    return sum(vals) / float(len(vals))

而且您必须以某种方式预处理一些字符串,以便它是一系列有效键:

some_string = '(laughter flower)'
keys = some_string[1:-1].lower().split()
print somefunc(keys, some_dict)
于 2012-10-22T07:12:45.130 回答
1

像这样的东西:

In [65]: lis=('(Laughter flower)', {'laughter': (8.5, 0.9313), 'flower': (7.88, 1.1718), 
'the':(4.98, 0.9145), 'puppy':(7.58, 1.4581), 'died':(1.56, 1.198), 'laugh': 
(9.5, 0.1),'flow': (2.3, 0.51)})

In [68]: strs=lis[0].strip('()').split() # returns ['Laughter', 'flower']

In [69]: lis1=[lis[1][x][0] for x in lis[1] if x in map(str.lower,strs)]

In [70]: sum(lis1)/float(len(lis1))
Out[70]: 8.1899999999999995
于 2012-10-22T07:03:41.710 回答
0
def score(string,d):
    if string=="":
        return 0
    string=string.lower().split()
    included=[d[word][0] for word in d if word in string]
    return(sum(included)/len(included))

你的字符串='(笑花)'

它是一个字符串而不是两个不同的单词,因此当您将 [d[word][0] for word in d if word in string]应用时,它没有得到该单词。因此,如果您不在字符串周围使用 () 括号,这将很容易。而是使用“笑花”。但它仍然是一个字符串而不是两个单词,所以你必须拆分它string.split()它将创建一个包含两个单词的列表,然后你的函数将起作用。

于 2012-10-27T04:45:48.510 回答