Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Array search and filter
#1
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]
Reply
#2
was looking for a python script. Thank you
Reply
#3
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.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#4
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)
Reply
#5
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'}]
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#6
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*")]
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#7
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'}]
perfringo likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  go over and search in numpy array faster caro 7 1,754 Jun-20-2022, 04:54 PM
Last Post: deanhystad
  I have an array, how can I search a seperate file for the contents of my array? Mr_Keystrokes 0 2,347 Mar-13-2018, 02:25 PM
Last Post: Mr_Keystrokes

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020