Python - 'unicode'对象没有属性'values'

时间:2016-11-07 14:48:24

标签: python dictionary

我有dictionary

 playlists = {
    u'user1': {u'Make You Feel My Love': 1.0, u'I See Fire': 1.0, u'High And Dry': 1.0, u'Fake Plastic Trees': 1.0, u'One': 1.0, u'Goodbye My Lover': 1.0, u'No Surprises': 1.0}, 
    u'user2': {u'Fake Plastic Trees': 1.0, u'High And Dry': 1.0, u'No Surprises': 1.0}, 
    u'user3': {u'Codex': 1.0, u'No Surprises': 1.0, u'O': 1.0, u'Go It Alone': 1.0}, 
    u'user4': {u'No Distance Left To Run': 1.0, u'Running Up That Hill': 1.0, u'Fake Plastic Trees': 1.0, u'The Numbers': 1.0, u'No Surprises': 1.0}, u'user5': {u'Wild Wood': 1.0, u'You Do Something To Me': 1.0, u'Reprise': 1.0}}

我正在尝试抓取float values

使用:

print playlist.keys()[0]
print [p for p in playlist.values()[0]]

我成功了。

但是如果我尝试[p.values() for p in playlist.values()],我会收到以下错误:

AttributeError: 'unicode' object has no attribute 'values'

我如何获得values

1 个答案:

答案 0 :(得分:0)

您将dict定义为"播放列表" (复数)但在你以后的代码片段中,你处理一个名为"播放列表"的变量。 (单数)。 FWIW,使用原始playlists定义,您的列表组合完美地工作(或课程):

>>> playlists = {
...     u'user1': {u'Make You Feel My Love': 1.0, u'I See Fire': 1.0, u'High And Dry': 1.0, u'Fake Plastic Trees': 1.0, u'One': 1.0, u'Goodbye My Lover': 1.0, u'No Surprises': 1.0}, 
...     u'user2': {u'Fake Plastic Trees': 1.0, u'High And Dry': 1.0, u'No Surprises': 1.0}, 
...     u'user3': {u'Codex': 1.0, u'No Surprises': 1.0, u'O': 1.0, u'Go It Alone': 1.0}, 
...     u'user4': {u'No Distance Left To Run': 1.0, u'Running Up That Hill': 1.0, u'Fake Plastic Trees': 1.0, u'The Numbers': 1.0, u'No Surprises': 1.0}, u'user5': {u'Wild Wood': 1.0, u'You Do Something To Me': 1.0, u'Reprise': 1.0}}
>>> [p.values() for p in playlists.values()]
[[1.0, 1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0]]

IOW,请了解最新完整和可验证"例子意味着......