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ステートメントの条件にしたいと思います。
に
保存。質問にも同様に言及してください –
辞書の値リストには引用符とカンマがありますか? – Lafexlos
@Lafexlos申し訳ありませんが、上記の例に記載されていない何らかの理由でクローズクォートがあります。 – rhillhouse