第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

在具有匹配屬性的 dict 數(shù)組中查找最小值,返回最大的分組

在具有匹配屬性的 dict 數(shù)組中查找最小值,返回最大的分組

大話(huà)西游666 2021-08-14 16:43:03
這很容易通過(guò)幾個(gè)循環(huán)來(lái)做到這一點(diǎn),但我相信有一種更有效的方法來(lái)實(shí)現(xiàn)這一點(diǎn),我很想學(xué)習(xí)??紤]以下 dict 數(shù)組,它表示從 nosql 數(shù)據(jù)庫(kù)中提取的數(shù)據(jù)。x = [    {        "loc" : "alpha",        "tag" : 1,        "dist" : 5    },    {        "loc" : "bravo",        "tag" : 0,        "dist" : 2    },    {        "loc" : "charlie",        "tag" : 5,        "dist" : 50    },    {        "loc" : "delta",        "tag" : 4,        "dist" : 2    },    {        "loc" : "echo",        "tag" : 2,        "dist" : 30    },    {        "loc" : "foxtrot",        "tag" : 4,        "dist" : 2    },    {        "loc" : "gamma",        "tag" : 4,        "dist" : 2    },    {        "loc" : "hotel",        "tag" : 0,        "dist" : 2    },]我想找到所有具有最低 'dist' 值的項(xiàng)目,并且如果有多個(gè)具有相同最低值的 dict,我希望對(duì)具有相同最低值的 dict 最多的屬性 'tag' 進(jìn)行分組.例如,上面所需的返回?cái)?shù)據(jù)是:r = [    {        "LocationName" : "delta",        "tag" : 4,        "dist" : 2    },    {        "loc" : "foxtrot",        "tag" : 4,        "dist" : 2    },    {        "loc" : "gamma",        "tag" : 4,        "dist" : 2    }]總結(jié):dist:2 是最低值,[bravo, delta, foxtrot, gamma, hotel] 的dist 都是2,[bravo, hotel] 的標(biāo)簽是:0,[delta, foxtrot, gamma] 的標(biāo)簽都是的:4。返回一個(gè) dicts [delta, foxtrot, gamma] 數(shù)組,因?yàn)樗鼈冇懈嗑哂邢嗤ヅ錁?biāo)簽和最低 dist 的。我正在使用 python 3.6。
查看完整描述

2 回答

?
POPMUISE

TA貢獻(xiàn)1765條經(jīng)驗(yàn) 獲得超5個(gè)贊

您可以key為max()和指定一個(gè)(即 lambda 函數(shù)),min()這可以幫助解決此問(wèn)題。對(duì)于你的第一次測(cè)試,


lowest_single_dist = min(x, key=lambda i: i["dist"])

返回 中x具有最低值的元素"dist"。如果您想要所有具有該標(biāo)簽值的元素,您可以使用列表理解:


lowest_dists = [i for i in x if i["dist"] == lowest_single_dist["dist"]]

為了獲得最大的分組,我將首先"tag"在該子集中創(chuàng)建一組可能的值,然后檢查每個(gè)有多少個(gè)lowest_dists,然后取哪個(gè)計(jì)數(shù)最高:


tags = [i["tag"] for i in lowest_dists]              # get a list of just the tags

ct = {t: tags.count(t) for t in set(tags)}           # make a dict of tag:count for each unique tag

max_tag = max(ct, key=lambda x: ct[x])               # find the largest count and get the largest tag

r = [i for i in lowest_dists if i["tag"] == max_tag] # use another list comprehension to get all the max tags

如果你想把它全部縮短成兩行,你可以不那么pythonic并這樣做:


m = min(x, key=lambda i: (i["dist"], -1 * max([j["tag"] for j in x if j["dist"] == i["dist"]].count(i["tag"])))

r = [i for i in x if i["tag"] == m["tag"] and i["dist"] == m["dist"]]

這利用了這樣一個(gè)事實(shí),即您可以返回一個(gè)元組作為排序的鍵,并且只有在第一個(gè)值相等時(shí)才會(huì)檢查元組的第二個(gè)值。我將稍微擴(kuò)展第一行并解釋每個(gè)部分的作用:


m = min(x, key=lambda i: (

    i["dist"], -1 * max(

        [j["tag"] for j in x if j["dist"] == i["dist"]].count(i["tag"])

    ))

  • 最內(nèi)層的列表推導(dǎo)式為所有元素生成一個(gè)標(biāo)簽列表,其x值為"dist"asi

  • 然后,取與相同的標(biāo)簽計(jì)數(shù) i

  • 乘以 -1 使其為負(fù)數(shù),以便min()正確運(yùn)行

  • 創(chuàng)建一個(gè)i["dist"]和我們剛剛計(jì)算的值(i["tag"]in的頻率x)的元組,并為每個(gè)元素返回該值

  • 分配給m列表中具有最低值"dist"和最頻繁值的元素"tag"

  • 分配給具有相同值r的元素的子列表和x"dist""tag"

所以基本上與上面相同的過(guò)程,但更短,效率更低,并且更復(fù)雜一些。


查看完整回答
反對(duì) 回復(fù) 2021-08-14
?
BIG陽(yáng)

TA貢獻(xiàn)1859條經(jīng)驗(yàn) 獲得超6個(gè)贊

按“dist”中的值對(duì)字典列表進(jìn)行排序,并取最低值


x.sort(key= lambda x:x['dist'])

lowest = x[0]['dist']

創(chuàng)建一個(gè)字典列表,其中 'dist' 的值等于最低值


x2 = [i for i in x if i['dist']==lowest]

這應(yīng)該是你的答案。如果列表中有多個(gè)項(xiàng)目,請(qǐng)重復(fù)上述過(guò)程。


if len(x2)>1:

  x3 = [i['tag'] for i in x2]

  mode = max(set(x3), key=x3.count)

  r = [i for i in x if i['tag']==mode]


查看完整回答
反對(duì) 回復(fù) 2021-08-14
  • 2 回答
  • 0 關(guān)注
  • 156 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)