Home | 简体中文 | 繁体中文 | 杂文 | Github | 知乎专栏 | 51CTO学院 | CSDN程序员研修院 | OSChina 博客 | 腾讯云社区 | 阿里云栖社区 | Facebook | Linkedin | Youtube | 打赏(Donations) | About
知乎专栏多维度架构

1.6. 数据结构

1.6.1. List

随机抽取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)	
		
		

1.6.2. Set

		
>>> 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'}		
		
		

1.6.3. Dict 字典

1.6.3.1. 随机选择字典的 key 和 value

随机字典的 key 和 value

			
			<![CDATA[
names = {1: '张三', 2: '李四', 3: '王五', 4: '赵六', 5: '牛七', 6: '马八'}
print(random.choice(list(names.keys())))
print(random.choice(list(names.values())))			
			
			

1.6.3.2. 字典合并

			
dict1 = {'name','neo'}
dict2 = {'nickname','netkiller'}
dict1.update(dict2)
print(dict1)			
			
			

			
{'name', 'neo', 'nickname', 'netkiller'}			
			
			

1.6.3.3. 取最大值

			

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)