Python使用random.shuffle()随机打乱字典排序
2021-03-06 08:28
标签:dom cts 随机 def shu 使用 port product rod 运行结果: 运行结果: Python使用random.shuffle()随机打乱字典排序 标签:dom cts 随机 def shu 使用 port product rod 原文地址:https://www.cnblogs.com/gqv2009/p/14303201.html示例.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()随机打乱字典排序
文章链接:http://soscw.com/essay/60776.html