python 巢狀字典取值增強版

2022-05-19 11:59:11 字數 1178 閱讀 5641

def getdictvalue(d,code):

result=

if isinstance(d, dict) :

try:

value = d[code]

except exception as e:

pass

for valuedd in d.values():

if isinstance(valuedd,dict):

yied_result=getdictvalue(valuedd,code)

if len(yied_result) != 0:

elif isinstance(valuedd,(list,tuple)):

for item in d:

valueitem=getdictvalue(valuedd,code)

if valueitem !="none" and valueitem is not none and len(valueitem)!=0:

if valueitem not in result:

elif isinstance(d, (list, tuple)):

for item in d:

value=getdictvalue(item,code)

if value !="none" and value is not none and len(value)!=0:

if value not in result:

return result

由於返回時list,經過層層巢狀,list是複雜的,封裝乙個,增加乙個list轉換

class listchangetype(object):

"""對於查詢後的list的資料的清洗"""

def __init__(self):

self.arg =

def make(self,listone):

for i in listone:

if isinstance(i,(type,list)):

for l in i:

self.make(i)

else:

if i not in self.arg:

return self.arg

這樣就按順序,讀取了複雜的dict, 比之前的一篇文章的**,更加完美。

增強版字典DictionaryEx

public class dictionaryex idictionary 初始化 dictionaryex key不存在時返回defaultvalue 預設值 比較鍵時要使用物件,如果為null則使用預設比較方法 public dictionaryex tvalue defaultvalue,ie...

python實現增強版ping

由於定位網路問題時,經常要ping,並且有時候要長時間同時ping多位址,系統自帶的ping不夠用 所以自己用python實現乙個,用py2exe編譯為exe程式後可以方便發布。import time import string import thread import os ping ip tim...

IntelliJ IDEA 便攜增強版

idea 全稱 intellij idea,是j a語言開發的整合環境,intellij在業界被公認為最好的j a開發工具之一,尤其在智慧型 助手 自動提示 重構 j2ee支援 各類版本工具 git svn github等 junit cvs整合 分析 創新的gui設計等方面的功能可以說是超常的。i...