知乎专栏 | 多维度架构 |
随机抽取list成员
import random # random choice from a list for i in range(5): print random.choice([1, 2, 3, 5, 9])
test = [] test.append('1') test.append('b') test.append('c') print(test)
>>> set = {'a','b','c'} >>> set set(['a', 'c', 'b']) >>> set = {"one", "two", "three","one"} >>> set set(['three', 'two', 'one'])
>>> set.add('four') >>> set set(['four', 'three', 'two', 'one'])
>>> ','.join(set); 'four,three,two,one' >>> set('four,three,two,one'.split(',')); {'one', 'four', 'three', 'two'} >>> fruit = [ 'banana', 'orange', 'pineapple' ] >>> set(fruit) {'banana', 'pineapple', 'orange'}
随机字典的 key 和 value
<![CDATA[ names = {1: '张三', 2: '李四', 3: '王五', 4: '赵六', 5: '牛七', 6: '马八'} print(random.choice(list(names.keys()))) print(random.choice(list(names.values())))
dict1 = {'name','neo'} dict2 = {'nickname','netkiller'} dict1.update(dict2) print(dict1)
{'name', 'neo', 'nickname', 'netkiller'}
data = [ { "question": "为什么过只加汇摇晃", "answer": "因为支架是由很多支索小圆轮组成的,所以吊厢过索轮时会有轻微的震动和摇晃,这些都是正常的", "ratio": 0, "distance": 41.112388610839844 }, { "question": "吊厢过之家为什么会摇晃", "answer": "因为支架是由很多支索小圆轮组成的,所以吊厢过索轮时会有轻微的震动和摇晃,这些都是正常的", "ratio": 0, "distance": 42.78510284423828 }, { "question": "嗦道为啥会摇晃", "answer": "因为支架是由很多支索小圆轮组成的,所以吊厢过索轮时会有轻微的震动和摇晃,这些都是正常的", "ratio": 1, "distance": 44.00849914550781 } ] ret = max(data, key=lambda dic: dic['ratio']) print(ret)