久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如果字典中的鍵重復(fù),如何引發(fā)錯誤

How to raise error if duplicates keys in dictionary(如果字典中的鍵重復(fù),如何引發(fā)錯誤)
本文介紹了如果字典中的鍵重復(fù),如何引發(fā)錯誤的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

如果用戶在字典中輸入重復(fù)鍵,我會嘗試引發(fā)錯誤.字典在一個文件中,用戶可以手動編輯該文件.

I try to raise an error if the user enter a duplicate key in a dictionary. The dictionary is in a file and the user can edit the file manually.

例子:

dico= {'root':{
                'a':{'some_key':'value',...},
                'b':{'some_key':'value',...},
                'c':{'some_key':'value',...},
                ...

                'a':{'some_key':'value',...},
              }
      }

新鍵a"已經(jīng)存在...

the new key 'a' already exist...

當(dāng)我從文件中加載 dico 時,如何測試 dico 并警告用戶?

How can I test dico and warn the user when I load dico from the file?

推薦答案

寫一個dict的子類,覆蓋__setitem__,這樣在替換現(xiàn)有key時會拋出錯誤;重寫文件以使用新子類的構(gòu)造函數(shù)而不是默認(rèn)的 dict 內(nèi)置函數(shù).

Write a subclass of dict, override __setitem__ such that it throws an error when replacing an existing key; rewrite the file to use your new subclass's constructor instead of the default dict built-ins.

import collections

class Dict(dict):
    def __init__(self, inp=None):
        if isinstance(inp,dict):
            super(Dict,self).__init__(inp)
        else:
            super(Dict,self).__init__()
            if isinstance(inp, (collections.Mapping, collections.Iterable)): 
                si = self.__setitem__
                for k,v in inp:
                    si(k,v)

    def __setitem__(self, k, v):
        try:
            self.__getitem__(k)
            raise ValueError("duplicate key '{0}' found".format(k))
        except KeyError:
            super(Dict,self).__setitem__(k,v)

那么你的文件必須寫成

dico = Dict(
    ('root', Dict(
        ('a', Dict(
            ('some_key', 'value'),
            ('another_key', 'another_value')
        ),
        ('b', Dict(
            ('some_key', 'value')
        ),
        ('c', Dict(
            ('some_key', 'value'),
            ('another_key', 'another_value')
        ),

        ....
    )
)

使用元組而不是字典來導(dǎo)入文件(使用 {} 表示法編寫,它將使用默認(rèn)的字典構(gòu)造函數(shù),并且重復(fù)項會在字典構(gòu)造函數(shù)獲取它們之前消失!).

using tuples instead of dicts for the file import (written using the {} notation, it would use the default dict constructor, and the duplicates would disappear before the Dict constructor ever gets them!).

這篇關(guān)于如果字典中的鍵重復(fù),如何引發(fā)錯誤的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

How to draw a rectangle around a region of interest in python(如何在python中的感興趣區(qū)域周圍繪制一個矩形)
How can I detect and track people using OpenCV?(如何使用 OpenCV 檢測和跟蹤人員?)
How to apply threshold within multiple rectangular bounding boxes in an image?(如何在圖像的多個矩形邊界框中應(yīng)用閾值?)
How can I download a specific part of Coco Dataset?(如何下載 Coco Dataset 的特定部分?)
Detect image orientation angle based on text direction(根據(jù)文本方向檢測圖像方向角度)
Detect centre and angle of rectangles in an image using Opencv(使用 Opencv 檢測圖像中矩形的中心和角度)
主站蜘蛛池模板: 精品成人在线视频 | av网站观看 | 国产乱码精品1区2区3区 | 中文字幕在线免费观看 | 国产精品一区二区欧美黑人喷潮水 | a级免费观看视频 | 激情五月综合网 | 91在线视频观看 | 久久久久国产精品免费免费搜索 | 国产日韩精品视频 | 五月激情综合网 | 精品丝袜在线 | 99r在线| 亚洲激情在线视频 | 51ⅴ精品国产91久久久久久 | 亚洲一区二区日韩 | 国产成人精品久久二区二区91 | av网站免费看 | 中文字幕亚洲精品 | 亚洲一区二区三区四区在线观看 | 一区二区三区欧美 | 福利视频亚洲 | 亚洲精品高清视频在线观看 | 日本久久久久久 | 丝袜美腿一区二区三区 | 国产精品18hdxxxⅹ在线 | 精品综合网 | 日韩精品免费一区二区在线观看 | 日本在线观看视频 | 91高清免费| 天天干天天玩天天操 | 99久久精品免费看国产高清 | 亚洲午夜精品久久久久久app | 精品久久久一区 | 人人干视频在线 | 风间由美一区二区三区在线观看 | 欧美激情久久久 | 国产成人午夜精品影院游乐网 | 中文字幕一区二区三区四区五区 | 国产免费让你躁在线视频 | 亚洲精品乱码久久久久久蜜桃91 |