关于PY的推导式

列表推导式:
	In [26]: [i*2 for i in range(10)]
	Out[26]: [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
字典推导式:
	In [29]: {key:"what"  for key in range(10) }
	Out[29]:
		{0: 'what',
		 1: 'what',
		 2: 'what',
		 3: 'what',
		 4: 'what',
		 5: 'what',
		 6: 'what',
		 7: 'what',
		 8: 'what',
		 9: 'what'}
	In [35]: def name():
    ...:     return "hahaha"
    ...:

	In [36]: {key:name()  for key in range(10) }
	Out[36]:
	{0: 'hahaha',
	 1: 'hahaha',
	 2: 'hahaha',
	 3: 'hahaha',
	 4: 'hahaha',
	 5: 'hahaha',
	 6: 'hahaha',
	 7: 'hahaha',
	 8: 'hahaha',
	 9: 'hahaha'}

  

posted @ 2017-08-17 15:50  两只老虎111  阅读(255)  评论(0编辑  收藏  举报