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
Flygar
V2EX  ›  Python

小白提问:对输入的 10 个按逗号分隔的数字进行排序。

  •  
  •   Flygar · 2017-11-12 14:08:59 +08:00 · 743 次点击
    这是一个创建于 2354 天前的主题,其中的信息可能已经有所发展或是发生改变。
    s = input('input:').split(',')
    n = [int(x) for x in s]
    n.sort()
    print(n)
    
    

    可不可以在第一行代码合适的地方插入 int,使每个元素按逗号分隔并转换为 int 型?
    如果可以的话能不能把 sort(),也放到第一行去。
    家里断网了,查不了百度,不会翻墙。
    好惨,大家帮帮我。

    8 条回复    2017-11-12 18:51:20 +08:00
    fzinfz
        1
    fzinfz  
       2017-11-12 14:28:03 +08:00   ❤️ 3
    print(sorted(list(map(lambda x: int(x), input('input:').split(',')))))
    Flygar
        2
    Flygar  
    OP
       2017-11-12 14:37:56 +08:00
    @fzinfz
    谢谢🙏太 6 了,我好好看看
    GuuJiang
        3
    GuuJiang  
       2017-11-12 14:59:33 +08:00 via iPhone   ❤️ 2
    @fzinfz 然而并不需要 lambda,直接 map(int,input().split(','))即可
    CSM
        4
    CSM  
       2017-11-12 15:02:19 +08:00 via Android   ❤️ 2
    那层 list 也不需要
    fzinfz
        5
    fzinfz  
       2017-11-12 15:06:02 +08:00
    @GuuJiang #3
    @CSM #4
    谢谢 2 位,学到了~~
    简化后的:print(sorted(map(int, input('input:').split(','))))
    orzfly
        6
    orzfly  
       2017-11-12 15:19:42 +08:00   ❤️ 3
    (off-topic) 话说我有个疑问百思不得其解(其实主要是好奇):断网了上不了百度,是怎么上 V2EX 的……?
    noe132
        7
    noe132  
       2017-11-12 18:50:52 +08:00
    看了 1 楼的嵌套有种 lisp 的感觉。。。
    如果是 JS 的话

    console.log(input.split(',').map(v => parseInt(v)).sort())
    winglight2016
        8
    winglight2016  
       2017-11-12 18:51:20 +08:00
    @orzfly 有用爱发电,当然也有用爱上网的,楼主显然是 V2 真爱粉~~~
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5786 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 02:54 · PVG 10:54 · LAX 19:54 · JFK 22:54
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.