Python 按单词过滤数组

Python array filter by words

我编程很弱。我需要根据以下逻辑过滤数据数组,但我不知道该怎么做。 Сan 使用 "while" 循环,但我认为这会花费很多时间,因为数组非常大。

我有非常大的打印机阵列驱动程序:

[...
{"PPD_NAME": "HP PSC 2500 Series, hpcups 3.18.5 (en)", "PPD_PATH": "hplip:4/ppd/hplip/HP/hp-psc_2500_series.ppd"}, 
{"PPD_NAME": "HP LaserJet m3035 MFP hpijs pcl3, 3.18.5 (en)", "PPD_PATH": "drv:///hpijs.drv/hp-laserjet_m3035_mfp-pcl3-hpijs.ppd"}, 
{"PPD_NAME": "HP LaserJet m2727 MFP pcl3, hpcups 3.18.5 (en)", "PPD_PATH": "drv:///hpcups.drv/hp-laserjet_m2727_mfp-pcl3.ppd"}
...]

并且有字符串(名称打印机):

"HP HP LaserJet m2727 Multifunction Printer"

我想按单词拆分字符串并按每个单词过滤数组直到有 "None".

示例:

循环 1: 单词:"HP" 结果:

{"PPD_NAME": "HP PSC 2500 Series, hpcups 3.18.5 (en)", "PPD_PATH": "hplip:4/ppd/hplip/HP/hp-psc_2500_series.ppd"}, {"PPD_NAME": "HP LaserJet m3035 MFP hpijs pcl3, 3.18.5 (en)", "PPD_PATH": "drv:///hpijs.drv/hp-laserjet_m3035_mfp-pcl3-hpijs.ppd"}, {"PPD_NAME": "HP LaserJet m2727 MFP pcl3, hpcups 3.18.5 (en)", "PPD_PATH": "drv:///hpcups.drv/hp-laserjet_m2727_mfp-pcl3.ppd"}

循环 2: 单词:"LaserJet" 结果:

{"PPD_NAME": "HP LaserJet m3035 MFP hpijs pcl3, 3.18.5 (en)", "PPD_PATH": "drv:///hpijs.drv/hp-laserjet_m3035_mfp-pcl3-hpijs.ppd"}, {"PPD_NAME": "HP LaserJet m2727 MFP pcl3, hpcups 3.18.5 (en)", "PPD_PATH": "drv:///hpcups.drv/hp-laserjet_m2727_mfp-pcl3.ppd"}

循环 3: 单词:"m2727"

结果:

{"PPD_NAME": "HP LaserJet m2727 MFP pcl3, hpcups 3.18.5 (en)", "PPD_PATH": "drv:///hpcups.drv/hp-laserjet_m2727_mfp-pcl3.ppd"}

循环4: 单词:"Multifunction"

结果: None 我想得到最后一次迭代的第一个对象不是 "None": {"PPD_NAME": "HP LaserJet m2727 MFP pcl3, hpcups 3.18.5 (en)",{...}}

前提是结果是"None"或者单词完成。 如何在 python 上制作类似的过滤器? 有人吗?:

for word in ArgInListFindNewPrinters['PRINTER_INFO'].split():
        name = next(name for name in ListPrinterDriver if re.search(word, str(name), re.IGNORECASE))
        print name

但它并没有像我预期的那样工作。

编辑:我的结果较低。此脚本在 CUPS(可用打印机)中搜索打印机并添加它。如果能以某种方式优化它,我将不胜感激。

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import requests
import json
import sys
from bs4 import BeautifulSoup
import re

def GetFindNewPrinters(cookie):
        headers = {}
        headers["Cookie"] = 'org.cups.sid='+cookie["org.cups.sid"]
        FromData = {}
        FromData["OP"] = 'find-new-printers'
        FromData["org.cups.sid"] = cookie["org.cups.sid"]
        Responce = requests.post(url, headers=headers, data=FromData)
        soup = BeautifulSoup(Responce.content.decode('utf-8'), 'html.parser')
        ListFindNewPrinters = []
        for id, link in enumerate(soup.find_all('form')):
                ListFindNewPrinters.append({'TEMPLATE_NAME': link.find("input", {"name":"TEMPLATE_NAME"})['value']})
                ListFindNewPrinters[id]['DEVICE_URI'] = link.find("input", {"name":"DEVICE_URI"})['value']
                ListFindNewPrinters[id]['PRINTER_LOCATION'] = link.find("input", {"name":"PRINTER_LOCATION"})['value']
                ListFindNewPrinters[id]['PRINTER_INFO'] = link.find("input", {"name":"PRINTER_INFO"})['value']
        return ListFindNewPrinters

