如何删除字典中字典的键值对?

时间:2017-11-18 13:44:57

标签: python-3.x dictionary

我有一本字典

<script type="text/javascript">
function LoadTheArchive(TotalFeed) 
{
    var PostTitles = new Array();
    var PostURLs = new Array();
    var PostYears = new Array();
    var PostMonths = new Array();
    var PostDays = new Array();

    if("entry" in TotalFeed.feed) 
    {
        var PostEntries=TotalFeed.feed.entry.length;
        for(var PostNum=0; PostNum<PostEntries ; PostNum++) 
        {
            var ThisPost = TotalFeed.feed.entry[PostNum];
            PostTitles.push(ThisPost.title.$t);
            PostYears.push(ThisPost.published.$t.substring(0,4));
            PostMonths.push(ThisPost.published.$t.substring(5,7));
            PostDays.push(ThisPost.published.$t.substring(8,10));
            var ThisPostURL;
            for(var LinkNum=0; LinkNum < ThisPost.link.length; LinkNum++) 
            {
                if(ThisPost.link[LinkNum].rel == "alternate") 
                {
                    ThisPostURL = ThisPost.link[LinkNum].href;
                    break
                }
            }
            PostURLs.push(ThisPostURL);
        }
    }
    DisplaytheTOC(PostTitles,PostURLs,PostYears,PostMonths,PostDays);
}

function DisplaytheTOC(PostTitles,PostURLs,PostYears,PostMonths,PostDays)
{
    var MonthNames=["January","February","March","April","May","June","July","August","September","October","November","December"];
    var NumberOfEntries=PostTitles.length;
    var currentMonth = "";
    var currentYear = "";

    for(var EntryNum = 0; EntryNum < NumberOfEntries; EntryNum++)
    {
        NameOfMonth = MonthNames[parseInt(PostMonths[EntryNum],10)-1]

        if (currentMonth != NameOfMonth || currentYear != PostYears[EntryNum]) 
        {
            currentMonth = NameOfMonth;
            currentYear = PostYears[EntryNum];

            document.write("<div class='dateStyle'><br />" + currentMonth+" "+currentYear+" </div>");
        }

        document.write('<a href ="'+PostURLs[EntryNum]+'"><div class=dayStyle>'+parseInt(PostDays[EntryNum],10)+":&nbsp;&nbsp;</div> "+PostTitles[EntryNum]+"</a><br />");
    }
}
</script>

<script src="https://mywebsite.com/feeds/posts/default?max-results=500&amp;alt=json-in-script&amp;callback=LoadTheArchive" />
</script>
<script src="https://mywebsite.com/feeds/posts/default?max-results=150&start-index=151&alt=json-in-script&callback=LoadTheArchive"></script>
<script src="https://mywebsite.com/feeds/posts/default?max-results=150&start-index=301&alt=json-in-script&callback=LoadTheArchive"></script>
<script src="https://mywebsite.com/feeds/posts/default?max-results=150&start-index=451&alt=json-in-script&callback=LoadTheArchive"></script>
<script src="https://mywebsite.com/feeds/posts/default?max-results=150&start-index=601&alt=json-in-script&callback=LoadTheArchive"></script>
<script src="https://mywebsite.com/feeds/posts/default?max-results=150&start-index=851&alt=json-in-script&callback=LoadTheArchive"></script>
<script src="https://mywebsite.com/feeds/posts/default?max-results=150&start-index=1001&alt=json-in-script&callback=LoadTheArchive"></script>

<!--CUSTOMIZATION-->
<style type="text/css">
.dateStyle {
     color:#000;
     font-size: 30px;
     font-family: Fjalla One;
     margin: 0;
}

.dayStyle {
     color:#000;
     font-family: Droid Sans;
     display: inline-block;
}

</style>

我想删除字典中键dicto = {12:{34:45,56:78},45:{67:23,90:15}} 的{​​{1}}键值对。我该怎么做?

这是我到目前为止所做的,但我收到了错误

67:23

我得到的错误是

45

如何删除特定的键值对?

3 个答案:

答案 0 :(得分:2)

或者更接近你所做的并从所有内部词典中删除键67而不需要知道这些键:

适用于python 2.7

dicto = {12:{34:45,56:78},45:{67:23,90:15}}
print ""
for k,v in dicto.items(): 
    for i in v.items():
         if (i[0] == 67):
                v.pop(i[0])
print(dicto)

输出:

{12: {56: 78, 34: 45}, 45: {90: 15}}

而不是修改某个元素,而是告诉正确的内部字典从其自身中删除其键/值对

此代码将从所有内部词典中删除键“67”:

dicto = {12:{34:45,56:78},45:{67:23,90:15},99:{67:1,88:5}}

将导致

{99: {88: 5}, 12: {56: 78, 34: 45}, 45: {90: 15}}

编辑: 正如Souvik Rey所指出的那样,这不适用于3.6(用pyfiddle测试它的2.7在哪里工作)

适用于3.6

dicto = {12:{34:45,56:78},45:{67:23,90:15},99:{67:1,88:5}}
print ("")
dictsToRemoveKeysFrom = []
for k,v in dicto.items():
    print (v)
    for i in v.items():
         if (i[0] == 67):
                dictsToRemoveKeysFrom.append(v)

for d in dictsToRemoveKeysFrom:
    d.pop(67)

print(dicto)

您收到错误是因为您在迭代父级时更改了字典。简单地记住之后修改和修改它们的dicts。

答案 1 :(得分:1)

看起来很简单。

>>> dicto = {12:{34:45,56:78},45:{67:23,90:15}}
>>> dicto
{12: {34: 45, 56: 78}, 45: {67: 23, 90: 15}}
>>> del dicto[45][67]
>>> dicto
{12: {34: 45, 56: 78}, 45: {90: 15}}

答案 2 :(得分:0)

看起来很简单,不需要:

del k[45][63]