Casting a value in a dictionary from str to float - python

I have been trying to look at other questions and not found anything that works for me. I have a list if dictionaries (trips) and want to access the values in each dictionary within the list of trips, and convert if from a string to a float.
If i take trips[0] output is:
# {'pickup_latitude': '40.64499',
# 'pickup_longitude': '-73.78115',
# 'trip_distance': '18.38'}
I am trying to build a function that iterates through the list of dictionaries, accessing the values and transferring them to floats.
I have tried various versions of the code below, including trying to build a new dictionary and returning that
def float_values(trips):
for trip in trips:
for key, value in trip.items():
value = float(value)
return
float_values[0] should output:
# {'pickup_latitude': 40.64499,
# 'pickup_longitude': -73.78115,
# 'trip_distance': 18.38}
Continuously getting
'function' object is not subscriptable'
OR
'NoneType' object is not subscriptable

If you what to override your values from the dictionary you should do something like this
def float_values(trips):
for trip in trips:
for key, value in trip.items():
trip[key] = float(value)
return trips
By doing value = float(value) you are writing temporary the float value and not saving it anywhere

Simply what you need to do is change value inside the dictionary:
for list_dict in list_of_dicts:
for key, value in list_dict.items():
list_dict[key] = float(value)

Related

Update dictionary and create key-value pairs with loop values

i'm trying to append to a dictionary. there are two loops. the name of the keys depends on the value of the inner loop and the key is the value of a variable which is updated within the loop. my script is
def append_value(dict_obj, key, value):
# Check if key exist in dict or not
if key in dict_obj:
# Key exist in dict.
# Check if type of value of key is list or not
if not isinstance(dict_obj[key], list):
# If type is not list then make it list
dict_obj[key] = [dict_obj[key]]
# Append the value in list
dict_obj[key].append(value)
else:
# As key is not in dict,
# so, add key-value pair
dict_obj[key] = value
for x in range(tot):
dict=['output'=x]
for a in range(33,91):
index_val=(a*sum_t)/x
# now i'm trying to create key names that would be year_33 year_34 and so on
head=''
head='year_{}'.format(a)
append_value(dict, head=avg_PMI)
i get the error name 'append_value' is not defined. would appreciate any help. i would like to loop over values of tot and the (33,91) range. each combination of the two gives a unique values and i want to create a dictionary which will become a csv where x values are rows, a is the column.
thanks!
edited: to show append_value function
The cause of the error is that when you call a function with head=avg_PMI, python assumes that head is an argument. For append_value, the only arguments are dict_obj, key and value. I assume that you want to add a value to the dictionary such that head=avg_PMI. To do this, you have to call the function append_value in the following way:
append_value(dict, head, avg_PMI).

Python 2.7: Why does json.loads not convert my string to a dict correctly?

