使用列表理解 [英] Using list comprehension
问题描述
我有一个字符串,字典的形式如下:
('(笑声花)',
{ 笑声:(8.5,0.9313),
'flower':(7.88,1.1718),
''((4.98,0.9145),
'puppy':(7.58,1.4581 ),
'死':(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 d中的单词] string
返回总和(包含) / len(包括)
当我运行:
(笑声)(8.5,0.9313),花:
(7.88,1.1718), :(4.98,0.9145),'小狗':( 7.58,1.4581),
'死':( 1.56,1.198),笑:(9.5,0.1),'流动':(2.3,0.51) })
我应该只得到平均值笑声
和'flower'
: 8.5 + 7.88 / 2
但是这个运行功能还包括'laugh'
和'flow'
: 8.5 + 7.88 + 9.5 + 2.3 / 4
。
@Ignaco是正确的,为什么你包括流和笑...
你可以写代码如下:
data =('(笑声花),(8.5,0.9313),花:(7.88,1.1718),
''(4.98,0.9145),'小狗':( 7.58,1.4581)死亡':( 1.56,1.198),'笑':
(9.5,0.1),'flow':(2.3,0.51)})
#打包命名
键,vals = data
#假设()和第一个和最后一个
look_for = keys [1:-1] .lower()。split()
#获取相关数字
nums = [vals [k] [0] for k in look_for]
#打印平均值
打印总和(nums)/ len(nums)
所以你可以将功能概括为只是平均相关键的第一个元素:
def somefunc(keys,dct):
vals = [dct [k] [0] for k in keys]
return sum(vals )/ float(len(vals))
您必须以某种方式预处理某些字符串,以便它是一系列有效的键:
some_string ='(笑声花)'
keys = some_string [1:-1] .lower()。split()
打印somefunc(keys,some_dict)
I have a string, dictionary in the form:
('(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)
}
)
Each parentheses is a tuple which corresponds to (score, standard deviation). I'm taking the average of just the first integer in each tuple. I've tried this:
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)
When I run:
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)})
I should get the average of only 'laughter'
and 'flower'
: 8.5 + 7.88 / 2
but this running function also includes 'laugh'
and 'flow'
: 8.5 + 7.88 + 9.5 + 2.3 /4
.
@Ignaco is right about why you're including "flow" and "laugh"...
You could write the code as the following though:
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)
so you generalise the function to just average the first element of relevant keys:
def somefunc(keys, dct):
vals = [dct[k][0] for k in keys]
return sum(vals) / float(len(vals))
And you have to pre-process some string somehow, so that it's a sequence of valid keys:
some_string = '(laughter flower)'
keys = some_string[1:-1].lower().split()
print somefunc(keys, some_dict)
这篇关于使用列表理解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!