V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
bestehen
V2EX  ›  Python

Python 3 有序字典的问题,这里的 字典格式 怎么是([])这种形式,字典不是{}???

  •  
  •   bestehen · Aug 3, 2018 · 4288 views
    This topic created in 2824 days ago, the information mentioned may be changed or developed.
    od = OrderedDict([('a', 1), ('c', 2), ('b', 3)])
    od
    OrderedDict([('a', 1), ('c', 2), ('b', 3)])
    test=collections.OrderedDict()
    test['张三'] = 92
    test['李四'] = 85
    test
    OrderedDict([('张三', 92), ('李四', 85)])
    30 replies    2018-08-04 14:07:04 +08:00
    whoami9894
        1
    whoami9894  
       Aug 3, 2018 via Android   ❤️ 26
    看了你的发帖记录我并不想回复,甚至想 block
    myyou
        3
    myyou  
       Aug 3, 2018
    你管它是什么格式干什么,只有拥有字典的特性,由是有序的不就行了。
    你看到的只是__repr__方法让你看到的样子,你也可以让普通字典显示为([])这种格式。
    megachweng
        4
    megachweng  
       Aug 3, 2018 via iPhone
    突然想换语言了。你真的是基础语法不看,完上手就撸吗,这问的相当于为什么 1x1 代码里面要写成 1*1 嘛
    SuperMild
        5
    SuperMild  
       Aug 3, 2018
    基础太薄弱,建议找本入门书从第一页开始从头到尾认真看一遍。
    henices
        6
    henices  
       Aug 3, 2018
    张三和李四不一样,是因为张三就是张三,他不是李四啊。
    ballshapesdsd
        7
    ballshapesdsd  
       Aug 3, 2018
    一天伸手好多次。。
    ballshapesdsd
        8
    ballshapesdsd  
       Aug 3, 2018
    而且还很没礼貌,真实让人讨厌
    xiaket
        9
    xiaket  
       Aug 3, 2018   ❤️ 2
    大家一起 block 吧...

    @Livid 这种没礼貌的同学能否在站规中加以限制? 我又感觉我们需要 downvote 的功能了, 如果一个人短时间在一个节点中的回复被 downvote 太多, 禁止发帖或回复什么的.
    misaka19000
        10
    misaka19000  
       Aug 3, 2018
    你们可以看看楼主的这个帖子,笑死我了

    /476354
    misaka19000
        11
    misaka19000  
       Aug 3, 2018
    ivechan
        12
    ivechan  
       Aug 3, 2018
    如果是 {k:v} 这种形式,那么在调用 OrderedDict 的“构造函数“ `__init__` 时,
    你是先生成一个 无序字典{k:v},然后再传给 OrderedDict。
    也就是说,你已经破坏了顺序。
    所以应该用 list, tuple 这种有序结构,保证 OrderedDict 知道你的顺序。
    ivechan
        13
    ivechan  
       Aug 3, 2018
    接 #12,另外,提问建议还是保留点虚心的态度。
    imn1
        14
    imn1  
       Aug 3, 2018
    @whoami9894
    @xiaket
    @ballshapesdsd
    其实也不必太计较,感觉就是个小学生学 python 而已

    @bestehen
    你应该学学网上发布那个 7 岁小孩打 110,虽然紧张,但该说的都能清楚表达,记忆、表达的条理都清晰

    你平时习惯 dict({"one": 1, "two": 2, "three": 3}) 这样写么?

    手册都写了
    >>> a = dict(one=1, two=2, three=3)
    >>> b = {'one': 1, 'two': 2, 'three': 3}
    >>> c = dict(zip(['one', 'two', 'three'], [1, 2, 3]))
    >>> d = dict([('two', 2), ('one', 1), ('three', 3)])
    >>> e = dict({'three': 3, 'one': 1, 'two': 2})
    >>> a == b == c == d == e
    True

    e 这种情况是最少见的,因为类似的 b 简单多了
    你要明白 dict()和 OrderedDict()最主要是转换,并不是从一个 dict 变为另一个 dict

    你是不是写惯了 powershell ?以为类似[ordered]@{}就搞定有序 dict 有序化
    crazycabbage
        15
    crazycabbage  
       Aug 3, 2018 via iPhone
    又让他有序,又让他和字典长一个样
    xiexingjia
        16
    xiexingjia  
       Aug 3, 2018
    #11

    “ d 等于白说,没说重点” hhhh
    inhzus
        17
    inhzus  
       Aug 3, 2018 via Android
    @xiexingjia 真的是听了想打人 hhh
    问别人问题还有这么大爷的太可怕了
    BingoXuan
        18
    BingoXuan  
       Aug 3, 2018
    class OtherDict:
    def __init__(self,*args):
    self._k = []
    self._v = []
    for k,v in args[0]:
    self._k.append(k)
    self._v.append(v)
    def __getitem__(self,name):
    try:
    return self._v[self._k.index(name)]
    except ValueError:
    raise KeyError
    def __repr__(self):
    str_list = []
    for i in range(len(self._k)):
    s = str(self._k[i]) + '~' + str(self._v[i])
    str_list.append(s)
    return 'Oh! You want see me?' +'「'+','.join(str_list)+'」'

    a = OtherDict([('a', 1), ('c', 2), ('b', 3)])

    print(a) #Oh! You want see me?「 a ~ 1,c ~ 2,b ~ 3 」

    a['a'] #1

    随便写了一下,python 自省威力很强大,很多神奇的功能都是靠自省实现的。但初学就不要深究细节,好好写一下代码
    ballshapesdsd
        19
    ballshapesdsd  
       Aug 3, 2018
    @imn1 #14
    @BingoXuan #18
    两位圣人让我产生了一个给小孩换尿布的既视感
    imn1
        20
    imn1  
       Aug 3, 2018
    @ballshapesdsd
    其实他应该去 CSDN,而不是这里
    wico97
        21
    wico97  
       Aug 3, 2018
    @xiaket
    @misaka19000 你们都当大神呀。人家问点基础的知识咋了。你看不上就别回答。
    BingoXuan
        22
    BingoXuan  
       Aug 3, 2018
    @ballshapesdsd
    即将下班划水很正常啊:)
    misaka19000
        23
    misaka19000  
       Aug 3, 2018
    @wico97 #21 你是不是搞错了什么?我并没有回答啊 : )
    wico97
        24
    wico97  
       Aug 3, 2018
    @misaka19000 我是看不惯你拽的样子
    wico97
        25
    wico97  
       Aug 3, 2018
    @ballshapesdsd 你很拽呀。除了嘲讽人家新手还会干嘛
    ballshapesdsd
        26
    ballshapesdsd  
       Aug 3, 2018
    @wico97 #25 会干很多。再说了,提问前动动脑子或者搜索引擎都不用的人有啥好尊重的,等于就是想用别人的时间换取自己的利益。如果不是他天天提这种低级问题,而且语气很拽,会有那么多人怼他?
    princelai
        27
    princelai  
       Aug 3, 2018 via Android
    楼主怎么不出来怼两句了,看你之前的发帖别人会打得不好你不是都会怼回去吗
    wjm2038
        28
    wjm2038  
       Aug 3, 2018 via Android
    想起来四个字母 RTFI
    hundan
        29
    hundan  
       Aug 4, 2018 via Android
    spam block
    May725
        30
    May725  
       Aug 4, 2018
    让我想起以前师兄提的一句话:不要过早的陷入细节。
    可能 up 就处于这种情况,建议先实现主体功能,这些细节的问题后续慢慢就明白了
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1015 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 65ms · UTC 19:05 · PVG 03:05 · LAX 12:05 · JFK 15:05
    ♥ Do have faith in what you're doing.