Demo在Python
import math
a = [-3,5,2,-10,7,8]
b = 'abc‘
c = [2*s for s in a] # c = [-6,10,4,-20,14,16]
d = [s for s in a if s >= 0] # d = [5,2,7,8]
e = [(x,y) for x in a # e = [(5,'a'),(5,'b'),(5,'c'),
for y in b # (2,'a'),(2,'b'),(2,'c'),
if x > 0] # (7,'a'),(7,'b'),(7,'c'),
# (8,'a'),(8,'b'),(8,'c')]
f = [(1,2), (3,4), (5,6)]
g = [math.sqrt(x*x+y*y) # g = [2.23606, 5.0, 7.81024]
for x,y in f]
h = reduce(lambda x,y: x+y, # 平方根的和
[math.sqrt(x*x+y*y)
for x,y in f])
點(diǎn)擊加載更多評(píng)論>>