简体   繁体   English

检查列表中字典中的键值?

[英]Checking the value of a key in a dictionary within a list?

So I haven't exactly used Python in a while and I'm rusty - I'm working on a personal project and the API I'm using returns a list of dictionaries that represent a song's data. 因此,我有一段时间没有完全使用Python了,我感到很生疏-我正在开发一个个人项目,使用的API返回了代表歌曲数据的词典列表。 What I'm trying to do is essentially trim down the returned list of songs to ONLY those with a rating of 5 (and to delete all others from the list). 我想要做的基本上是将返回的歌曲列表减少到只有5级的歌曲(并从列表中删除所有其他歌曲)。 I have some code below that doesn't seem to be working. 我下面有一些代码似乎不起作用。 It doesn't seem to actually remove anything, and prints out the entire list of songs (which in my case gets pretty large, as it is around 11,000 songs or so). 它似乎并没有真正删除任何内容,而是打印出整个歌曲列表(在我看来,这是相当大的,因为大约有11,000首歌曲左右)。

As a bit of help, I'll also post an example of what the API returns (for one song): 作为帮助,我还将发布一个API返回的示例(针对一首歌曲):

{
   'comment':'',
   'rating':'0',
   'albumArtRef':[
     {
       'url': 'http://lh6.ggpht.com/...'
     }
   ],
   'artistId':[
     'Aod62yyj3u3xsjtooghh2glwsdi'
   ],
   'composer':'',
   'year':2011,
   'creationTimestamp':'1330879409467830',
   'id':'5924d75a-931c-30ed-8790-f7fce8943c85',
   'album':'Heritage ',
   'totalDiscCount':0,
   'title':'Haxprocess',
   'recentTimestamp':'1372040508935000',
   'albumArtist':'',
   'trackNumber':6,
   'discNumber':0,
   'deleted':False,
   'storeId':'Txsffypukmmeg3iwl3w5a5s3vzy',
   'nid':'Txsffypukmmeg3iwl3w5a5s3vzy',
   'totalTrackCount':10,
   'estimatedSize':'17229205',
   'albumId':'Bdkf6ywxmrhflvtasnayxlkgpcm',
   'beatsPerMinute':0,
   'genre':'Progressive Metal',
   'playCount':7,
   'artistArtRef':[
     {
       'url': 'http://lh3.ggpht.com/...'
     }
   ],
   'kind':'sj#track',
   'artist':'Opeth',
   'lastModifiedTimestamp':'1330881158830924',
   'clientId':'+eGFGTbiyMktbPuvB5MfsA',
   'durationMillis':'418000'
 }

And my code is as follows: 我的代码如下:

library = api.get_all_songs()
    print("There are",len(library),"items in your music library")
    for track in library:
        if track['rating'] != 5:
            library.remove(track)
    print("You have",len(library),"favorite tracks!")
    return library

I added in the "favorite songs" amount (as well as the number of songs in the library) to test and see if my loop and if statement would remove anything, and it doesn't (as the numbers are the same before and after the loop) - I figure it has something to do with that, as the code works fine otherwise, it just doesn't prune the list. 我添加了“喜欢的歌曲”数量(以及库中的歌曲数量)以测试并查看我的循环和if语句是否会删除任何内容,但不会删除(因为之前和之后的数量相同)循环)-我认为这与此有关,因为代码可以正常工作,否则,它不会修剪列表。

If any extra code is needed, I'd be more than happy to provide it. 如果需要任何额外的代码,我会很乐意提供。

Try a list comprehension to filter your list of dictionaries: 尝试使用列表理解来过滤您的词典列表:

library = ...

good_songs = [ x for x in library if x['rating'] == '5' ]

The API is treating the rating as a string. API将评分视为字符串。

You are treating the rating as an integer. 您正在将评分视为整数。 That's why no track is ever deleted. 这就是为什么不会删除任何曲目的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM