Getting data from a dictionary Python - python

I'm using python dictionaries from counting the repeated objects from an array.
I use a function obtained from this forum from counting the objects, and the obtained result is on the next format: {object: nºelements, ...).
My issue is that the function doesn't return the dictionary objects differentiated by keys and I don't know how to get the objects.
count_assistence_issues = {x:list(assistances_issues).count(x) for x in list(assistances_issues)}
count_positive_issues = {x:list(positive_issues).count(x) for x in list(positive_issues)}
count_negative_issues = {x:list(negative_issues).count(x) for x in list(negative_issues)}
print(count_assistence_issues)
print(count_positive_issues)
print(count_negative_issues)
This is the output obtained:
{school.issue(10,): 1, school.issue(13,): 1}
{school.issue(12,): 1}
{school.issue(14,): 2}
And this is the output I need to obtain:
{{issue: school.issue(10,), count: 1},
{issue: school.issue(13,), count: 1}}
{{issue: school.issue(12,), count: 1}}
{{issue: school.issue(14,), count: 2}}
Anyone know how to differenciate by keys the elements of the array using the function?
Or any other function for counting the repeated objects for obtaining a dictionary with the format {'issue': issue,'count': count)
Thanks for reading!

Given your input and output. I would consider the following.
1) Merge all your counts into a single dictionary
#assuming that what diffrentitaes your issues is a unique ID/key/value etc.
#meaning that no issues are subsets of the other. IF they are this will need some tweaking
issue_count = {}
issue_count.update(count_assistence_issues)
issue_count.update(count_positive_issues)
issue_count.update(count_positive_issues)
Getting the counts is then simply:
issue_count[school.issue(n,)]
The key is your array. If you want an alternative. You could make a list of keys or dict of your keys. You can make this as verbose as you want.
key_issues = {"issue1":school.issue(1,),"issue2":school.issue(2,)....}
This then allows you to call your counts by:
issue_count[key_issues["issue1"]]
If you want to use the "count" field. You would need to fix your counter to give you a dict of your issue with field count but that's another question.

Related

How to start from second key when iterating over dictionary using for loop in Python

I am computing returns from data in a dictionary. My keys are dates and for every key I have a dataframe with data to compute my returns. To compute the returns I need data today and yesterday (t and t-1), hence I want to initiate from the second observation (key).
Since I do not have much experience my initial thought was to execute like this:
dict_return = {}
for t, value in dict_data.items()[1:]:
returns = 'formula'
dict_returns[t] = returns
Which gave me the error:
TypeError: 'dict_items' object is not subscriptable
Searching for an answer, the only discussion I could find was skipping the first item, e.g. like this:
from itertools import islice
for key, value in islice(largeSet.items(), 1, None):
Is there a simple approach to skip the first key?
Thank you
If you are in Python 3 you need to use a list, Dict_ items ([‘No surfacing ‘,’flippers’]) returns a dict_ The items object is no longer of the list type and does not support index, this is why the list type can be used
I can think of 2 options, both require an extra step:
Option 1: Create a second dict without your key and loop over that
loop_dict = dict_data.pop(<key_to_remove>)
Then loop over loop_dict as you have done above.
Option 2: Create a list of keys from your dict and loop over that
keys = dict_data.keys()
loop_keys = keys[1:]
for key in loop_keys:
Etc
If you pass a reference to your dictionary to list() you will get a list of the dictionary's keys. This is because dictionaries are iterable. In your code you're not interested in the key's value so:
dict_data = {'a': 1, 'b': 2} # or whatever
dict_data[list(dict_data)[1]] = 3
print(dict_data)
Output:
{'a': 1, 'b': 3}

iter through the dict store the key value and iter again to look for similar word in dict and delete form dict eg(Light1on,Light1off) in Python

[I had problem on how to iter through dict to find a pair of similar words and output it then the delete from dict]
My intention is to generate a random output label then store it into dictionary then iter through the dictionary and store the first key in the list or some sort then iter through the dictionary to search for similar key eg Light1on and Light1off has Light1 in it and get the value for both of the key to store into a table in its respective columns.
such as
Dict = {Light1on,Light2on,Light1off...}
store value equal to Light1on the iter through the dictionary to get eg Light1 off then store its Light1on:value1 and Light1off:value2 into a table or DF with columns name: On:value1 off:value2
As I dont know how to insert the code as code i can only provide the image sry for the trouble,its my first time asking question here thx.
from collections import defaultdict
import difflib, random
olist = []
input = 10
olist1 = ['Light1on','Light2on','Fan1on','Kettle1on','Heater1on']
olist2 = ['Light2off','Kettle1off','Light1off','Fan1off','Heater1off']
events = list(range(input + 1))
for i in range(len(olist1)):
output1 = random.choice(olist1)
print(output1,'1')
olist1.remove(output1)
output2 = random.choice(olist2)
print(output2,'2')
olist2.remove(output2)
olist.append(output1)
olist.append(output2)
print(olist,'3')
outputList = {olist[i]:events[i] for i in range(10)}
print (str(outputList),'4')
# Iterating through the keys finding a pair match
for s in range(5):
for i in outputList:
if i == list(outputList)[0]:
skeys = difflib.get_close_matches(i, outputList, n=2, cutoff=0.75)
print(skeys,'5')
del outputList[skeys]
# Modified Dictionary
difflib.get_close_matches('anlmal', ['car', 'animal', 'house', 'animaltion'])
['animal']
Updated: I was unable to delete the pair of similar from the list(Dictionary) after founding par in the dictionary
You're probably getting an error about a dictionary changing size during iteration. That's because you're deleting keys from a dictionary you're iterating over, and Python doesn't like that:
d = {1:2, 3:4}
for i in d:
del d[i]
That will throw:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
RuntimeError: dictionary changed size during iteration
To work around that, one solution is to store a list of the keys you want to delete, then delete all those keys after you've finished iterating:
keys_to_delete = []
d = {1:2, 3:4}
for i in d:
if i%2 == 1:
keys_to_delete.append(i)
for i in keys_to_delete:
del d[i]
Ta-da! Same effect, but this way avoids the error.
Also, your code above doesn't call the difflib.get_close_matches function properly. You can use print(help(difflib.get_close_matches)) to see how you are meant to call that function. You need to provide a second argument that indicates the items to which you wish to compare your first argument for possible matches.
All of that said, I have a feeling that you can accomplish your fundamental goals much more simply. If you spend a few minutes describing what you're really trying to do (this shouldn't involve any references to data types, it should just involve a description of your data and your goals), then I bet someone on this site can help you solve that problem much more simply!

How to recode numerical to categorical data

I am new to Python, I am studying it for data science purposes. Right now, I am trying to recode some numerical data (1,2,3 etc) into categories. It requires a little loop in the end, but I can't get that right. It causes a key error 3.
The dataset has 21 columns.
Can anyone help?
Thanks!!
for col_dic in code_list:
col = col_dic[0]
dic = col_dic[1]
values[col] = [dic[x] for x in values[col]]
It is rather hard to understand what exactly you want to see in result, but the cause of this error is clear:
You are iterating through a list of lists. Every col_dic contains a col = col_dic[0] (string like 'property_type') and dic = col_dic[1] (dictionary). In the last line you are writing info to a values dict (I suppose you created it before). This error is appearing because dic doesn't contain a particular key from the values[col]. For example:
values[col] is equal to {1: [], 2: [], 3: []} and dic is equal to {1: 'One', 2: 'Two'}. When you are iterating through values[col], you are trying to find a key 3 in dic. But it contains no 3 key, so the error is appearing. You should check that dic contains this key like this:
values_list = []
for v in values[col]:
if v in dic:
values_list.append(dic[v])
values[col] = values_list
Also note that your keys are strings represents integers. Your error can appear when you try to find a key '3' (string) in a dict contains keys like 3 (integers). So I suggest you to convert your keys to strings: str(key).

JSON Object parsing: Loop through dynamic data

