Python list parsing

Source: Internet
Author: User

#! /bin/env python


#这句会生成一个列表 [0,1,2,3,4,5,6,7,8,9]

List_a = [item for item ' in range (10)]


#下面对这个列表进行筛选 (conditions are even)

List_b = [item for item in List_a if (item%2==0)]

#这样就可以筛选出列表中的偶数


#其实列表解析可以看成是这样的

#[variable (or variable operation expression) iterator (an iterative object is equivalent to a For loop) filter condition]

#下面这个会生成0-10 squared

List_c = [item**2 for item in range (11)]


#当然也是可以添加判断条件的

#list_c = [item**2 for item in range (one) if (item%2 ==0)]

#请读者自行上机调试

#俗话说: The eye times, better hand over again


#如果对本文内容或者结果有疑问的话, please leave a message, I will reply as soon as possible, common learning, common progress.

#质疑以及好奇是学习最好最快的方式

This article is from the "phize" blog, make sure to keep this source http://12756301.blog.51cto.com/12746301/1910706

Python list parsing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.