码迷,mamicode.com
首页 > 编程语言 > 详细

python_code list_3

时间:2017-05-20 22:43:36      阅读:295      评论:0      收藏:0      [点我收藏+]

标签:ack   zip   random   port   fresh   list   pen   module   syntax   

>>> seq=[‘foo‘,‘x41‘,‘?‘,‘***‘]
>>> def func(x):
return x.isalnum()

>>> list(filter(func,seq))
[‘foo‘, ‘x41‘]
>>> seq
[‘foo‘, ‘x41‘, ‘?‘, ‘***‘]
>>> [x for x in seq if x.isalnum(),seq]
SyntaxError: invalid syntax
>>> [x for x in seq if x.isalnum()]
[‘foo‘, ‘x41‘]
>>> list(filter(lambda x: x.isalnum(),seq))
[‘foo‘, ‘x41‘]
>>> list(filter(None,[1,2,0,4,0,6]))
[1, 2, 4, 6]
>>> [1,2,3,4,5]-[2,3]
Traceback (most recent call last):
File "<pyshell#11>", line 1, in <module>
[1,2,3,4,5]-[2,3]
TypeError: unsupported operand type(s) for -: ‘list‘ and ‘list‘
>>> import random
>>> x=[random.randint(1,100)for i in range(10)]
>>> x
[82, 2, 90, 58, 81, 18, 42, 40, 19, 94]
>>> list(map(lambda i: i+5,x))
[87, 7, 95, 63, 86, 23, 47, 45, 24, 99]
>>> y=[random.randint(1,10)for i in range(10)]
>>> y
[4, 8, 10, 10, 7, 2, 9, 1, 7, 4]
>>> import operator
>>> sum(map(operator.mul,x,y))
3354
>>> sum((i*j for i,j in zip(x,y)))
3354
>>> list(map(operator.add,x,y))
[86, 10, 100, 68, 88, 20, 51, 41, 26, 98]
>>> list(map(lambda i,j: i+j, x, y))
[86, 10, 100, 68, 88, 20, 51, 41, 26, 98]
>>> aList=[x*x for x in range(10)]
>>>
>>> aList
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
>>> aList=[]
>>> for x in range(10)
SyntaxError: invalid syntax
>>> for x in range(10):
aList.append(x*x)


>>> aList
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
>>> aList=list(lambda x: x*x, range(10))
Traceback (most recent call last):
File "<pyshell#32>", line 1, in <module>
aList=list(lambda x: x*x, range(10))
TypeError: list() takes at most 1 argument (2 given)
>>> aListt=list(map(lambda x:x*x, range(10)))
>>> aListt
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
>>> freshfruit=[‘banana‘,‘loganberry‘,‘passion fruit‘]
>>> aList=[w.strip() for w in freshfruit]
>>> aList
[‘banana‘, ‘loganberry‘, ‘passion fruit‘]
>>> freshfruit=[‘banana‘,‘loganberry‘,‘passion fruit‘]
>>> aList=[]
>>> for item in freshfruit:
aList.append(item.strip())


>>> aList
[‘banana‘, ‘loganberry‘, ‘passion fruit‘]
>>> freshfruit=[‘banana‘,‘loganberry‘,‘passion fruit‘]
>>> aList =list(map(lambda x: x.strip(),freshfruit))
>>> aList
[‘banana‘, ‘loganberry‘, ‘passion fruit‘]
>>> freshfruit=[‘banana‘,‘loganberry‘,‘passion fruit‘]
>>> aList=list(map(str.strip,freshfruit))
>>> aList
[‘banana‘, ‘loganberry‘, ‘passion fruit‘]
>>> sum([2**x for x in range(64)])
18446744073709551615

python_code list_3

标签:ack   zip   random   port   fresh   list   pen   module   syntax   

原文地址:http://www.cnblogs.com/cmnz/p/6883350.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!