Python 在字典列表中打印条件语句 这是普通文本。您好,我需要解决一个字典和条件语句列表的问题,但是我的代码没有给我输出,也没有错误消息。这就是问题所在:

Python 在字典列表中打印条件语句 这是普通文本。您好,我需要解决一个字典和条件语句列表的问题,但是我的代码没有给我输出,也没有错误消息。这就是问题所在:,python,dictionary,conditional-statements,Python,Dictionary,Conditional Statements,定义名为“dataset”的变量,该变量包含以下信息: Artist Track Plays Dire Straits Sultans of Swing 65 Pink Floyd Comfortably Numb 23 The Smiths Cemetery Gates 15 Nick Cave & The Bad Seeds The Ship Song 36 Dire Straits Brothers in Arms 20 打印少于

定义名为“dataset”的变量,该变量包含以下信息:

Artist  Track   Plays
Dire Straits    Sultans of Swing    65
Pink Floyd  Comfortably Numb    23
The Smiths  Cemetery Gates  15
Nick Cave & The Bad Seeds   The Ship Song   36
Dire Straits    Brothers in Arms    20
打印少于25首曲目的艺术家和曲目(标题)。每个艺术家和曲目组合打印在单独的行上

打印Smiths和Pink Floyd歌曲的所有曲目标题。只打印标题是可以的,每个标题都打印在单独的一行上

打印播放的总数。这是所有5首歌曲的播放总数

到目前为止,我的代码是:

在此处输入代码

  dataset = [
  {'Artist':'Dire Straits','Track':'Sultans of Swing','Plays':65},
  {'Artist':'Pink Floyd','Track':'Comfortably Numb','Plays':23},
  {'Artist':'The Smiths','Track':'Cemetry Gates','Plays':15},
  {'Artist':'Nick Cave & The Bad Seeds','Track':'The Ship Song','Plays':36},
  {'Artist':'Dire Straits','Track':'Brothers in Arms','Plays':20},
]


“”“

您的代码中有错误,您应该访问在迭代中声明的x变量,而不是询问数据集上是否有“Plays”属性

试试这个,它应该可以工作:

for item in dataset:
    if item['Plays'] < 25:
        print(f"Artist: {item['Artist']} \n Track: {item['Track']}")
数据集中的项的
:
如果项目['Plays']<25:
打印(f“艺术家:{item['Artist']}\n曲目:{item['Track']}”)
for item in dataset:
    if item['Plays'] < 25:
        print(f"Artist: {item['Artist']} \n Track: {item['Track']}")