|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
一
members = {
"鱼C工作室" : {"小甲鱼":83, "不二如是":89, "二师兄":64, "小师妹":75, "鱼小二":96},
"复仇者联盟" : {"钢铁侠":85, "绿巨人":39, "黑寡妇":82, "鹰眼":73, "雷神":99},
"奥特曼家族" : {"迪迦":99, "艾斯":84, "泰罗":63, "佐菲":78, "赛文":78}}
x = []
for k,v in members.items():
x.append(str(k+":"+min(v,key=v.get)))
print(x)
二
members = {
"鱼C工作室" : {"小甲鱼":83, "不二如是":89, "二师兄":64, "小师妹":75, "鱼小二":96},
"复仇者联盟" : {"钢铁侠":85, "绿巨人":39, "黑寡妇":82, "鹰眼":73, "雷神":99},
"奥特曼家族" : {"迪迦":99, "艾斯":84, "泰罗":63, "佐菲":78, "赛文":78}}
x = [':'.join((i, min(members[i].items(), key = lambda x:x[1])[0])) for i in members]
print(x)
三
members = {
"鱼C工作室" : {"小甲鱼":83, "不二如是":89, "二师兄":64, "小师妹":75, "鱼小二":96},
"复仇者联盟" : {"钢铁侠":85, "绿巨人":39, "黑寡妇":82, "鹰眼":73, "雷神":99},
"奥特曼家族" : {"迪迦":99, "艾斯":84, "泰罗":63, "佐菲":78, "赛文":78}}
x = [':'.join(k,min(v,key=v.get) for k,v in members.items())]
print(x)
为什么一、二可以运行,三就报错呢
本帖最后由 isdkz 于 2023-1-7 11:21 编辑
一:在生成器表达式里面 k,min(v,key=v.get) 需要加一个括号表示元组
二:因为生成器表达式的生成元素都是元组,所以不能直接用 join
members = {
"鱼C工作室" : {"小甲鱼":83, "不二如是":89, "二师兄":64, "小师妹":75, "鱼小二":96},
"复仇者联盟" : {"钢铁侠":85, "绿巨人":39, "黑寡妇":82, "鹰眼":73, "雷神":99},
"奥特曼家族" : {"迪迦":99, "艾斯":84, "泰罗":63, "佐菲":78, "赛文":78}}
x = [':'.join(i) for i in ((k,min(v,key=v.get)) for k,v in members.items())]
print(x)
|
|