簡體   English   中英

使用python reduce函數創建動態查詢?

[英]Dynamic query creation using python reduce function?

目前,以下代碼動態創建查詢: -

碼:

zip_cols = list(zip(['name','address'],
                    ['name_1','address_1']))


self.matches = self.features[
                            (
                                [
                                    reduce(
                                        lambda x, y: x + y,
                                        [self.features[a + "_" + c[0] + "_" + c[1]] for a in self._algos],
                                    )
                                    for c in zip_cols
                                ][0]
                                > (self.input_args.get('threshold', 0.7) * 4)
                            )
                            & (
                                [
                                    reduce(
                                        lambda x, y: x + y,
                                        [self.features[a + "_" + c[0] + "_" + c[1]] for a in self._algos],
                                    )
                                    for c in zip_cols
                                ][1]
                                > (self.input_args.get('threshold', 0.7) * 4)
                            )].copy()

查詢:

matches = features[(
                    (
                       (features['fw_name_name_1'] / 100)  
                      + features['sw_name_name_1']
                      + features['jw_name_name_1']
                      + features['co_name_name_1']
                    )  > 2.8
                   ) 
                   & 
                    (
                       (
                        (features['fw_address_address_1'] / 100)  
                      + features['sw_address_address_1']
                      + features['jw_address_address_1']
                      + features['co_address_address_1']
                       ) > 2.8
                    )
           ].copy()

但是如果source_compare_names中有2列並且1或者2以上失敗,則此查詢有效。我們如何解決此問題?

有了最小的輸入和上下文我得到了這應該讓你開始。 我們的想法是您將過濾條件動態建立為字符串,加入它們並對其進行評估。


threshold = self.input_args.get('threshold', 0.7) * 4
column_selection = [reduce(lambda x, y: x + y,
                           [self.features[a + "_" + c[0] + "_" + c[1]] for a in self._algos]) for c in zip_cols]

size = 10 # number of items you need 
total_filter_list = []
for i in range(size):
    # build the filter columns as list of strings
    total_filter_list.append(f'(column_selection[{i}] > {threshold})') 

# join the list of strings with '&', build the total filter criteria as string
total_filter_string = ' & '.join(total_filter_list)

# evaluate the filter
self.features[eval(total_filter_string)]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM