2016-10-27 10 views
0

辞書からキーを取得するコードと、galleryを含むリストのすべての値があります。リストがキーに割り当てられます。リストの辞書からキーと単一の値を取得するための条件

マイコード:

photos_dict = {} 
for key, list in sets_dict.items(): 
    for item in (item for item in list if 'gallery' in item): 
     photos_dict[key] = item 
print(photos_dict) 

そして、私の辞書(sets_dict):

{ 
    'A':[ 
     'http://example.com/gallery 
     'http://example.com/video 
    ], 
    'B':[ 
     'http://example.com/gallery 
    ], 
    'C':[ 
     'http://example.com/gallery 
    ], 
    'A':[ 
     'http://example.com/gallery 
     'http://example.com/video 
    ], 
    'D':[ 
     'http://example.com/video 
     'http://example.com/gallery 
    ], 
    'E':[ 
     'http://example.com/gallery 
     'http://example.com/video 
    ], 
    'F':[ 
     'http://example.com/gallery 
    ], 
    'G':[ 
     'http://example.com/gallery 
    ], 
    'H':[ 
     'http://example.com/gallery 
     'http://example.com/video 
    ], 
    'I':[ 
     'http://example.com/gallery 
    ], 
    'J':[ 
     'http://example.com/gallery 
    ], 
    'K':[ 
     'http://example.com/gallery 
     'http://example.com/video 
    ], 
    'L':[ 
     'http://example.com/gallery 
    ], 
    'M':[ 
     'http://example.com/gallery 
     'http://example.com/video 
    ], 
    'N':[ 
     'http://example.com/gallery 
    ] 
} 

は、この短いを書くための方法はありますか?私はif 'gallery in item'を最初のforステートメントの条件にしたいと思います。

+0

{key: link for key, links in photos_dict.items() for link in links if 'gallery' in link} 

保存。質問にも同様に言及してください –

+1

辞書の値リストには引用符とカンマがありますか? – Lafexlos

+0

@Lafexlos申し訳ありませんが、上記の例に記載されていない何らかの理由でクローズクォートがあります。 – rhillhouse

答えて

2

あなたはとしてのdictの内包表記を使用してそれを書くことができます。これは `photos_dict`に必要なフォーマットが何であるかをphotos_dict

関連する問題