码迷,mamicode.com
首页 > 编程语言 > 详细

python-list comprehension

时间:2018-05-29 21:13:32      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:bin   list   types.h   href   class   color   核心   功能   int   

list comprehension

Using a list comprehension: [x for x in iterable] to create list instance

[expresion for x in iterabel ] 列表推导式的核心在expression.

 

 1 #!/usr/bin/env python3
 2 #-*- coding:utf-8 -*-
 3 ############################
 4 #File Name: test_list_comprehension.py
 5 #Author: frank
 6 #Mail: frank0903@aliyun.com
 7 #Created Time:2018-05-29 20:33:56
 8 ############################
 9 import numpy as np
10 
11 seed = 123
12 
13 #模拟数据集标签
14 #基于seed产生随机数
15 rdg = np.random.RandomState(seed)
16 #先生成一个5行2列的随机数矩阵
17 X = rdg.rand(5,2)
18 #每次取出一行,判断如果两个数的和小于1,给a赋值1;如果两个数的和不小于1,给a赋值
19 #[expression for x in iterable]                                                
20 Y = [[int(x0+x1<1)] for (x0,x1) in X]
21 print("X:{}".format(X))
22 print("Y:{}".format(Y))

列表推推导式有很强的扩展性, expression 不同,可执行的功能就大不一样。

 

python-list comprehension

标签:bin   list   types.h   href   class   color   核心   功能   int   

原文地址:https://www.cnblogs.com/black-mamba/p/9107844.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!