def GetPrinterDrivers(cookie,PRINTER_NAME,PRINTER_INFO,PRINTER_LOCATION,DEVICE_URI):
        headers = {}
        headers["Cookie"] = 'org.cups.sid='+cookie["org.cups.sid"]
        FromData = {}
        FromData["OP"] = 'add-printer'
        FromData["org.cups.sid"] = cookie["org.cups.sid"]
        FromData["PRINTER_IS_SHARED"] = 'on'
        FromData["PRINTER_LOCATION"] = PRINTER_LOCATION
        FromData["PRINTER_INFO"] = PRINTER_INFO
        FromData["PRINTER_NAME"] = PRINTER_NAME
        FromData["DEVICE_URI"] = DEVICE_URI
        FromData["PPD_MAKE"] = PRINTER_INFO.split(' ', 1)[0]
        FromData["PPD_FILE"] = '(binary)'
        Responce = requests.post(url, headers=headers, data=FromData)
        soup = BeautifulSoup(Responce.content.decode('utf-8'), 'html.parser')
        ListPrinterDriver = []
        for id, link in enumerate(soup.find_all('option')):
                ListPrinterDriver.append({'PPD_PATH': link['value']})
                ListPrinterDriver[id]['PPD_NAME'] = link.next.replace("\n","")
        return ListPrinterDriver

def FindInArrayByWord(ArrayDrivers,PrinterName):
        if ArrayDrivers == []:
                return None
        for word in PrinterName.split():
                if FindInArray(ArrayDrivers,word) == []:
                        break
                else:
                        ArrayDrivers = FindInArray(ArrayDrivers,word)
        return ArrayDrivers[0]

def AddPrinter(cookie,PRINTER_NAME,PRINTER_INFO,PRINTER_LOCATION,DEVICE_URI,PPD_NAME):
        headers = {}
        headers["Cookie"] = 'org.cups.sid='+cookie["org.cups.sid"]
        FromData = {}
        FromData["org.cups.sid"] = cookie["org.cups.sid"]
        FromData["OP"] = 'add-printer'
        FromData["printer_name"] = PRINTER_NAME
        FromData["PRINTER_NAME"] = PRINTER_NAME
        FromData["PRINTER_INFO"] = PRINTER_INFO
        FromData["PRINTER_LOCATION"] = PRINTER_LOCATION
        FromData["DEVICE_URI"] = DEVICE_URI
        FromData["PRINTER_IS_SHARED"] = 'on'
        FromData["PPD_NAME"] = PPD_NAME
        FromData["MAX_FILE_SIZE"] = '262144'
        FromData["PPD_FILE"] = '(binary)'
        Responce = requests.post(url, headers=headers, data=FromData)
        return Responce

def AddPrinterOptions(cookie,PRINTER_NAME):
        headers = {}
        headers["Cookie"] = 'org.cups.sid='+cookie["org.cups.sid"]
        FromData = {}
        FromData["org.cups.sid"] = cookie["org.cups.sid"]
        FromData["PRINTER_NAME"] = PRINTER_NAME
        FromData["OP"] = 'set-printer-options'
        FromData["PageSize"] = 'A4'
        FromData["PageSize.Width"] = '0'
        FromData["PageSize.Height"] = '0'
        FromData["PageSize.Units"] = 'pt'
        FromData["Duplex"] = 'None'
        FromData["job_sheets_start"] = 'none'
        FromData["job_sheets_end"] = 'none'
        FromData["printer_error_policy"] = 'retry-job'
        FromData["printer_op_policy"] = 'default'
        Responce = requests.post(url, headers=headers, data=FromData)
        return Responce

def FindInArray(Array,Arg,*args):
        ArrayObject = None
        NewArray = []
        if len(args) != 0:
                for ArrayObjectName in args:
                        try:
                                ArrayObject = [name for name in Array if re.search(Arg, name[ArrayObjectName], re.IGNORECASE)]
                        except StopIteration: pass
        else:
                try:
                        ArrayObject = [name for name in Array if re.search(Arg, str(name), re.IGNORECASE)]
                except StopIteration: pass
        return ArrayObject