I am asking an ElasticSearch database to provide me with a list of indices and their creation dates using Python 2.7 and the Requests package. The idea is to quickly calculate which indices have exceeded the retention policy and need to be put to sleep.
The request works perfectly and the results are exactly what I want. However, when I run the code below, when I try to convert the json result to a dict, the type of theDict is correct but it reports a size of 1, when there should be at least a couple dozen entries. What am I doing wrong? I have a feeling it's something really dumb but I just can't snag it! :)
import json
import requests
esEndPoint = "https://localhost:9200"
retrieveString = "/_cat/indices?h=index,creation.date.string&format=json&s=creation.date"
# Gets the current indices and their creation dates
def retrieveIndicesAndDates():
try:
theResult = requests.get(esEndPoint+retrieveString)
print (theResult.content)
except Exception as e:
print("Unable to retrieve list of indices with creation dates.")
print("Error: "+e)
exit(3)
return theResult.content
def main():
theDict = dict(json.loads(retrieveIndicesAndDates()))
print(type(theDict)) # Reports correct type
print(len(theDict)) # Always outputs "1" ??
for index, creationdate in theDict.items():
print("Index: ",index,", Creation date: ",theDict[index])
return
The json the call returns:
[{"index":".kibana","creation.date.string":"2017-09-14T15:01:38.611Z"},{"index":"logstash-2018.07.23","creation.date.string":"2018-07-23T00:00:01.024Z"},{"index":"cwl-2018.07.23","creation.date.string":"2018-07-23T00:00:03.877Z"},{"index":"k8s-testing-internet-2018.07.23","creation.date.string":"2018-07-23T14:19:10.024Z"},{"index":"logstash-2018.07.24","creation.date.string":"2018-07-24T00:00:01.023Z"},{"index":"k8s-testing-internet-2018.07.24","creation.date.string":"2018-07-24T00:00:01.275Z"},{"index":"cwl-2018.07.24","creation.date.string":"2018-07-24T00:00:02.157Z"},{"index":"k8s-testing-internet-2018.07.25","creation.date.string":"2018-07-25T00:00:01.022Z"},{"index":"logstash-2018.07.25","creation.date.string":"2018-07-25T00:00:01.186Z"},{"index":"cwl-2018.07.25","creation.date.string":"2018-07-25T00:00:04.012Z"},{"index":"logstash-2018.07.26","creation.date.string":"2018-07-26T00:00:01.026Z"},{"index":"k8s-testing-internet-2018.07.26","creation.date.string":"2018-07-26T00:00:01.185Z"},{"index":"cwl-2018.07.26","creation.date.string":"2018-07-26T00:00:02.587Z"},{"index":"k8s-testing-internet-2018.07.27","creation.date.string":"2018-07-27T00:00:01.027Z"},{"index":"logstash-2018.07.27","creation.date.string":"2018-07-27T00:00:01.144Z"},{"index":"cwl-2018.07.27","creation.date.string":"2018-07-27T00:00:04.485Z"},{"index":"ctl-2018.07.27","creation.date.string":"2018-07-27T09:02:09.854Z"},{"index":"cfl-2018.07.27","creation.date.string":"2018-07-27T11:12:44.681Z"},{"index":"elb-2018.07.27","creation.date.string":"2018-07-27T11:13:51.340Z"},{"index":"cfl-2018.07.24","creation.date.string":"2018-07-27T11:45:23.697Z"},{"index":"cfl-2018.07.23","creation.date.string":"2018-07-27T11:45:24.646Z"},{"index":"cfl-2018.07.25","creation.date.string":"2018-07-27T11:45:25.700Z"},{"index":"cfl-2018.07.26","creation.date.string":"2018-07-27T11:45:26.341Z"},{"index":"elb-2018.07.24","creation.date.string":"2018-07-27T11:45:27.440Z"},{"index":"elb-2018.07.25","creation.date.string":"2018-07-27T11:45:29.572Z"},{"index":"elb-2018.07.26","creation.date.string":"2018-07-27T11:45:36.170Z"},{"index":"logstash-2018.07.28","creation.date.string":"2018-07-28T00:00:01.023Z"},{"index":"k8s-testing-internet-2018.07.28","creation.date.string":"2018-07-28T00:00:01.316Z"},{"index":"cwl-2018.07.28","creation.date.string":"2018-07-28T00:00:03.945Z"},{"index":"elb-2018.07.28","creation.date.string":"2018-07-28T00:00:53.992Z"},{"index":"ctl-2018.07.28","creation.date.string":"2018-07-28T00:07:19.543Z"},{"index":"k8s-testing-internet-2018.07.29","creation.date.string":"2018-07-29T00:00:01.026Z"},{"index":"logstash-2018.07.29","creation.date.string":"2018-07-29T00:00:01.378Z"},{"index":"cwl-2018.07.29","creation.date.string":"2018-07-29T00:00:04.100Z"},{"index":"elb-2018.07.29","creation.date.string":"2018-07-29T00:00:59.241Z"},{"index":"ctl-2018.07.29","creation.date.string":"2018-07-29T00:06:44.199Z"},{"index":"logstash-2018.07.30","creation.date.string":"2018-07-30T00:00:01.024Z"},{"index":"k8s-testing-internet-2018.07.30","creation.date.string":"2018-07-30T00:00:01.179Z"},{"index":"cwl-2018.07.30","creation.date.string":"2018-07-30T00:00:04.417Z"},{"index":"elb-2018.07.30","creation.date.string":"2018-07-30T00:01:01.442Z"},{"index":"ctl-2018.07.30","creation.date.string":"2018-07-30T00:08:28.936Z"},{"index":"cfl-2018.07.30","creation.date.string":"2018-07-30T06:52:16.739Z"}]
Your error is trying to convert a list of dicts to a dict:
theDict = dict(json.loads(retrieveIndicesAndDates()))
# ^^^^^ ^
That would only work for a dict of lists. It would be redundant, though.
Just use the reply directly. Each entry is a dict with the appropriate keys:
data = json.loads(retrieveIndicesAndDates())
for entry in data:
print("Index: ", entry["index"], ", Creation date: ", entry["creation.date.string"])
So what happens when you do convert that list to a dict? Why is there just one entry?
The dict understands three initialisation methods: keywords, mappings and iterables. A list fits the last one.
Initialisation from an iterable goes through it and expects key-value iterables as elements. If one were to do it manually, it would look like this:
def sequence2dict(sequence):
map = {}
for element in sequence:
key, value = element
map[key] = value
return map
Notice how each element is unpacked via iteration? In the reply each element is a dict with two entries. Iteration on that yields the two keys but ignores the values.
key, value = {"index":".kibana","creation.date.string":"2017-09-14T15:01:38.611Z"}
print(key, '=>', value) # prints "index => creation.date.string"
To the dict constructor, every element in the reply has the same key-value pair: "index" and "creation.date.string". Since keys in a dict are unique, all elements collapse to the same entry: {"index": "creation.date.string"}.

Store Widgets in Dictionary

