简体   繁体   中英

Convert double for loop into list comprehension

I have the following json object:

{u'trailerAvailable': False, u'number': 10, u'watched': False, u'starringCast': u'Jan Fedder,Anja Nejarri', u'genres': [u'Krimi'], u'title': u'S\xfc\xdfes Jenseits', u'ancestorTitles': [{u'contentType': u'SERIES', u'titleId': u'B00ILY1FWE', u'title': u'Gro\xdfstadtrevier'}, {u'contentType': u'SEASON', u'number': 19, u'titleId': u'B00ILZTVXI', u'title': u'Gro\xdfstadtrevier - Staffel 19'}], u'restricted': False, u'synopsis': u"Dirk Matthies und Katja Metz erhalten einen entscheidenden Tipp, um endlich Hannes Rogolski alias 'die Ziege' zu fassen. Allerdings bringt der schwer kranke Informant dadurch seine Lebensgef\xe4hrtin Hilde in Gefahr.", u'titleId': u'B00ILZOQWE', u'watchCompleted': False, u'creditsStartTimeMillis': 2836000, u'customerReviewsCollectionIncluded': True, u'contentType': u'EPISODE', u'childTitles': [], u'studioOrNetwork': u'ARD', u'amazonRating': {u'count': 0, u'rating': 0.0}, u'watchedPositionMillis': {u'valueMillis': 0}, u'listInfo': [], u'previewAvailable': False, u'subtitles': {u'content': {u'languages': []}, u'preview': {u'languages': []}, u'trailer': {u'languages': []}}, u'restrictions': [], u'regulatoryRating': u'12', u'releaseOrFirstAiringDate': {u'valueFormatted': u'2005-03-21T00:00:00', u'valueDate': 1111363200000000}, u'hasSubtitles': False, u'formats': [{u'hasMobileTrailerEncode': False, u'videoAspectRatio': 0.0, u'offers': [{u'asin': u'B0043YVHMY', u'offerType': u'SUBSCRIPTION', u'buyable': False}, {u'asin': u'B00ILZOQWE', u'price': {u'valueFormatted': u'1,49\u20ac', u'valueLong': 149.0}, u'offerType': u'PURCHASE', u'purchaseButtonText': u'Buy episode - 1,49\u20ac', u'buyable': True}, {u'rentalExpiryTermFromPurchase': {u'valueMillis': 2592000000, u'valueFormatted': u'30 days to start watching'}, u'asin': u'B00QVECS4U', u'rentalExpiryTermFromStart': {u'valueMillis': 86400000, u'valueFormatted': u'24 hours to complete watching'}, u'offerType': u'RENTAL', u'buyable': False}], u'audioFormatTypes': [u'STEREO'], u'videoFormatType': u'SD', u'hasMobileEncode': True, u'images': [{u'heightPx': 0, u'widthPx': 0, u'type': u'COVER_ART_TV', u'uri': u'http://ecx.images-amazon.com/images/I/51a8c53GcjL._UR667,500_V1_PJStripe-Prime-Only-500px,TopLeft,0,0_.jpg'}, {u'heightPx': 0, u'widthPx': 0, u'type': u'COVER_ART_TV', u'uri': u'http://ecx.images-amazon.com/images/I/51a8c53GcjL._UR667,500_V1_PJStripe-Prime-Only-500px,TopLeft,0,0_.jpg'}], u'hasTrailerEncode': False, u'hasEncode': True}, {u'hasMobileTrailerEncode': False, u'videoAspectRatio': 0.0, u'offers': [{u'asin': u'B0043YVHMY', u'offerType': u'SUBSCRIPTION', u'buyable': False}, {u'asin': u'B00ILZOR9Q', u'offerType': u'PURCHASE', u'buyable': False}, {u'rentalExpiryTermFromPurchase': {u'valueMillis': 2592000000, u'valueFormatted': u'30 days to start watching'}, u'asin': u'B00QVFSN0W', u'rentalExpiryTermFromStart': {u'valueMillis': 86400000, u'valueFormatted': u'24 hours to complete watching'}, u'offerType': u'RENTAL', u'buyable': False}], u'audioFormatTypes': [u'STEREO'], u'videoFormatType': u'HD', u'hasMobileEncode': False, u'images': [{u'heightPx': 0, u'widthPx': 0, u'type': u'COVER_ART_TV', u'uri': u'http://ecx.images-amazon.com/images/I/51a8c53GcjL._UR667,500_V1_PJStripe-Prime-Only-500px,TopLeft,0,0_.jpg'}, {u'heightPx': 0, u'widthPx': 0, u'type': u'COVER_ART_TV', u'uri': u'http://ecx.images-amazon.com/images/I/51a8c53GcjL._UR667,500_V1_PJStripe-Prime-Only-500px,TopLeft,0,0_.jpg'}], u'hasTrailerEncode': False, u'hasEncode': False}], u'customerReviewCollection': {u'customerReviews': [], u'customerReviewSummary': {u'totalReviewCount': 0, u'threeStarReviewCount': 0, u'averageOverallRating': 0.0, u'twoStarReviewCount': 0, u'fiveStarReviewCount': 0, u'oneStarReviewCount': 0, u'fourStarReviewCount': 0}}, u'runtime': {u'valueMillis': 2820000, u'valueFormatted': u'47m'}, u'contractID': u'UXSG4'}

I am trying to get all the asin s by doing:

possible_episode_ids = []
for item in t['formats']:
    for i in item['offers']:
        possible_episode_ids.append(i['asin']) 

>>> possible_episode_ids
[u'B0043YVHMY', u'B00ILZOQWE', u'B00QVECS4U', u'B0043YVHMY', u'B00ILZOR9Q', u'B00QVFSN0W']

How would I do this with a list comprehension?

Just copy the existing lines:

possible_episode_ids = [
    i['asin']
    for item in t['formats']
    for i in item['offers']
]

尝试这个:

possible_episode_ids=[offer['asin']  for fmt in t['formats'] for offer in fmt['offers']]
possible_episode_ids = [j['asin'] for i in t['formats'] for j in i['offers']]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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