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

python,能用一行语句,生成 10 个随机的奇数嘛?

  •  
  •   csdreamdong · 2016-06-18 19:15:31 +08:00 · 7644 次点击
    这是一个创建于 2868 天前的主题,其中的信息可能已经有所发展或是发生改变。
    22 条回复    2016-06-19 20:06:18 +08:00
    ioven
        1
    ioven  
       2016-06-18 19:25:00 +08:00
    mengzhuo
        2
    mengzhuo  
       2016-06-18 19:25:08 +08:00
    都没有范围……我设为小于 100 , 大于 0
    >>> map(lambda x:x if x%2 == 1 else x - 1, [random.randint(0,100) for i in range(10)])
    >>> [59, 85, 37, 21, 75, 7, 41, 13, 99, 89]
    pimin
        3
    pimin  
       2016-06-18 19:33:05 +08:00
    def random_str(size=6, chars=string.ascii_uppercase + string.digits):
    return ''.join(random.choice(chars) for _ in range(size))
    pimin
        4
    pimin  
       2016-06-18 19:33:56 +08:00
    没看清题目..我的锅
    lightening
        5
    lightening  
       2016-06-18 19:36:05 +08:00
    [random.randint(0, 50) * 2 + 1 for x in range(10)]
    thekoc
        6
    thekoc  
       2016-06-18 19:38:14 +08:00
    @mengzhuo 你这个有可能生成 - 1 了……
    skydiver
        7
    skydiver  
       2016-06-18 19:41:46 +08:00 via iPad   ❤️ 1
    @mengzhuo 你这个不随机了

    应该直接生成 n 然后输出 2n+1
    allenling
        8
    allenling  
       2016-06-18 19:50:38 +08:00
    感觉没什么要求呀
    直接 random.choice 或者 random.randrange 不就好了
    practicer
        9
    practicer  
       2016-06-18 20:25:31 +08:00   ❤️ 1
    import random import 语句也算吗?

    random.sample([i for i in xrange(10000) if i % 2 == 1], 10)

    random.sample(iter, k) 从一组 iter 对象里面随机选 k 个元素出来,
    因此只要先先定义一个奇数列表,放进去就行了
    csdreamdong
        10
    csdreamdong  
    OP
       2016-06-18 20:27:33 +08:00
    @practicer 啊哈哈。我喜欢这个方法,, 2n+1 虽然也能实现,,但有点 tricky
    congeec
        11
    congeec  
       2016-06-18 21:55:23 +08:00
    @practicer
    __import__("random").sample([i for i in range(10000) if i % 2 == 1], 10)
    aubee
        12
    aubee  
       2016-06-18 22:17:35 +08:00
    [random.randint(0, 100) for i in range(10)]
    这个有什么问题嘛?
    zhuangzhuang1988
        13
    zhuangzhuang1988  
       2016-06-18 22:59:13 +08:00
    用好 eval/exec 啥都是一行解决
    exec "import random\nprint [random.randint(0, 50) * 2 + 1 for x in range(10)]"
    practicer
        14
    practicer  
       2016-06-18 23:05:31 +08:00
    @congeec 原来可以这样写
    zhangbohun
        15
    zhangbohun  
       2016-06-18 23:35:03 +08:00
    偶数加一
    happyz90
        16
    happyz90  
       2016-06-19 00:06:17 +08:00 via Android
    happyz90
        17
    happyz90  
       2016-06-19 00:06:56 +08:00 via Android
    @happyz90
    想写 8+1 。。。为啥写了 5+1😂
    ryd994
        18
    ryd994  
       2016-06-19 00:42:03 +08:00 via Android
    为什么 2n+1 不好?
    debiann
        19
    debiann  
       2016-06-19 00:47:22 +08:00 via iPhone
    @skydiver 他的做法是可以的,因为本身用的是均匀分布,唯一的问题是-1 应该改成+1
    debiann
        20
    debiann  
       2016-06-19 00:51:22 +08:00 via iPhone
    @debiann 还有取值范围应该是个偶数,比如[1, 100]这样
    mengzhuo
        21
    mengzhuo  
       2016-06-19 08:52:29 +08:00 via iPhone
    RqPS6rhmP3Nyn3Tm
        22
    RqPS6rhmP3Nyn3Tm  
       2016-06-19 20:06:18 +08:00 via Android
    @lightening 本题最佳,小学知识好多人都忘了。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3685 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 43ms · UTC 05:04 · PVG 13:04 · LAX 22:04 · JFK 01:04
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.