PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, October 8, 2022

[FIXED] How do I take the mean of this output

 October 08, 2022     list, python, statistics     No comments   

Issue

I have this function

updated the code a bit

for c,d,y in zip(crypto.Wicker,crypto.Dateroundts,crypto.Dateround1ts):
        yst = cg.get_coin_market_chart_range_by_id(c,'usd', d, y)
        print(c)
        #yst = {k: [[np.nan, np.nan]] if not v else v for k, v in yst.items() }
        bst = list(yst.values())[0]
        caz = []
        for new_lst in bst:
            caz.append(new_lst[1])
            print(caz)

buu-inu
[1.7844348972932573e-05]
[1.7844348972932573e-05, 1.8661555226871793e-05]
[1.7844348972932573e-05, 1.8661555226871793e-05, 1.879232323095649e-05]
[1.7844348972932573e-05, 1.8661555226871793e-05, 1.879232323095649e-05, 1.8801994944205594e-05]

What I want is to take all the prices for the individual coin and take the mean. Then put said mean into the list. If I do something like mean(new_lst[1]) I'll get an error float object is not iterable

heres what print(bst) outputs

buu-inu
[[1637557978123, 1.7844348972932573e-05], [1637559018888, 1.8661555226871793e-05], [1637560001812, 1.879232323095649e-05], [1637560315539, 1.8801994944205594e-05]]

The first number is the time in unix and the second input is the price


Solution

Given this value of bst:

>>> bst = [[1637557978123, 1.7844348972932573e-05], [1637559018888, 1.8661555226871793e-05], [1637560001812, 1.879232323095649e-05], [1637560315539, 1.8801994944205594e-05]]

you can unpack each sublist into time and price and take the mean of the prices:

>>> from statistics import mean
>>> mean(price for [_, price] in bst)
1.8525055593741612e-05


Answered By - Samwise
Answer Checked By - Clifford M. (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing