【Python基础】以条件数组、匹配检查另一个数组的例子
返回: Python基础 索引页
以条件数组、匹配检查另一个数组的例子。
对如下的数组:
[Actin --3][0] [conditon aa]
[conditon kk]
[conditon rr]
[conditon cc]
有另外一个条件数组:
"condition aa"
"condition kk"
"condition cc"
如果对条件数组的每一条记录,
和上述数组的第三维中任何一条相等,则计数加1
最后统计 符合条件的有几条,将结果 送入 第一个数组的 第二维。
对上述例子,结果会变成:
[Actin --3][3] [conditon aa]
[conditon kk]
[conditon rr]
[conditon cc]
程序如下:
#### def get action-plan-list actionList = [] tmpFactor= [] tmpFactor.append("[Action --1]") tmpFactor.append(0) contents = [] contents.append("condition aa") contents.append("condition kk") contents.append("condition rr") contents.append("condition cc") actionList.append(tmpFactor) actionList.append(contents) print(actionList) ##begin to serach tmpVals = actionList[1] #### def prepare-condition() KeyWord = [] KeyWord.append("condition aa") KeyWord.append("condition kk") KeyWord.append("condition cc") #### def make-search() tmpCnt = 0 for j in range (len(tmpVals)): curVal = tmpVals[j] ## for i in range(len(KeyWord)): ##print (curVal) ##print ( KeyWord[i] ) if (curVal == KeyWord[i]): tmpCnt = tmpCnt +1 ## ## ## #### end of action-plan-list iteration actionList[0][1] = tmpCnt print(actionList)
运行结果如下:
[['[Action --1]', 0], ['condition aa', 'condition kk', 'condition rr', 'condition cc']]
[['[Action --1]', 3], ['condition aa', 'condition kk', 'condition rr', 'condition cc']]
返回: Python基础 索引页