[python] Sort dictionary into list based on another dict · GitHub

文章推薦指數: 80 %
投票人數:10人

[python] Sort dictionary into list based on another dict ... dict_key = {'a': 1, 'b': 3, 'c': 2} # The order should be "a c b". # sort dict_to_sort by using ... Skiptocontent Allgists BacktoGitHub Signin Signup Sign in Sign up {{message}} Instantlysharecode,notes,andsnippets. sheenzhaox/sort_dict_by_another_dict.py LastactiveMay9,2022 Star 4 Fork 0 Star Code Revisions 2 Stars 4 Embed Whatwouldyouliketodo? Embed Embedthisgistinyourwebsite. Share Copysharablelinkforthisgist. Clonevia HTTPS ClonewithGitorcheckoutwithSVNusingtherepository’swebaddress. LearnmoreaboutcloneURLs DownloadZIP [python]Sortdictionaryintolistbasedonanotherdict Raw sort_dict_by_another_dict.py ThisfilecontainsbidirectionalUnicodetextthatmaybeinterpretedorcompileddifferentlythanwhatappearsbelow.Toreview,openthefileinaneditorthatrevealshiddenUnicodecharacters. LearnmoreaboutbidirectionalUnicodecharacters Showhiddencharacters dict_to_sort={'a':123,'b':'test','c':'-'} dict_key={'a':1,'b':3,'c':2}#Theordershouldbe"acb" #sortdict_to_sortbyusingdict_key sorted_pair_list=sorted(dic.items(),key=lambdax:dict_key.get(x[0])) #thelistofvalues value_list=zip(*sorted_pair_list)[1] Copylink daluu commented Feb4,2019 Ibelieveinline6,dict.items()shouldbedict_to_sort.items() Sorry,somethingwentwrong. Signupforfree tojointhisconversationonGitHub. Alreadyhaveanaccount? Signintocomment Youcan’tperformthatactionatthistime. Yousignedinwithanothertaborwindow.Reloadtorefreshyoursession. Yousignedoutinanothertaborwindow.Reloadtorefreshyoursession.



請為這篇文章評分?