简体   繁体   English

如何使用python划分列表的两个元素

[英]How to divide two elements of list using python

I have python code as follow. 我有如下的python代码。 jv_list is populated from resultset retrieve from DB query. 从数据库查询检索的结果集中填充jv_list。

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

I am printing jv_list and it is giving me below mention output. 我正在打印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'}]

How can i get division result which is actually second value divided by first value. 我如何获得除法结果,实际上是第二个值除以第一个值。 for. 对于。 ie 79923888/ 19834

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}]

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

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. 对于要彼此分开的每对元素,分别为jv_list[0]['length_bytes'] / jv_list[0]['in']

If you want the result of length-bytes / in , you can use those keys and retrieve their values: 如果要得到length-bytes / in的结果,则可以使用这些键并检索其值:

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

根据您的数据结构,看来这将是答案。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM