2021-04-13 Python 随机列表、集合、元组、字典的生成和排序
发布日期:2021-05-04 07:31:33 浏览次数:12 分类:技术文章

本文共 2990 字,大约阅读时间需要 9 分钟。

随机列表

>>> from random import randrange as rand>>> [rand(0,10) for i in range(8)][3, 1, 0, 9, 2, 9, 9, 5]>>> [rand(0,10) for i in range(8)][9, 2, 4, 9, 3, 4, 2, 8]>>> [rand(0,10) for i in range(8)][7, 2, 2, 4, 5, 0, 1, 8]>>> [rand(0,10) for i in range(8)][9, 1, 2, 0, 1, 5, 9, 0]>>>

列表排序

>>> from random import randrange as rand>>> listA=[rand(0,10) for i in range(8)]>>> listA[1, 2, 2, 4, 8, 6, 2, 9]>>> listB=sorted(listA)>>> listB[1, 2, 2, 2, 4, 6, 8, 9]>>>

随机集合

>>> from random import randrange as rand>>> {rand(0,10) for i in range(8)}{2, 3, 5, 6, 8, 9}>>> {rand(0,10) for i in range(8)}{0, 1, 3, 8, 9}>>> {rand(0,10) for i in range(8)}{0, 1, 3, 4, 5, 6, 7, 8}>>> {rand(0,10) for i in range(8)}{0, 2, 3, 4}>>>

因为集合set不允许有重复值,这种方式生成实际上每次都有8个随机数生成,只是最后结果已去掉了重复数字,所以集合的元素个数也变随机的了。还有一点:这样生成的集合已是升序排列。

集合排序

直接对一个集合用sorted()排序会被强制转成列表的,用set()转回来即可。

>>> setA={5, 1, 9, 4, 2, 6, 3, 8}>>> sorted(setA)[1, 2, 3, 4, 5, 6, 8, 9]>>> set(sorted(setA)){1, 2, 3, 4, 5, 6, 8, 9}>>>

随机元组

与上面用同样的表达式,只是[] {} 换作(),但是并没有得到想要的“随机元组”,意外的产品:迭代器

>>> from random import randrange as rand>>> (rand(0,10) for i in range(8))
at 0x00000000029E6DD0>>>> (rand(0,10) for i in range(8))
at 0x00000000029E6CF0>>>> g=(rand(0,10) for i in range(8))>>> type(g)
>>> for i in g: print(i)68975399>>>

用tuple()转换一下这个迭代器即可得到随机元组了: 

>>> from random import randrange as rand>>> g=(rand(0,10) for i in range(8))>>> tuple(g)(5, 0, 4, 7, 5, 9, 5, 9)>>> tuple(rand(0,10) for i in range(8))(8, 3, 2, 5, 0, 7, 0, 9)>>> tuple(rand(0,10) for i in range(8))(5, 8, 1, 2, 3, 4, 5, 4)>>> tuple(rand(0,10) for i in range(8))(7, 8, 9, 0, 5, 8, 2, 2)>>>

元组排序

直接对它排序也被转成列表,也说得过去,因为元组的元素不可变,再用tuple()转回来即可。

>>> from random import randrange as rand>>> tupleA=tuple(rand(0,10) for i in range(8))>>> tupleB=sorted(tupleA)>>> tupleB[0, 2, 2, 2, 3, 4, 5, 7]>>> tupleB=tuple(sorted(tupleA))>>> tupleB(0, 2, 2, 2, 3, 4, 5, 7)>>>

随机字典

字典的键是唯一的,所以这样生成的键值对个数也是不定的。

>>> from random import randrange as rand>>> {rand(0,10):rand(0,10) for i in range(8)}{6: 7, 5: 3, 9: 0, 1: 9, 0: 5, 3: 6}>>> {rand(0,10):rand(0,10) for i in range(8)}{7: 2, 0: 2, 4: 7}>>> {rand(0,10):rand(0,10) for i in range(8)}{1: 7, 8: 8, 5: 6, 0: 4, 6: 6}>>> {rand(0,10):rand(0,10) for i in range(8)}{3: 6, 1: 6, 4: 4, 0: 1, 7: 5, 8: 5, 2: 7}>>> dictA={rand(0,10):rand(0,10) for i in range(8)}>>> type(dictA)
>>>

固定键值对个数的生成法:

>>> from random import randrange as rand>>> {i+1:rand(0,10) for i in range(8)}{1: 1, 2: 6, 3: 3, 4: 9, 5: 4, 6: 4, 7: 6, 8: 3}>>> {i+1:rand(0,10) for i in range(8)}{1: 9, 2: 3, 3: 4, 4: 9, 5: 5, 6: 4, 7: 3, 8: 8}>>>

字典排序

直接对字典排序,只是生成一个升序的临时列表,字典本身并没有被排序。按那个临时列表的顺序,读取对应的键值即可生成升序字典了。

>>> from random import randrange as rand>>> dictA={rand(0,10):rand(0,10) for i in range(8)}>>> dictA{8: 8, 4: 9, 0: 2, 6: 6, 3: 9, 9: 0}>>> sorted(dictA)[0, 3, 4, 6, 8, 9]>>> dictA{8: 8, 4: 9, 0: 2, 6: 6, 3: 9, 9: 0}>>> dictB={i:dictA[i] for i in sorted(dictA)}>>> dictB{0: 2, 3: 9, 4: 9, 6: 6, 8: 8, 9: 0}>>> type(dictB)
>>>

 

 

上一篇:2021-04-14 Python tkinter库之Toplevel 子窗口与主窗口之间的联系
下一篇:2021-04-12 Python 空元组、空列表、空字典、空集合

发表评论

最新留言

路过,博主的博客真漂亮。。
[***.116.15.85]2025年03月12日 02时50分55秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章