根据python中的条件列表搜索多个列表的最佳方法是什么?

时间:2017-06-15 15:22:07

标签: python list

假设我有:

list1 = ['section', 'section', 'section', 3, 'mirror', 0, 'blue']
list2 = ['section', 'section', 'section', 3, 'mirror']
list3 = ['section', 'section', 'section', 3, 'light',]
list4 = ['subsection', 'section', 'section', 3, 'light',]

使用下面的criteria_list选择仅部分匹配criteria_list的起始元素的列表并排除某些关键字元素(例如mirror元素)的最佳方式是什么?

criteria_list = ['section', 'section', 'section', 3]

期望的匹配将是:

list3 = ['section', 'section', 'section', 3, 'light',]

这是因为它部分匹配criteria_list并且不包含字符串'mirror'

我希望我的问题很明确,我们将不胜感激。

3 个答案:

答案 0 :(得分:0)

list_of_lists = [
    ['section', 'section', 'section', 3, 'mirror', 0, 'blue'],
    ['section', 'section', 'section', 3, 'mirror'],
    ['section', 'section', 'section', 3, 'light'],
    ['subsection', 'section', 'section', 3, 'light']
]

你可以硬编码像

这样的东西
def is_match(lst):
    return (
        len(lst) >= 5
        and lst[0] == 'section'
        and lst[1] == 'section'
        and lst[2] == 'section'
        and lst[3] == 3
        and lst[4] != 'mirror'
    )

matches = [lst for lst in list_of_lists if is_match(lst)]

但这非常不灵活。你可以构建一个像

这样的系统
def make_item_matcher(offs, value):
    def item_matcher(lst):
        return len(lst) > offs and lst[offs] == value
    return item_matcher

def make_item_not_matcher(offs, value):
    def item_not_matcher(lst):
        return len(lst) <= offs or lst[item] != value
    return item_not_matcher

def find_matches(criteria, items):
    return [item for item in items if all(crit(item) for crit in criteria)]

matches = find_matches(
    [
        make_item_matcher(0, 'section'),
        make_item_matcher(1, 'section'),
        make_item_matcher(2, 'section'),
        make_item_matcher(3, 3),
        make_item_not_matcher(4, 'mirror')
    ],
    list_of_lists
)

但这真的不是更好。

答案 1 :(得分:0)

查看此代码:

function roundFloat(number) {
  return parseFloat((number).toPrecision(10))
}

roundFloat(67.30000000000001); // 67.3

答案 2 :(得分:0)

我的方法是将criteria_list转换为String。每当您想要查看输入列表是否与criteria_list的开头部分匹配时,您可以使用 str.startswith 来执行比较。然后来排除部分,我们还使用 str.startswith 来排除特殊关键字。

def is_startwith(input_list, criteria_list, exclude_keyword):
    is_match = False

    #convert criteria_list to string
    criteria_str = ",".join(str(x) for x in criteria_list)

    #convert input_list to string
    input_list_str = ",".join(str(x) for x in input_list)

    #see if input_list partially match the beginning of criteria_list
    is_match = str.startswith(input_list_str, criteria_str) 

    #see if we need to exclude keywords
    if is_match and str.startswith(input_list_str[len(criteria_str) + 1:] , exclude_keyword):
        is_match = False

    return is_match

#our test data
list1 = ['section', 'section', 'section', 3, 'mirror', 0, 'blue']
list2 = ['section', 'section', 'section', 3, 'mirror']
list3 = ['section', 'section', 'section', 3, 'light',]
list4 = ['subsection', 'section', 'section', 3, 'light',]
criteria_list = ['section', 'section', 'section', 3]
exclude_keyword = 'mirror'

# should return False and True
print is_startwith(list1, criteria_list, exclude_keyword )
print is_startwith(list3, criteria_list, exclude_keyword )