Python uses loops to generate a list

Source: Internet
Author: User

#-*-coding:utf-8-*-squares=[]for x in range: Squares.append (x**2) print squares#[0, 1, 4, 9, 16, 25, 36, 4 9,,, Bayi] #等价于squares =[x**2 for x in range (Ten)]print squares#[0, 1, 4, 9, 1,,,, 81]combs=[]for x in [ , 2,3]: for y in [3,1,4]: If X!=y:combs.append ((x, y)) print combs# equivalent to: combs=[(x, y) for x in [1,  2,3] for y in [3,1,4] if X!=y]print combs#[(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]vec=[-4,-2,0,2,4]print [X*2 for X in Vec]#[-8, -4, 0, 4, 8]print 30* ' * ', ' Print [x for x in VEC if x>=0] ', 30* ' * ' Print [x for x in VEC   If x>=0]#[0, 2, 4]print [ABS (x) for x in Vec]#[4, 2, 0, 2, 4]freshfruit=[' banana ', ' apple ', ' loganberry ', ' passion  Fruit '] #strip () Remove the string end-to-end space print [Weapon.strip () for weapon in freshfruit]#[' banana ', ' apple ', ' loganberry ', ' passion Fruit ']print 30* ' * ', "[(x,x**2) for X in range (6)]", 30* ' * ' Print [(x,x**2)-X in range (6)]#[(0, 0), (1, 1), (2, 4) , (3, 9), (4, 16), (5, 25)]vec=[[1,2,3],[4,5,6],[7,8,9]]print [num for elem in VEC for num in Elem]#[1, 2, 3, 4, 5, 6, 7, 8, 9]matrix=[[1,2,3  , 4],[5,6,7,8],[9,10,11,12]] #list中的每个元素都是list, the elements in each list element in the list are combined by index into a new listprint [[Row[i] for row in the matrix] for I in range (4)]#[[1, 5, 9], [2, 6, ten], [3, 7, one], [4, 8, 12]]transposed=[]for I in range (4): Transposed.append ([row[i ] for row in matrix]) print transposed#[[1, 5, 9], [2, 6, ten], [3, 7, one], [4, 8,]] #等价于: transposed=[]for i in Ran GE (4): transposed_row=[] for row in Matrix:transposed_row.append (Row[i]) transposed.append (transposed_       Row) print transposed# equivalent to zip (*matrix) print zip (*matrix) #[(1, 5, 9), (2, 6, 10), (3, 7, 11), (4, 8, 12)]

In addition, map () can also return List,map () is a Python built-in high-order function that receives a function f and a list, and by putting the function f on each element of the list in turn, gets a new list and returns.
#lambda表示匿名函数, the X in front of the colon is the function argument, the colon is followed by an expression, and the anonymous function has a limit, that is, there can be only one expression, without writing return, the return value is the result of the expression.

Squares=map (lambda x:x**2,range) print   

Python uses loops to generate a list

Related Article

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.