Python 从字典中获取最大值
这就是我的键值的样子Python 从字典中获取最大值,python,dictionary,Python,Dictionary,这就是我的键值的样子 dict_info = {} dict_info['math 12345'] = 10 dict_info['math 1234'] = 2 dict_info['math 123'] = 1 dict_info['SCI 124'] = 16 dict_info['SCI 345'] = 2 所以我有五本不同的字典。然而,我想做的是确保我比较空格前的第一个字母,例如,我有3个数学和2个SCI。我只想得到代表数学12345的
dict_info = {}
dict_info['math 12345'] = 10
dict_info['math 1234'] = 2
dict_info['math 123'] = 1
dict_info['SCI 124'] = 16
dict_info['SCI 345'] = 2
所以我有五本不同的字典。然而,我想做的是确保我比较空格前的第一个字母,例如,我有3个数学和2个SCI。我只想得到代表数学12345的最大值10。得到16,代表SCI124,跳过剩下的。所以我想要的是数学和SCI课程的最高价值。把那些没那么值钱的扔掉。到目前为止,我的代码是这样的。我搞不懂语法
def check_if_works():
import operator
dict_info = {}
dict_info['math 12345'] = 10
dict_info['math 1234'] = 2
dict_info['math 123'] = 1
dict_info['SCI 124'] = 16
dict_info['SCI 345'] = 2
for key, value in dict_info.iteritems():
arr = key.split(' ')
class_first_letters_only = arr[0]
if class_first_letters_only == arr[0]:
check_if_works()
@用户161151
这是代码,但它会打印json文件中的所有副本
for key,value in new_dictionary.iteritems():
#print key
k = key.split()[0]
full_info = k + ' ' + key.split()[-1]
print full_info
if ans.get(k,0) < value:
ans[k] = value
#print new_dictionary
sort_info = sorted(ans.items(), key=itemgetter(1,0), reverse=True)
first_20 = sort_info[:20]
with open('output_1.json','wb') as outfile:
json.dump(first_20,outfile,indent=4)
对于键,新字典中的值。iteritems():
#打印键
k=key.split()[0]
full_info=k+''+key.split()[-1]
打印完整信息
如果ans.get(k,0)<值:
ans[k]=值
#打印新词典
sort_info=sorted(ans.items(),key=itemgetter(1,0),reverse=True)
first_20=排序信息[:20]
以open('output_1.json','wb')作为输出文件:
dump(第一个文件,输出文件,缩进=4)
我还想知道,对于我的输出,我想要的是完整的键名,而不是像MATH一样的前缀,我想要MATH 12345:16。此外,我还想将输出保存到一个json文件中,该文件从最大值对到最小值对进行排序。
@詹科斯·弗兰卡斯
for key, value in new_dictionary.iteritems():
try:
if result[key.split()[0]] < value:
result[key.split()[0]] = value
keys[key.split()[0]] = key
except KeyError:
result[key.split()[0]] = value
keys[key.split()[0]] = key
#replace the key prefixes with full length key
for key in keys.keys():
result[keys[key]] = result.pop(key)
#return result
with open('output_check_123.json','wb') as outfile:
outfile.write(json.dumps(new_dictionary,indent=4))
对于键,新字典中的值。iteritems():
尝试:
如果结果[key.split()[0]]<值:
结果[key.split()[0]]=value
键[key.split()[0]]=键
除KeyError外:
结果[key.split()[0]]=value
键[key.split()[0]]=键
#用全长键替换键前缀
对于键入键。键()
结果[键[键]]=result.pop(键)
#返回结果
以open('output_check_123.json','wb')作为输出文件:
outfile.write(json.dumps(新字典,缩进=4))
这是代码。返回
dict\u info
关键字中每个唯一第一个单词的最大值,作为关键字字典,最大值对应于dict\u info
中的第一个单词
def get_max_groups(dict_info):
result = {}
for key, value in dict_info.iteritems():
sub_key = key.split()[0]
match_keys = filter(lambda ikey: ikey.split()[0] == sub_key, result)
if not match_keys:
result[key] = value
continue
m = match_keys[0]
if result[m] < value:
del result[m]
ans[key] = value
return ans
def get_max_组(dict_信息):
结果={}
对于键,dict_info.iteritems()中的值:
sub_key=key.split()[0]
匹配_键=过滤器(lambda-ikey:ikey.split()[0]==子_键,结果)
如果不匹配_键:
结果[键]=值
持续
m=匹配_键[0]
如果结果[m]<值:
del结果[m]
ans[键]=值
返回ans
返回dict\u info
键中每个唯一第一个单词的最大值,作为该键的字典,最大值对应于dict\u info
中的第一个单词
def get_max_groups(dict_info):
result = {}
for key, value in dict_info.iteritems():
sub_key = key.split()[0]
match_keys = filter(lambda ikey: ikey.split()[0] == sub_key, result)
if not match_keys:
result[key] = value
continue
m = match_keys[0]
if result[m] < value:
del result[m]
ans[key] = value
return ans
def get_max_组(dict_信息):
结果={}
对于键,dict_info.iteritems()中的值:
sub_key=key.split()[0]
匹配_键=过滤器(lambda-ikey:ikey.split()[0]==子_键,结果)
如果不匹配_键:
结果[键]=值
持续
m=匹配_键[0]
如果结果[m]<值:
del结果[m]
ans[键]=值
返回ans
试试这个:
def check_if_works():
import operator
dict_info = {}
dict_info['math 12345'] = 10
dict_info['math 1234'] = 2
dict_info['math 123'] = 1
dict_info['SCI 124'] = 16
dict_info['SCI 345'] = 2
result={}
for key, value in dict_info.iteritems():
try:
if result[key.split()[0]] < value:
result[key.split()[0]] = value
except KeyError:
result[key.split()[0]] = value
return result
dict_info = check_if_works()
print dict_info
结果:
{'math 12345':10,'SCI 124':16}
试试这个:
def check_if_works():
import operator
dict_info = {}
dict_info['math 12345'] = 10
dict_info['math 1234'] = 2
dict_info['math 123'] = 1
dict_info['SCI 124'] = 16
dict_info['SCI 345'] = 2
result={}
for key, value in dict_info.iteritems():
try:
if result[key.split()[0]] < value:
result[key.split()[0]] = value
except KeyError:
result[key.split()[0]] = value
return result
dict_info = check_if_works()
print dict_info
结果:
{'math 12345':10,'SCI 124':16}
与以下部分一起使用:
与以下部分一起使用:
def检查副本(记录信息):
进口经营者
结果={}
键={}
对于键,dict_info.iteritems()中的值:
尝试:
#打印结果[key.split()[0]]
如果结果[key.split()[0]]<值:
结果[key.split()[0]]=value
键[key.split()[0]]=键
除KeyError外:
结果[key.split()[0]]=value
键[key.split()[0]]=键
#用全长键替换键前缀
对于键入键。键()
结果[键[键]]=result.pop(键)
返回结果
多亏了@Jankos,这一点非常有效。我添加了一个名为dict_info的参数,在这个参数中,我可以使用这个函数使它在我的主脚本下工作,主脚本的字典格式与前面的相同。现在它工作完美,谢谢你 def检查副本(记录信息):
进口经营者
结果={}
键={}
对于键,dict_info.iteritems()中的值:
尝试:
#打印结果[key.split()[0]]
如果结果[key.split()[0]]<值:
结果[key.split()[0]]=value
键[key.split()[0]]=键
除KeyError外:
结果[key.split()[0]]=value
键[key.split()[0]]=键
#用全长键替换键前缀
对于键入键。键()
结果[键[键]]=result.pop(键)
返回结果
多亏了@Jankos,这一点非常有效。我添加了一个名为dict_info的参数,在这个参数中,我可以使用这个函数使它在我的主脚本下工作,主脚本的字典格式与前面的相同。现在它工作完美,谢谢你 没有-你有一本字典。在一个字典中有5个键值对。是。我有一本有5对键值的字典
def check_duplicates(dict_info):
import operator
result={}
keys = {}
for key, value in dict_info.iteritems():
try:
#print result[key.split()[0]]
if result[key.split()[0]] < value:
result[key.split()[0]] = value
keys[key.split()[0]] = key
except KeyError:
result[key.split()[0]] = value
keys[key.split()[0]] = key
#replace the key prefixes with full length key
for key in keys.keys():
result[keys[key]] = result.pop(key)
return result