zoukankan      html  css  js  c++  java
  • 字典问题

    >>> dir1=dir(((1,100),(2,200)))
    >>> dir1
    ['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'count', 'index']
    >>> dir1=dict(((1,100),(2,200)))
    >>> dir1
    {1: 100, 2: 200}
    >>> dir2=(黎明='李宁',大家='你好')
    SyntaxError: invalid syntax
    >>> dir2=dict(黎明='李宁',大家='你好')
    SyntaxError: invalid syntax
    >>> dir2=dict(黎明='李宁',大家='你好')
    SyntaxError: invalid syntax
    >>> dir2=dict(黎明='李宁', 大家='你好')
    SyntaxError: invalid syntax
    >>> 
    >>> 
    >>> dir2=dict(    nihao='wozhidao', buha='buzhida')
    >>> dir2
    {'nihao': 'wozhidao', 'buha': 'buzhida'}
    >>> dir2=dict(    nihao='天天', buha='大世界')
    >>> 地热
    SyntaxError: invalid syntax
    >>> 
    >>> dir2
    {'nihao': 'xccxecxccxec', 'buha': 'xb4xf3xcaxc0xbdxe7'}
    >>> dir2=dict(    nihao='wozhidao', 大家='buzhida')
    SyntaxError: invalid syntax
    >>> dir3['12']=21
    
    Traceback (most recent call last):
      File "<pyshell#37>", line 1, in <module>
        dir3['12']=21
    NameError: name 'dir3' is not defined
    >>> dir2
    {'nihao': 'xccxecxccxec', 'buha': 'xb4xf3xcaxc0xbdxe7'}
    >>> dir1
    {1: 100, 2: 200}
    >>> dir1[1]=2000
    >>> dir1
    {1: 2000, 2: 200}
    >>> dir1[3]=3000
    >>> dir1
    {1: 2000, 2: 200, 3: 3000}
    >>>字典的几种初始化问题,注意汉语。。。。。。。。。如果添加,修改------------------》map






    #字典的创建删除一些操作

    >>> dir1=dir(((1,100),(2,200))) >>> dir1 ['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'count', 'index'] >>> dir1=dict(((1,100),(2,200)))#注意此处两个外面的小括号 >>> dir1 {1: 100, 2: 200} >>> dir2=(黎明='李宁',大家='你好') SyntaxError: invalid syntax >>> dir2=dict(黎明='李宁',大家='你好') SyntaxError: invalid syntax >>> dir2=dict(黎明='李宁',大家='你好') SyntaxError: invalid syntax >>> dir2=dict(黎明='李宁', 大家='你好') SyntaxError: invalid syntax >>> >>> >>> dir2=dict( nihao='wozhidao', buha='buzhida') >>> dir2 {'nihao': 'wozhidao', 'buha': 'buzhida'} >>> dir2=dict( nihao='天天', buha='大世界') >>> 地热 SyntaxError: invalid syntax >>> >>> dir2 {'nihao': 'xccxecxccxec', 'buha': 'xb4xf3xcaxc0xbdxe7'} >>> dir2=dict( nihao='wozhidao', 大家='buzhida') SyntaxError: invalid syntax >>> dir3['12']=21 Traceback (most recent call last): File "<pyshell#37>", line 1, in <module> dir3['12']=21 NameError: name 'dir3' is not defined >>> dir2 {'nihao': 'xccxecxccxec', 'buha': 'xb4xf3xcaxc0xbdxe7'} >>> dir1 {1: 100, 2: 200} >>> dir1[1]=2000 >>> dir1 {1: 2000, 2: 200} >>> dir1[3]=3000 >>> dir1 {1: 2000, 2: 200, 3: 3000} >>> >>> >>> >>> >>> >>> >>> >>> >>> >>> dir11={} >>> >>> dir11 {} >>> dir11.fromkeys((1,2,3)) {1: None, 2: None, 3: None} >>> dir11.fromkeys((1,2,3),('number')) {1: 'number', 2: 'number', 3: 'number'} >>> dir11.fromkeys((1,2,3),('one','two','three')) {1: ('one', 'two', 'three'), 2: ('one', 'two', 'three'), 3: ('one', 'two', 'three')} >>> dir11.fromkeys((1,3),'nukmber') {1: 'nukmber', 3: 'nukmber'} >>> >>> >>> #eg2 >>> di={(1,11),(2,22),3(33)} Traceback (most recent call last): File "<pyshell#63>", line 1, in <module> di={(1,11),(2,22),3(33)} TypeError: 'int' object is not callable >>> di=dirt{(1,11),(2,22),3(33)} SyntaxError: invalid syntax >>> di=dict{(1,11),(2,22),3(33)} SyntaxError: invalid syntax >>> di=dict{(1,11),(2,22),(3,33)} SyntaxError: invalid syntax >>> di=dirt{1:11,2:22,3:33} SyntaxError: invalid syntax >>> di=dict{1:11,2:22,3:33} SyntaxError: invalid syntax >>> >>> di=dict(((1,11),(2,22),3(33))) Traceback (most recent call last): File "<pyshell#70>", line 1, in <module> di=dict(((1,11),(2,22),3(33))) TypeError: 'int' object is not callable >>> di=dict(((1,11),(2,22),(3,33))) >>> di {1: 11, 2: 22, 3: 33} >>> di.iterkeys <built-in method iterkeys of dict object at 0x0000000002E498C8> >>> list(di.iterkeys) Traceback (most recent call last): File "<pyshell#74>", line 1, in <module> list(di.iterkeys) TypeError: 'builtin_function_or_method' object is not iterable >>> for eachKey in di.iterkeys: print eachKey Traceback (most recent call last): File "<pyshell#77>", line 1, in <module> for eachKey in di.iterkeys: TypeError: 'builtin_function_or_method' object is not iterable >>> for eachKey in di.iterkeys():#三种变量方式 1遍历值 2遍历建 3遍历键值对 print(eachKey) 1 2 3 >>> for eachKey in di.iterkeys: print eachKey Traceback (most recent call last): File "<pyshell#84>", line 1, in <module> for eachKey in di.iterkeys: TypeError: 'builtin_function_or_method' object is not iterable >>> for eachKey in di.itervalues(): print(eachKey) 11 22 33 >>> for eachitem in di.iteritems SyntaxError: invalid syntax >>> for eachitem in di.iteritems(): print (eachitem)


  • 相关阅读:
    LeetCode 977 有序数组的平方
    LeetCode 24 两两交换链表中的节点
    LeetCode 416 分割等和子集
    LeetCode 142 环形链表II
    LeetCode 106 从中序与后序遍历序列构造二叉树
    LeetCode 637 二叉树的层平均值
    LeetCode 117 填充每个节点的下一个右侧节点
    LeetCode 75 颜色分类
    redhat 7.4 挂载ntfs格式的u盘并且使用
    redhat 查看CPU frequency scaling(CPU频率缩放)
  • 原文地址:https://www.cnblogs.com/13224ACMer/p/6048744.html
Copyright © 2011-2022 走看看