Python Forum
How do I get the last element? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: How do I get the last element? (/thread-18946.html)



How do I get the last element? - DaytonJones - Jun-07-2019

Newbie question....


I have a ResultSet that looks like:

ResultSet({'('system', {'host': 'my.host.net'})': [{'time': '2019-05-30T00:00:00Z', 'deltaUptime': 53175}, {'time': '2019-05-31T00:00:00Z', 'deltaUptime': 86400}, {'time': '2019-06-01T00:00:00Z', 'deltaUptime': 86400}, {'time': '2019-06-02T00:00:00Z', 'deltaUptime': 86400}, {'time': '2019-06-03T00:00:00Z', 'deltaUptime': 86400}, {'time': '2019-06-04T00:00:00Z', 'deltaUptime': 86400}, {'time': '2019-06-05T00:00:00Z', 'deltaUptime': 86400}]})



How can I get only the last "time" value, ignoring the "deltaUptime"? I've tried so many things, and have not been able to accomplish this (what I know should be simple) task..



>>> for key, value in result.items():
...  type(value)
... 
<class 'generator'>
>>> for key, value in result.items():
...   for item in value:
...     type(item)
... 
<class 'dict'>
<class 'dict'>
<class 'dict'>
<class 'dict'>
<class 'dict'>
<class 'dict'>
<class 'dict'>
>>> for key, value in result.items():
...   key[1]["host"]
...   for item in value:
...     list(item.items())[0]
... 
'my.host.net'
('time', '2019-05-30T00:00:00Z')
('time', '2019-05-31T00:00:00Z')
('time', '2019-06-01T00:00:00Z')
('time', '2019-06-02T00:00:00Z')
('time', '2019-06-03T00:00:00Z')
('time', '2019-06-04T00:00:00Z')
('time', '2019-06-05T00:00:00Z')
>>> for key, value in result.items():
...   key[1]["host"]
...   list(value.items())[0]
... 
'my.host.net'
Traceback (most recent call last):
  File "<stdin>", line 3, in <module>
AttributeError: 'generator' object has no attribute 'items'



RE: How do I get the last element? - Gribouillis - Jun-07-2019

Try this perhaps
[x['time'] for x in value]



RE: How do I get the last element? - heiner55 - Jun-07-2019

Which library or database delivers "ResultSet"?


RE: How do I get the last element? - jefsummers - Jun-07-2019

MySQL at least, possibly all of them. The cursor.fetchall() instruction returns a ResultSet.

See https://pynative.com/python-mysql-select-query-to-fetch-data/


RE: How do I get the last element? - heiner55 - Jun-07-2019

Ok, I understood.


RE: How do I get the last element? - DaytonJones - Jun-07-2019

(Jun-07-2019, 03:00 PM)heiner55 Wrote: Which library or database delivers "ResultSet"?

In this case, ResultSet is returned from InfluxDBClient


RE: How do I get the last element? - heiner55 - Jun-07-2019

I don't have InfluxDbDClient, so I cannot help.


RE: How do I get the last element? - DaytonJones - Jun-10-2019

(Jun-07-2019, 04:49 PM)heiner55 Wrote: I don't have InfluxDbDClient, so I cannot help.

Couldn't you just use the ResultSet I have, and try to parse it out?


RE: How do I get the last element? - snippsat - Jun-10-2019

(Jun-10-2019, 02:49 PM)DaytonJones Wrote: Couldn't you just use the ResultSet I have, and try to parse it out?
It's not a valid data(json) that we can just copy and test out.
There is a .raw parameter to access the raw JSON response from InfluxDB.
You can post the .raw result then can we help parse out wanted result.
getting-started-python-influxdb
Quote:The query() function returns a ResultSet object (API Docs),
which contains all the data of the result along with some convenience methods.
Our query is requesting all the measurements in our pyexample database, grouped by user.
You can use the .raw parameter to access the raw JSON response from InfluxDB:

Looking at parse data on a typically example(data coming from json) which return Python dictionary with mix of dictionary/list.
Just using a other example.
data = {
  "name": "John",
  "age": 30,
  "married": True,
  "divorced": False,
  "children": ("Ann","Billy"),
  "pets": None,
  "cars": [
    {"model": "BMW 230", "mpg": 27.5},
    {"model": "Ford Edge", "mpg": 24.1}
  ]
} 
So parsing out value what many struggle with is to add [0],[1] ect. or can loop to access the dictionary in the list.
You also have dictionary/list mix in data shown.
>>> data['age']
30
>>> data['cars']
[{'model': 'BMW 230', 'mpg': 27.5}, {'model': 'Ford Edge', 'mpg': 24.1}]
>>> # Now need get into list
>>> data['cars'][0]['model']
'BMW 230'
>>> data['cars'][1]['model']
'Ford Edge'



RE: How do I get the last element? - DaytonJones - Jun-10-2019

I'd somehow missed Gribouillis' reply, and that got me where I needed to go, with one minor addition:

[x['time'] for x in value]
returns the list of "time", so to get the last item in that list:

>>> for key, value in result.items():                                           
   key[1]["host"], [x['time'] for x in value][-1]
That gives me:
('my.host.net', '2019-06-10T00:00:00Z')


Thanks for all the replies, and I'm sure there are plenty more suggestions/ways but this works for me.