Python---List-generated

Source: Internet
Author: User

#  list Generation #  list generation is list comprehensions, which is a very simple but powerful built-in import os#  build that can be used to create a list of Python  [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]l1 = list (range (1),  11)) print (' L1: ',  l1) #  generate  [1*1, 2*2, 3*3, ..., 10*10]# 1. Using loops to generate L2  = []for x in range (1, 11):     l2.append (x * x) Print (' L2: ',  l2) # 2. List Generation L2 = [x * x for x in range (1,  11)]print (' L2: ',  l2) # if condition filter only even squares l3 = [x * x for x in  range (1, 11)  if x % 2 == 0]print (' L3: ',  L3) #  uses a two-layer loop to generate the full array L4  = [m + n for m in  ' ABC '  for n in  ' XYZ ']print (' L4: ',  L4) #  lists all files and directory names under the current directory L5 = [d for d in os.listdir ('. ')] Print (' L5: ',  L5) #  uses two variables to generate LISTD = {' x ':  ' A ',  ' y ':  ' B ',  ' z ':  ' C '}l6 = [k +  ' = '  +  v for k, v in d.items ()]print (' L6: ',  l6) #  all the strings in a list into lowercase l =  [' Hello ',  ' World ',  ' IBM ',  ' Apple ']l7 = [s.lower ()  for s in  L]print (' L7: ',  L7)


Python---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.