Python Forum

Full Version: Array search and filter
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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]
was looking for a python script. Thank you
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.
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)
def get_power(elements):
    return [
        element for element in elements
        if "power" in element.get("sec", "").lower()
    ]
elements is a list, with dicts 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)
Output:
[{'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'}]
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*")]
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'}]