0
私は次のコードを持っています。例えば、chan_numが248の場合、numキー値のテストに基づいて外側のdictキーを取得しようとしています。 'lifestyle & Culture 'キーが、私は常に最初のアイテムに一致しています。別のdictキー値の一致方法
これをどのように達成できますか?
chan_tags = {
'Entertainment': {'num': 101, 'on': 1},
'Lifestyle and Culture': { 'num': 240, 'on': 1 },
'Movies': { 'num': 301, 'on': 1 }
}
def chanToTag(chan_num, chan_tags):
tag = ""
for n in sorted(chan_tags, key=lambda k: chan_tags[k]['num']):
if chan_num >= chan_tags[n]['num']:
tag = n
break
return tag
tag_name = chanToTag(248, chan_tags)
print(tag_name)
は休憩をコメントアウトするか、それが正常に動作します削除:
reverse=True
キーワード引数を渡すと、逆の順序でソートsorted
を作ります –