I have a JSON object as follows:
{u'2015-05-11': 2, u'2015-05-04': 1}
Here the dates can vary, i.e., I can query for multiple dates.
I want to extract the numbers in the object and add them. So in this example I want to extract 2 and 1 and them up to get the result 3.
Another example:
{u'2015-05-11': 6, u'2015-05-04': 0}
Here the answer will be 6 + 0 = 6.
Another example of the JSON object with multiple dates:
{u'2015-04-27': 0, u'2015-05-11': 2, u'2015-05-04': 1}
Here the answer will be: 0 + 2 + 1 = 3
I want to loop through the JSON object and extract all the numbers. I've read the answers provided here and here. The problem is the, the JSON object that I have does not have a fixed key that I can query.
How do I go about this?
These are all Python dicts, not JSON objects.
Since you don't seem to care about the keys at all, you can just get the values and sum them:
result = sum(data.values())
If you are sure it's a json data, the data is like as below:
'{"2015-05-11": 2, "2015-05-04": 1}'
Convert json to dict:
data = json.loads(a)
# {u'2015-05-11': 2, u'2015-05-04': 1}
According to your question, solve the problem.
result = sum(data.values())
# 3
Daniel's answer provides you the solution youre looking for but you should also know that you can iterate through a Python dict with enumerate.
for value, key in enumerate(your_dict):
print value, key

Adding Multiple Values to a Single Key in Python Dictionary

Python dictionaries really have me today. I've been pouring over stack, trying to find a way to do a simple append of a new value to an existing key in a python dictionary adn I'm failing at every attempt and using the same syntaxes I see on here.
This is what i am trying to do:
#cursor seach a xls file
definitionQuery_Dict = {}
for row in arcpy.SearchCursor(xls):
# set some source paths from strings in the xls file
dataSourcePath = str(row.getValue("workspace_path")) + "\\" + str(row.getValue("dataSource"))
dataSource = row.getValue("dataSource")
# add items to dictionary. The keys are the dayasource table and the values will be definition (SQL) queries. First test is to see if a defintion query exists in the row and if it does, we want to add the key,value pair to a dictionary.
if row.getValue("Definition_Query") <> None:
# if key already exists, then append a new value to the value list
if row.getValue("dataSource") in definitionQuery_Dict:
definitionQuery_Dict[row.getValue("dataSource")].append(row.getValue("Definition_Query"))
else:
# otherwise, add a new key, value pair
definitionQuery_Dict[row.getValue("dataSource")] = row.getValue("Definition_Query")
I get an attribute error:
AttributeError: 'unicode' object has no attribute 'append'
But I believe I am doing the same as the answer provided here
I've tried various other methods with no luck with various other error messages. i know this is probably simple and maybe I couldn't find the right source on the web, but I'm stuck. Anyone care to help?
Thanks,
Mike
The issue is that you're originally setting the value to be a string (ie the result of row.getValue) but then trying to append it if it already exists. You need to set the original value to a list containing a single string. Change the last line to this:
definitionQuery_Dict[row.getValue("dataSource")] = [row.getValue("Definition_Query")]
(notice the brackets round the value).
ndpu has a good point with the use of defaultdict: but if you're using that, you should always do append - ie replace the whole if/else statement with the append you're currently doing in the if clause.
Your dictionary has keys and values. If you want to add to the values as you go, then each value has to be a type that can be extended/expanded, like a list or another dictionary. Currently each value in your dictionary is a string, where what you want instead is a list containing strings. If you use lists, you can do something like:
mydict = {}
records = [('a', 2), ('b', 3), ('a', 4)]
for key, data in records:
# If this is a new key, create a list to store
# the values
if not key in mydict:
mydict[key] = []
mydict[key].append(data)
Output:
mydict
Out[4]: {'a': [2, 4], 'b': [3]}
Note that even though 'b' only has one value, that single value still has to be put in a list, so that it can be added to later on.
Use collections.defaultdict:
from collections import defaultdict
definitionQuery_Dict = defaultdict(list)
# ...

Categories

Resources