I would like to store my various checkboxes in a dictionary so that I can later call upon them. Since I would like to perform actions based on the number of widgets with len(self.il['Line2']) I need some way of storing them all in an array. Storing each of widgets in a unique entry like:
for i in range(7): #INPUT LINE 2
self.il['Line2',i] = QtWidgets.QCheckBox(self.il2info[i],self)
print(self.il['Line2',i])
--------output----------
<PyQt5.QtWidgets.QCheckBox object at 0x0000021A4398EE58>
<PyQt5.QtWidgets.QCheckBox object at 0x0000021A4398EF78>
<PyQt5.QtWidgets.QCheckBox object at 0x0000021A439690D8>
<PyQt5.QtWidgets.QCheckBox object at 0x0000021A43969168>
<PyQt5.QtWidgets.QCheckBox object at 0x0000021A439691F8>
<PyQt5.QtWidgets.QCheckBox object at 0x0000021A43969288>
<PyQt5.QtWidgets.QCheckBox object at 0x0000021A43969318>
but then my len(self.il['Line2']) command does not work.
I have tried something like the following:
self.il['Line2'[i]] = QtWidgets.QCheckBox(self.il2info[i],self)
but get an error of:
IndexError: string index out of range
I have also tried to do:
self.il['Line2':[i]] = QtWidgets.QCheckBox(self.il2info[i],self)
but I am met with the following error of:
TypeError: unhashable type: 'slice'
Is there some syntax error that I am missing? Can widget objects not be stored in dictionaries? Is there a way for me to ID widgets that would allow me to store the ID in dictionary?
EDIT: My original problem has been solved as I was incorrectly defining the keys/values of my dictionary. Using a temp dictionary to collect all widgets into an array and then equating them to my master dictionary with a key of 'Line2' fixed the issue.
When you add an element as follows:
d[val1, val2] = some_value
is similar to:
d[(val1, val2)] = some_value
That is, the key is a tuple, so you must pass the tuple as a key so that it returns the value.
new_value = d[(val1, val2)]
In your case:
self.il['Line2', i] = some_value
new_value = self.il['Line2', i]
When you indicate for example:
self.il['Line2'[2]]
It is equivalent to:
self.il['n']
Or worse if you pass an index higher than the number of letters.
self.il['Line2'[6]]
Note: What you put a tuple as a key does not generate an array, if you want to get the structure of an array you must create a dictionary with dictionaries.
tmp_dict = {}
for i in range(7):
tmp_dict[i] = QtWidgets.QCheckBox(self.il2info[i],self)
self.il['Line2'] = tmp_dict
Then when you want to access you use:
#read
new_value = self.il['Line2'][i]
#write
self.il['Line2'][i] = some_value
Example:
for i in range(len(self.il['Line2'])):
new_value = self.il['Line2'][i]
self.il['Line2'][i] = some_value

python call function in get method of dictionary instead default value

Need some help in order to understand some things in Python and get dictionary method.
Let's suppose that we have some list of dictionaries and we need to make some data transformation (e.g. get all names from all dictionaries by key 'name'). Also I what to call some specific function func(data) if key 'name' was not found in specific dict.
def func(data):
# do smth with data that doesn't contain 'name' key in dict
return some_data
def retrieve_data(value):
return ', '.join([v.get('name', func(v)) for v in value])
This approach works rather well, but as far a I can see function func (from retrieve_data) call each time, even key 'name' is present in dictionary.
If you want to avoid calling func if the dictionary contains the value, you can use this:
def retrieve_data(value):
return ', '.join([v['name'] if 'name' in v else func(v) for v in value])
The reason func is called each time in your example is because it gets evaluated before get even gets called.

Cannot append string to dictionary key

I've been programming for less than four weeks and have run into a problem that I cannot figure out. I'm trying to append a string value to an existing key with an existing string stored in it but if any value already exists in the key I get "str object has no attribute 'append'.
I've tried turning the value to list but this also does not work. I need to use the .append() attribute because update simply replaces the value in clientKey instead of appending to whatever value is already stored. After doing some more research, I understand now that I need to somehow split the value stored in clientKey.
Any help would be greatly appreciated.
data = {}
while True:
clientKey = input().upper()
refDate = strftime("%Y%m%d%H%M%S", gmtime())
refDate = refDate[2 : ]
ref = clientKey + refDate
if clientKey not in data:
data[clientKey] = ref
elif ref in data[clientKey]:
print("That invoice already exists")
else:
data[clientKey].append(ref)
break
You can't .append() to a string because a string is not mutable. If you want your dictionary value to be able to contain multiple items, it should be a container type such as a list. The easiest way to do this is just to add the single item as a list in the first place.
if clientKey not in data:
data[clientKey] = [ref] # single-item list
Now you can data[clientkey].append() all day long.
A simpler approach for this problem is to use collections.defaultdict. This automatically creates the item when it's not there, making your code much simpler.
from collections import defaultdict
data = defaultdict(list)
# ... same as before up to your if
if clientkey in data and ref in data[clientkey]:
print("That invoice already exists")
else:
data[clientKey].append(ref)
You started with a string value, and you cannot call .append() on a string. Start with a list value instead:
if clientKey not in data:
data[clientKey] = [ref]
Now data[clientKey] references a list object with one string in it. List objects do have an append() method.
If you want to keep appending to the string you can use data[clientKey]+= ref

Categories