Ammad
Ammad

Reputation: 4235

How to divide two elements of list using python

I have python code as follow. jv_list is populated from resultset retrieve from D.B. query.

jv_list = list(result.get_points())
print(jv_list)

I am printing jv_list and it is giving me below mention output.

[{u'in': u'19834', u'length-bytes': u'79923888', u'run-time': u'1h50m43.489993955s', u'time': u'2017-09-08T21:20:39.846582783Z'}]

How can i get division result which is actually second value divided by first value. for. i.e. 79923888/ 19834

Upvotes: 1

Views: 67

Answers (5)

Bernardo Duarte
Bernardo Duarte

Reputation: 4264

Based on your data structure it seems this would be the answer.

float(jv_list[0]['length-bytes']) / float(jv_list[0]['in'])

Upvotes: 1

White
White

Reputation: 637

If this is what you mean

x = [{u'in': u'19834', u'length-bytes': u'79923888', u'run-time': u'1h50m43.489993955s', u'time': u'2017-09-08T21:20:39.846582783Z'}]
for i in x:
    print(float(i['length-bytes'])/float(i['in']))

results as

4029.64041544822

Upvotes: 2

Djaouad
Djaouad

Reputation: 22794

If you want the result of length-bytes / in, you can use those keys and retrieve their values:

jv_list = [{u'in': u'19834', u'length-bytes': u'79923888', u'run-time': u'1h50m43.489993955s', u'time': u'2017-09-08T21:20:39.846582783Z'}]

result = float(jv_list[0]['length-bytes']) / float(jv_list[0]['in'])
print(result) # => 4029.64041544822

Upvotes: 1

Reblochon Masque
Reblochon Masque

Reputation: 36742

You can't reliably do that, dictionaries are unordered.
With this data structure, you will need to address the elements via their keys.

that is jv_list[0]['length_bytes'] / jv_list[0]['in'] for each pair of elements you want to divide by each other.

Upvotes: 1

nehem
nehem

Reputation: 13672

Try this sequence of commands

>>> X = [{u'in': u'19834', u'length-bytes': u'79923888', u'run-time': u'1h50m43.489993955s', u'time': u'2017-09-08T21:20:39.846582783Z'}]
>>> for x in X:
...     x['answer'] = float(x['length-bytes'])/float(x['in'])
...
>>> X
[{'in': '19834', 'length-bytes': '79923888', 'run-time': '1h50m43.489993955s', 'time': '2017-09-08T21:20:39.846582783Z', 'answer': 4029.64041544822}]

Upvotes: 4

Related Questions