Sho*_*ded 0 python logic list-comprehension python-2.7
有人可以简化这段代码背后的逻辑:
scores=[(similarity(prefs,person,other),other)
for other in prefs if other!=person ]
Run Code Online (Sandbox Code Playgroud)
我尝试像这样实现它
for others in prefs:
if others!=person:
scores=[similarity(prefs,person, others),others]
Run Code Online (Sandbox Code Playgroud)
但它只挑选了其他人的最后一个元素.Btw prefs是一个2D字典,分数应该是一个元组列表.
这与将元组重复追加到列表中的情况相同:
scores = []
for others in prefs:
if others!=person:
scores.append((similarity(prefs, person, others), others))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
68 次 |
| 最近记录: |