列表推导式都是按for循环的顺序来排列的,可是集合推导式中除了纯数字的集合外,有字符串的集合都是乱序的,到底是按什么规律排的?有谁知道什么原理?
Python 3.8.8 (tags/v3.8.8:024d805, Feb 19 2021, 13:08:11) [MSC v.1928 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license()" for more information. >>> [chr(i+48) for i in range(10)] ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'] >>> ["%dA"%i for i in range(10)] ['0A', '1A', '2A', '3A', '4A', '5A', '6A', '7A', '8A', '9A'] >>> ["A%d"%i for i in range(10)] ['A0', 'A1', 'A2', 'A3', 'A4', 'A5', 'A6', 'A7', 'A8', 'A9'] >>> {i for i in range(10)} {0, 1, 2, 3, 4, 5, 6, 7, 8, 9} >>> {"A%d"%i for i in range(10)} {'A0', 'A5', 'A3', 'A1', 'A4', 'A6', 'A2', 'A9', 'A7', 'A8'} >>> {"A%d"%i for i in range(10)} {'A0', 'A5', 'A3', 'A1', 'A4', 'A6', 'A2', 'A9', 'A7', 'A8'} >>> {"%dA"%i for i in range(10)} {'8A', '9A', '4A', '1A', '7A', '6A', '2A', '0A', '3A', '5A'} >>> {"%dA"%i for i in range(10)} {'8A', '9A', '4A', '1A', '7A', '6A', '2A', '0A', '3A', '5A'} >>> {i for i in range(10)} {0, 1, 2, 3, 4, 5, 6, 7, 8, 9} >>> {"%dA"%i for i in range(10)}|{i for i in range(10)} {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, '6A', '2A', '3A', '5A', '8A', '1A', '7A', '0A', '4A', '9A'} >>> {i for i in range(10)}|{"%dA"%i for i in range(10)} {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, '6A', '2A', '3A', '5A', '8A', '1A', '7A', '0A', '4A', '9A'} >>> {i for i in range(10)}|{"A%d"%i for i in range(10)} {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'A0', 'A5', 'A1', 'A4', 'A3', 'A6', 'A2', 'A9', 'A7', 'A8'} >>> {chr(i+48) for i in range(10)} {'3', '4', '9', '2', '8', '5', '6', '7', '0', '1'} >>> {chr(i+48) for i in range(10)} {'3', '4', '9', '2', '8', '5', '6', '7', '0', '1'} >>>
纯数字的,即使是随机生成的集合也会以升序排列:
>>> from random import randint as rnd >>> {rnd(0,20) for i in range(20)} {0, 1, 2, 3, 4, 5, 6, 8, 11, 12, 16, 19} >>> {rnd(0,20) for i in range(20)} {0, 2, 4, 5, 6, 8, 12, 13, 14, 15, 18} >>> {rnd(0,20) for i in range(20)} {0, 3, 6, 7, 8, 9, 10, 12, 13, 14, 18, 20} >>>
列表强制转换到集合,也是乱序的,顺序还与推导式直接生成的不一致。又是为什么?
>>> ["A%d"%i for i in range(10)] ['A0', 'A1', 'A2', 'A3', 'A4', 'A5', 'A6', 'A7', 'A8', 'A9'] >>> set(["A%d"%i for i in range(10)]) {'A0', 'A5', 'A3', 'A1', 'A4', 'A6', 'A2', 'A9', 'A7', 'A8'} >>> {"%dA"%i for i in range(10)} {'8A', '9A', '4A', '1A', '7A', '6A', '2A', '0A', '3A', '5A'} >>> sorted({"%dA"%i for i in range(10)}) ['0A', '1A', '2A', '3A', '4A', '5A', '6A', '7A', '8A', '9A'] >>> set(sorted({"%dA"%i for i in range(10)})) {'8A', '9A', '4A', '1A', '7A', '6A', '2A', '0A', '3A', '5A'} >>>
同样的方法,在字典推导式中也升序排列的。
>>> {i for i in range(10)} {0, 1, 2, 3, 4, 5, 6, 7, 8, 9} >>> {"A%d"%i for i in range(10)} {'A8', 'A4', 'A2', 'A3', 'A9', 'A0', 'A5', 'A6', 'A1', 'A7'} >>> type({"A%d"%i for i in range(10)}) <class 'set'> >>> >>> {"A%d"%i:i for i in range(10)} {'A0': 0, 'A1': 1, 'A2': 2, 'A3': 3, 'A4': 4, 'A5': 5, 'A6': 6, 'A7': 7, 'A8': 8, 'A9': 9} >>> type({"A%d"%i:i for i in range(10)}) <class 'dict'> >>> >>> {i:"A%d"%i for i in range(10)} {0: 'A0', 1: 'A1', 2: 'A2', 3: 'A3', 4: 'A4', 5: 'A5', 6: 'A6', 7: 'A7', 8: 'A8', 9: 'A9'} >>>