#!/usr/bin/python
# -*- coding: utf-8 -*-
print('List Comprehensions!!')
mylist=[x*x for x in range(1,11)]
print(mylist)
mylist2 = [x*x for x in range(1,10) if x%2 == 0]
print(mylist2)
mylist3 = [m+n for m in 'ABC' for n in 'XYZ']
print(mylist3)
import os
mylist4 =[d for d in os.listdir('.')]
print(mylist4)
d = {'x': 'A', 'y': 'B', 'z': 'C' }
for k,v in d.items():
print(k,'=',v)
mylist5 = [k+'='+v for k,v in d.items() ]
print(mylist5)
L = ['Hello', 'World', 'IBM', 'Apple']
lowerL = [s.lower() for s in L]
print(lowerL)
L2 = ['Hello', 'World', 18, 'Apple', None]
lowerL2 = [s.lower() for s in L2 if isinstance(s,str)]
print(lowerL2)
Python练习-- List Comprehensions
最新推荐文章于 2024-09-04 20:18:34 发布