码迷,mamicode.com
首页 > 编程语言 > 详细

Python使用random.shuffle()随机打乱字典排序

时间:2021-01-21 10:36:13      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:dom   cts   随机   def   shu   使用   port   product   rod   

示例.1

import random
from random import shuffle
x = [[i] for i in range(10)]
shuffle(x)
print(x)

运行结果:
[[1], [2], [5], [0], [7], [9], [3], [8], [4], [6]]
[[6], [0], [7], [1], [3], [9], [5], [2], [4], [8]]

示例.2

dicts = {
    "productCode": "xyd",
    "account": "phone",
    "appType": "ios",
    "channelCode": "AppStore",
    "event": "FORGET_PWD"
}

def random_dic(dicts):
    dict_key_ls = list(dicts.keys())
    random.shuffle(dict_key_ls)
    new_dic = {}
    for key in dict_key_ls:
        new_dic[key] = dicts.get(key)
    return new_dic


print(random_dic(dicts))

运行结果:
{‘channelCode‘: ‘AppStore‘, ‘productCode‘: ‘xyd‘, ‘appType‘: ‘ios‘, ‘event‘: ‘FORGET_PWD‘, ‘account‘: ‘phone‘}
{‘event‘: ‘FORGET_PWD‘, ‘account‘: ‘phone‘, ‘productCode‘: ‘xyd‘, ‘appType‘: ‘ios‘, ‘channelCode‘: ‘AppStore‘}

Python使用random.shuffle()随机打乱字典排序

标签:dom   cts   随机   def   shu   使用   port   product   rod   

原文地址:https://www.cnblogs.com/gqv2009/p/14303201.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!