def main():
        # Check connection
        try:
                Responce = requests.get(url, timeout=10)
        except requests.exceptions.RequestException as e:
                print e
                sys.exit(-1)

        # Get list available printers in CUPS, and find. Else add printer with specific destination.
        if sys.argv[3] != 'False':
                ArgPrinterIP = sys.argv[3]
                ArgInListFindNewPrinters = {}
                ArgInListFindNewPrinters["TEMPLATE_NAME"] = ArgPrinter.replace(" ", "_")
                ArgInListFindNewPrinters["PRINTER_INFO"] = ArgPrinter
                ArgInListFindNewPrinters["PRINTER_LOCATION"] = 'Добавленный по IP'
                ArgInListFindNewPrinters["DEVICE_URI"] = 'socket://'+ArgPrinterIP
        else:
                ListFindNewPrinters = GetFindNewPrinters(Responce.cookies)
                ArgInListFindNewPrinters = FindInArrayByWord(ListFindNewPrinters,ArgPrinter)
                if ArgInListFindNewPrinters == None:
                        print "Not find printers with arguments."
                        sys.exit(1)

        ListPrinterDriver = GetPrinterDrivers(Responce.cookies,ArgInListFindNewPrinters['TEMPLATE_NAME'],ArgInListFindNewPrinters['PRINTER_INFO'],ArgInListFindNewPrinters['PRINTER_LOCATION'],ArgInListFindNewPrinters['DEVICE_URI'])
        Driver = FindInArrayByWord(ListPrinterDriver,ArgInListFindNewPrinters['PRINTER_INFO'])
        AddPrinter(Responce.cookies,ArgInListFindNewPrinters['TEMPLATE_NAME'],ArgInListFindNewPrinters['PRINTER_INFO'],ArgInListFindNewPrinters['PRINTER_LOCATION'],ArgInListFindNewPrinters['DEVICE_URI'],Driver['PPD_PATH'])
        AddPrinterOptions(Responce.cookies,ArgInListFindNewPrinters['TEMPLATE_NAME'])
        print "Printer: "+ArgInListFindNewPrinters['TEMPLATE_NAME']+". Succesfull add."

# Set arguments
ArgURL = sys.argv[1]
ArgPrinter = sys.argv[2]
url = 'http://'+ArgURL+':631/admin/'
if __name__ == "__main__":
        try:
                main()
        except Exception as e:
                print e
                sys.exit(0)

这是你想要做的吗?如果有效,请将其标记为答案。

printers = [{"PPD_NAME": "HP PSC 2500 Series, hpcups 3.18.5 (en)", "PPD_PATH": "hplip:4/ppd/hplip/HP/hp-psc_2500_series.ppd"}
]

filterby = "HP HP LaserJet m2727 Multifunction Printer"
filterby_split = filterby.split(" ")
output = []
flag = False


for i in filterby_split:
    for j in printers:
        if j["PPD_NAME"].find(i):
            output.append(j)
            flag = True

    if not flag:
        output.append(printers[-1])

for i in output:
    print(printers[i])

使用正则表达式匹配所需的过滤器。

import re

# List of printers
printers = [{"PPD_NAME": "HP PSC 2500 Series, hpcups 3.18.5 (en)", "PPD_PATH": "hplip:4/ppd/hplip/HP/hp-psc_2500_series.ppd"}, 
{"PPD_NAME": "HP LaserJet m3035 MFP hpijs pcl3, 3.18.5 (en)", "PPD_PATH": "drv:///hpijs.drv/hp-laserjet_m3035_mfp-pcl3-hpijs.ppd"}, 
{"PPD_NAME": "HP LaserJet m2727 MFP pcl3, hpcups 3.18.5 (en)", "PPD_PATH": "drv:///hpcups.drv/hp-laserjet_m2727_mfp-pcl3.ppd"}]

# List of filter words
filterby = "HP HP LaserJet m2727 Multifunction Printer"
# Separate into separate words
filterby_split = filterby.split()

# start with all filters
prev = printers[:]

for filt in filterby_split:
  # regex to match current filter word
  p = re.compile(r'\b' + filt + r'\b', flags = re.IGNORECASE)

  # Generate list of printers which has the filter word
  # filtering from previous list
  current = [printer for printer in prev if p.search(printer["PPD_NAME"])]

  if current:  # there were some printers
    prev[:] = current  # copy to previous since found items in filter
  else:        # No printers found
    current = prev  # found none in last iteration
    break

print(current[0]) # first object of last successful iteration