将double for循环转换为列表理解

时间:2016-01-20 02:14:36

标签: python

我有以下json对象:

def solve(matrix,size):
    c = []
    d = 0
    print_matrix(matrix,size)
    if size == 0:
        for i in range(len(matrix)):
            d = d + matrix[i]
        return d
    elif len(matrix) == 4:
        c = (matrix[0] * matrix[3]) - (matrix[1] * matrix[2])
        print(c)
        return c
    else:
        for j in range(size):
            new_matrix = []
            for i in range(size*size):
                if i % size != j and i > = size:
                    new_matrix.append(matrix[i])

            c.append(solve(new_matrix,size-1) * matrix[j] * ((-1)**(j+2)))

        d = solve(c,0)
        return d

我试图通过以下方式获取所有{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'}

asin

如何使用列表解析来完成此操作?

3 个答案:

答案 0 :(得分:1)

只需复制现有的行:

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

答案 1 :(得分:1)

试试这个:

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

答案 2 :(得分:1)

title='title1'