Thursday, December 8, 2016

Re: Django Rest Framework Output as list of lists

Hi!

Seems like more StackOverflow question to me, since it has nothing to do with Django. But anyway, you could use list comprehensions:


    >>> result = [[x['date'], x['price']] for x in source['navs']]


Also, it looks like list of lists is overkill (list uses more memory than tupple), and if you don't plan to change inner lists, it would be better to do that:


    >>> result = [(x['date'], x['price']) for x in source['navs']]


Furthermore, it may be a good idea to use namedtuples here, it could help readability:


    >>> from collections import namedtuple
    >>> PriceOfTheDay = namedtuple('PriceOfTheDay', ('date', 'price'))
    >>> result = [PriceOfTheDay(x['date'], x['price']) for x in source['navs']]


...and after that you could access date and price via PriceOfTheDay attributes:


    >>> print(result[0].date, result[0].price)




On 8 Dec 2016, at 17:26, Artem Bernatskyy <artem.bernatskyy@gmail.com> wrote:

Hello,

How can i change this output

{"navs":[{"date":1480982400000,"price":86.57},{"date":1480896000000,"price":86.78},{"date":1480636800000,"price":87.29},{"date":1480550400000,"price":87.52},{"date":1480464000000,"price":87.19},{"date":1480377600000,"price":87.28},{"date":1480291200000,"price":87.47},{"date":1480032000000,"price":87.27},{"date":1479945600000,"price":87.24},{"date":1479859200000,"price":87.09},{"date":1479772800000,"price":87.21},{"date":1479686400000,"price":87.34},{"date":1479427200000,"price":87.48},{"date":1479340800000,"price":87.24},{"date":1479254400000,"price":87.26},{"date":1479168000000,"price":87.15},{"date":1479081600000,"price":87.48},{"date":1478822400000,"price":87.11},{"date":1478736000000,"price":86.88},{"date":1478649600000,"price":87.11},{"date":1478563200000,"price":87.2},{"date":1478476800000,"price":87.58},{"date":1478217600000,"price":88.4},{"date":1478131200000,"price":88.42},{"date":1478044800000,"price":88.4},{"date":1477872000000,"price":87.81},{"date":1477612800000,"price":87.57},{"date":1477526400000,"price":87.54},{"date":1477440000000,"price":87.43}

and etc ...

to this


[[1219968000000, 100.0], [1220227200000, 99.34], [1220313600000, 99.07], [1220400000000, 96.7], [1220486400000, 94.85], [1220572800000, 93.49], [1220832000000, 94.1], [1220918400000, 93.73], [1221004800000, 88.86], [1221091200000, 89.29], [1221177600000, 89.09], [1221436800000, 89.71], [1221523200000, 86.38], [1221609600000, 87.33], [1221696000000, 83.29], [1221782400000, 86.16], [1222041600000, 88.7], [1222128000000, 84.94], [1222214400000, 83.47], [1222300800000, 82.0], [1222387200000, 80.58], [1222646400000, 78.79], [1222732800000, 73.61], [1222819200000, 75.76], [1222905600000, 76.34], [1222992000000, 69.28], [1223251200000, 69.07], [1223337600000, 65.8], [1223424000000, 63.62], [1223510400000, 63.87], [1223596800000, 61.62], [1223856000000, 60.46], [1223942400000, 65.07], [1224028800000, 65.25], [1224115200000, 60.65], [1224201600000, 60.77], [1224460800000, 60.56], [1224547200000, 63.04], [1224633600000, 63.37], [1224720000000, 60.06], [1224806400000, 59.69], [1225065600000, 58.02], [1225152000000, 56.48], [1225238400000, 59.2],

In other words i need to turn any dict like object into list

Any help is appreciated


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/6eb02681-7404-4c5f-88dd-39308bc8414d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment