Python list comprehensions (list-generated)

Source: Internet
Author: User
Tags python list

List generation: Creating a list


First, the normal creation list

#!/usr/bin/python

#common Establish
Lis1 = [];
For x in range (1, 10):
Lis1.append (x);
Print "Lis1:", lis1;


Ii. List-Generated

#List comprehensions
LIS2 = [x for x in range (1, 10)]
Print "Lis2:", Lis2;


#also can choose the even number in list
LIS3 = [x * x for x in range (1, ten) if x%2 = = 0]
Print "Lis3:", Lis3;


#two for in list
LIS4 = [x + y for x in ' ABC ' for y in ' XYZ ']
Print "LIS4:", LIS4;


#show the file in directory
Import OS; #导入OS模块
LIS5 = [D for D in Os.listdir ('. ')]
Print lis5;


#convert all Big_write string to Small_write
L = [' ABC ', ' EFG ', ' Hij ', ' 8 '] #只能为char类型, other types prompt error
LIS6 = [S.lower () for S in L] #lower () is a built-in function that converts uppercase to lowercase
Print LIS6;


Python list comprehensions (list-generated)

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.