arrayList=[[1,2,3],[4,5,6],[7,8,9]]
arrayNew = [yy for yy in arrayList if yy[0]%2==1] #新列表为该行第一个数为偶数。
print(arrayNew)
(2)enumerate同时获取索引和元素值。
def getitemstr(index, element):
return '%d: %s' % (index, element)
def getitemtuple(index,element):
return (index,element)
ListStr = ['Thanks', 'Good morning', 'Good afternoon']
ItemTuple= [getitemtuple(index, element) for index, element in enumerate(ListStr)]
print(ItemTuple)
ItemStr= [getitemstr(index, element) for index, element in enumerate(ListStr)]
print(ItemStr)
(3) map的使用(python3.6需要转换才能成为list)
D=map(float, ['1','2','3','4','5','6'])
print(D) #<map object at 0x0A047A50>
for y in D:
print(y) #通过迭代方式取值。
(4)yield生成器,提高内存效率
def msm(n):
for ss in range(n):
yield ss**2
print(ss)
ff=msm(10)
for i in ff:
print(i)