![]() |
Array search and filter - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Array search and filter (/thread-32344.html) |
Array search and filter - tech_frk - Feb-04-2021 Hello experts, I have an array as shown below with 6 common elements repeated for 20-100 entries and want to extract the elements with 'Power' in the first column and put it in a text file with 'Filtered' word appended at the beginning of each line. appreicate your help. [inline]input array: a = [{'sec': 'coalmine', 'country': 'usa', 'state': 'newyork', 'zip': '17304', 'amt': '15480', 'tg': '910-456-8415'}, {'sec': 'coalPower', 'country': 'uk', 'state': 'birmingham', 'zip': '51730', 'amt': '26663', 'tg': '321-854-7536'}, . . . . {'sec': 'hydelPower', 'country': 'usa', 'state': 'sanfrancisco', 'zip': '92416', 'amt': '748521', 'tg': '451-278-9632'}] [/inline] [inline] output to text file comments sec(Power) state tg filtered coalpower birmingham 321-854-7536 filtered hydelPower usa 451-278-9632 [/inline] RE: Array search and filter - tech_frk - Feb-04-2021 was looking for a python script. Thank you RE: Array search and filter - perfringo - Feb-04-2021 This is not script writing service. No pain, no gain. Show your effort and with hight probability there will be help. To get you started: you have list of of dictionaries. You should go over every dictionary in list, check whether dictionary key 'sec' value consists 'power' and do something if it does. You should also think about case sensitivity (do you want both power and Power or not) etc. RE: Array search and filter - tech_frk - Feb-04-2021 yes i did try something very basic as iam a beginner apologize for not putting up the code here. dictOfNames = [ {'sec': 'coalmine', 'country': 'usa', 'state': 'newyork', 'zip': '17304', 'amt': '15480', 'tg': '910-456-8415'}, {'sec': 'coalPower', 'country': 'uk', 'state': 'birmingham', 'zip': '51730', 'amt': '26663', 'tg': '321-854-7536'}, {'sec': 'hydelPower', 'country': 'usa', 'state': 'sanfrancisco', 'zip': '92416', 'amt': '748521', 'tg': '451-278-9632'} ] newDict = { key: value for (key,value) in dictOfNames.sec() if key == '%Power%'} print('Filtered Dictionary : ') print(newDict) RE: Array search and filter - DeaD_EyE - Feb-04-2021 def get_power(elements): return [ element for element in elements if "power" in element.get("sec", "").lower() ] elements is a list , with dict s inside.Each element is checked, if power is in the value of the key "sec". To be more fault tolerant, I used the get method to prevent a KeyError if the key "sec" does not exist, it returns an empty str "".This result is changed to lower case. Then "power" in "the_text_from_sec" is checked.If the evaluation is True , then the element is in the list. Otherwise it's skipped.data = [ {'sec': 'coalmine', 'country': 'usa', 'state': 'newyork', 'zip': '17304', 'amt': '15480', 'tg': '910-456-8415'}, {'sec': 'coalPower', 'country': 'uk', 'state': 'birmingham', 'zip': '51730', 'amt': '26663', 'tg': '321-854-7536'}, {'sec': 'hydelPower', 'country': 'usa', 'state': 'sanfrancisco', 'zip': '92416', 'amt': '748521', 'tg': '451-278-9632'} ] with_power = get_power(data) print(with_power)
RE: Array search and filter - perfringo - Feb-04-2021 Just for fun - iteration of DeaD_EyE function with abusing filename pattern matching: from fnmatch import fnmatch def get_power(records): return [record for record in records if fnmatch(record.get("sec"), "*[Pp]ower*")] RE: Array search and filter - nilamo - Feb-04-2021 Oh are we golfing? Generators make the world go round :) >>> dictOfNames = [ ... {'sec': 'coalmine', 'country': 'usa', 'state': 'newyork', 'zip': '17304', 'amt': '15480', 'tg': '910-456-8415'}, ... {'sec': 'coalPower', 'country': 'uk', 'state': 'birmingham', 'zip': '51730', 'amt': '26663', 'tg': '321-854-7536'}, ... {'sec': 'hydelPower', 'country': 'usa', 'state': 'sanfrancisco', 'zip': '92416', 'amt': '748521', 'tg': '451-278-9632'} ... ] >>> def filter_by(data, key, value): ... for row in data: ... if key in row and value in row[key].lower(): ... yield row ... >>> list(filter_by(dictOfNames, 'sec', 'power')) [{'sec': 'coalPower', 'country': 'uk', 'state': 'birmingham', 'zip': '51730', 'amt': '26663', 'tg': '321-854-7536'}, {'sec': 'hydelPower', 'country': 'usa', 'state': 'sanfrancisco', 'zip': '92416', 'amt': '748521', 'tg': '451-278-9632'}] |