可变大小的数组匹配 [英] variable size array matching
本文介绍了可变大小的数组匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这是基于第一列排序的数据文件
I have a data file which is sorted based on the first column
1 3 2
3 6
4 8 5 6 2
4 9
5 2 2
有是有三个项目的关键,说看到= [4 8 5]
我想上面的数组中进行搜索。由于某些行的不到三列,下面code未能比较,我知道
There is a key with three items, say seen = [4 8 5]
that I want to search in the above array. Since some of the rows have less than three columns, the following code fails to compare and I know that
take = [row for row in lines if row[0] == seen[0] and row[1] == seen[1] and row[2] == seen[2]]
所以,我应该有少于三列的行吗?
So what should I do for the rows that have less than three columns?
推荐答案
使用切片,您不必手动检查所有3项,并检查长度:
Using slice, you don't need to check all 3 items manually and check length:
take = [row for row in lines if row[:3] == seen]
这篇关于可变大小的数组匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文