count(),列表推导式,transpose()

count()函数 列表推导式

In [85]:
#统计列表每个元素中指定单词出现的个数
words=['apple','pare','banana','and','peach','Anda']
for word in words:
    print(word.lower().count('a'))  #lower()识别大小写
1
1
3
1
1
2
In [86]:
 [word for word in words if word.lower().count('a')>=2]
 
 
Out[86]:
['banana', 'Anda']
In [69]:
strings=['a','bv','tit','apple','ctr']
[x.title() for x in strings if len(x)>2]
 
 
Out[69]:
['Tit', 'Apple', 'Ctr']
In [74]:
list(map(len,strings))
  
Out[74]:
[1, 2, 3, 5, 3]
 

transpose() 转置

In [88]:
import numpy as np
three=np.arange(18).reshape(2,3,3)
 
In [89]:
three
 
Out[89]:
array([[[ 0,  1,  2],
        [ 3,  4,  5],
        [ 6,  7,  8]],

       [[ 9, 10, 11],
        [12, 13, 14],
        [15, 16, 17]]])
In [97]:
 
three.transpose(2,1,0)
Out[97]:
array([[[ 0,  9],
        [ 3, 12],
        [ 6, 15]],

       [[ 1, 10],
        [ 4, 13],
        [ 7, 16]],

       [[ 2, 11],
        [ 5, 14],
        [ 8, 17]]])
 
原文地址:https://www.cnblogs.com/liyun1/p/11261867.html