Python Forum
How do I get the last element?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do I get the last element?
#1
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'
Reply
#2
Try this perhaps
[x['time'] for x in value]
Reply
#3
Which library or database delivers "ResultSet"?
Reply
#4
MySQL at least, possibly all of them. The cursor.fetchall() instruction returns a ResultSet.

See https://pynative.com/python-mysql-select...etch-data/
Reply
#5
Ok, I understood.
Reply
#6
(Jun-07-2019, 03:00 PM)heiner55 Wrote: Which library or database delivers "ResultSet"?

In this case, ResultSet is returned from InfluxDBClient
Reply
#7
I don't have InfluxDbDClient, so I cannot help.
Reply
#8
(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?
Reply
#9
(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'
Reply
#10
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Unable to locate element no such element gahhon 6 4,370 Feb-18-2019, 02:09 PM
Last Post: gahhon
  Change single element in 2D list changes every 1D element AceScottie 9 11,947 Nov-13-2017, 07:05 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020