V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
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
zedde
V2EX  ›  Python

Python 如何实现,数字 1 到 10,如何通过程序实现[1:{2:3, 4:5},6:{7:8, 9:10}]

  •  1
     
  •   zedde · 2014-05-14 23:28:54 +08:00 · 8854 次点击
    这是一个创建于 3636 天前的主题,其中的信息可能已经有所发展或是发生改变。
    如题,数字1到10,如何通过程序实现[1:{2:3, 4:5},6:{7:8, 9:10}]
    不胜感激~
    第 1 条附言  ·  2014-05-15 00:00:08 +08:00
    上面显示结果有误,比如数字1到20,实现结果
    {1:{2:3, 4:5}, 6:{7:8, 9:10}, 11:{12:13, 14:15}, 16:{17:18, 19:20}}
    20 条回复    2014-05-15 16:14:46 +08:00
    Muninn
        1
    Muninn  
       2014-05-14 23:34:35 +08:00
    不太理解
    zedde
        2
    zedde  
    OP
       2014-05-14 23:38:29 +08:00
    @Muninn 应该是{1:{2:3,4:5},6:{7:8,9:10}} 嵌套字典。
    zedde
        3
    zedde  
    OP
       2014-05-14 23:41:47 +08:00
    1-20 类似{1:{2:3, 4:5}, 6:{7:8, 9:10}, 11:{12:13, 14:15}, 16:{17:18, 19:20}}
    fdsfsdfsdf3334
        4
    fdsfsdfsdf3334  
       2014-05-15 00:06:58 +08:00
    # coding=utf-8

    t={} #初始化字典

    for x in range(1,21):
    if x%5==1:
    print x
    t[x]="{"+str(x+1)+":"+str(x+2)+","+str(x+3)+":"+str(x+4)+"}"




    t1= sorted(t.iteritems(), key=lambda d:d[0], reverse = False) #按照键值排序

    print t1
    fdsfsdfsdf3334
        5
    fdsfsdfsdf3334  
       2014-05-15 00:07:38 +08:00
    fdsfsdfsdf3334
        6
    fdsfsdfsdf3334  
       2014-05-15 00:09:27 +08:00
    好像有点问题,你看看能不能用
    fdsfsdfsdf3334
        7
    fdsfsdfsdf3334  
       2014-05-15 00:14:12 +08:00
    print "---------------------------------------------\n\r"
    print "t:\n\r"
    print t
    print t[1]

    print "---------------------------------------------\n\r"
    print "t1:\n\r"
    print t1
    print t1[0][1]
    kxxoling
        8
    kxxoling  
       2014-05-15 00:44:25 +08:00
    我理解你是想通过一个list获取这样的dict,list的长度要能被5整除。
    ```
    l = range(1, 21)
    assert len(l)%5 == 0
    d = {}
    for i in range(0, len(l), 5):
    d[l[i]] = {l[i+1]: l[i+2], l[i+3]: l[i+4]}
    print d
    ```
    delo
        9
    delo  
       2014-05-15 00:44:43 +08:00
    kxxoling
        10
    kxxoling  
       2014-05-15 00:45:56 +08:00
    额,v2ex不支持这么加代码。。。不过逻辑不复杂,我就不用gist了,讲究看看吧~
    palytoxin
        11
    palytoxin  
       2014-05-15 00:51:17 +08:00 via iPhone
    这不是和女神合租的rubyer么,没认错吧
    cbsw
        12
    cbsw  
       2014-05-15 01:02:05 +08:00
    一行代码而已
    {k:{k+1:k+2, k+3:k+4} for k in [5*i+1 for i in range(4)]}
    如果要写成函数,就把最后一个 4 作为参数
    kavinyao
        13
    kavinyao  
       2014-05-15 04:31:20 +08:00   ❤️ 1
    In [13]: {i: {j:j+1 for j in [i+1, i+3]} for i in range(1, 20, 5)}
    Out[13]: {1: {2: 3, 4: 5}, 6: {7: 8, 9: 10}, 11: {12: 13, 14: 15}, 16: {17: 18, 19: 20}}
    daniel7725
        14
    daniel7725  
       2014-05-15 09:30:27 +08:00
    ```
    def test(num):
    if num <5:
    return None
    targe = {}
    for i in xrange(1,num,5):
    tmp_dict = {str(i):{str(i+1):str(i+2),str(i+3):str(i+4)}}
    targe.update(tmp_dict)
    return targe
    daniel7725
        15
    daniel7725  
       2014-05-15 09:34:51 +08:00
    看看v2ex支不支持markdown
    ```
    def test(num):
    if num <5:
    return None
    targe = {}
    for i in xrange(1,num,5):
    tmp_dict = {str(i):{str(i+1):str(i+2),str(i+3):str(i+4)}}
    targe.update(tmp_dict)
    return targe
    ```
    wangyongbo
        16
    wangyongbo  
       2014-05-15 10:52:41 +08:00   ❤️ 2
    { x : { x + 1 :x+ 2, x+ 3: x+ 4} for x in range(1, 20, 5)}
    wangyongbo
        17
    wangyongbo  
       2014-05-15 10:54:08 +08:00
    { x : { i : i + 1 for i in xrange(x+1, x+4, 2)} for x in range(1, 20, 5)}
    toctan
        18
    toctan  
       2014-05-15 13:00:32 +08:00
    Ruby:

    (1..20).step(5).reduce({}) { |r, x| r.merge(x => Hash[*(x + 1..x + 4)]) }

    or the lazy way:

    Hash.new { |h, k| h[k] = Hash[*(k + 1..k + 4)]) }
    hanks315
        19
    hanks315  
       2014-05-15 13:57:40 +08:00
    foomorrow
        20
    foomorrow  
       2014-05-15 16:14:46 +08:00
    n = 2
    dict(map(lambda x:(x,dict(((x+1,x+2),(x+3,x+4)))),[x+1 for x in xrange(0,n*10,5)]))
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5456 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 08:44 · PVG 16:44 · LAX 01:44 · JFK 04:44
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.