zoukankan      html  css  js  c++  java
  • Python使用random.shuffle()随机打乱字典排序

    示例.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'}

  • 相关阅读:
    flex-direction
    flex-grow
    Push API
    ServiceWorker.state
    Using Service Workers
    Promise.then
    Promise()
    Using promises
    node-rsa
    async.waterfall
  • 原文地址:https://www.cnblogs.com/gqv2009/p/14303201.html
Copyright © 2011-2022 走看看