简体   繁体   English

如何从列表中获取值,然后在基本数学方程式中使用它?

[英]How can I take a value from a list and then use it in a basic math equation?

I am trying to take a float value from an api provided list. 我正在尝试从api提供的列表中获取浮点值。 I was able to make it look like a float value when I print it but I don't think it is a true number because I cant use it in my math equation. 我可以在打印时使它看起来像浮点值,但我认为它不是真实的数字,因为我无法在数学方程式中使用它。 There are other functions to get trailTrigger and stopLoss but I am happy that they work. 还有其他函数可以获取TrailTrigger和stopLoss,但我很高兴它们能够正常工作。 I just can't get lastTradePrice to be a float I can use. 我只是无法让lastTradePrice成为我可以使用的浮点数。

    # Call My last trade from Coinbase
def coinBaseMyLastTrade():
    global lastTradePrice
    global lastTradeSize
    global lastTradeFee
    global lastTradeSide
    global lastTradeSettled
    myLastTrade = requests.get(api_url + 'fills?product_id=BTC-USDC&limit=1', auth=auth)
    lastTradeList = myLastTrade.json()
    lastTradePrice = [float(lastTradeList_item['price']) for lastTradeList_item in lastTradeList]
    lastTradeSize = [float(lastTradeList_item['size']) for lastTradeList_item in lastTradeList]
    lastTradeFee = [float(lastTradeList_item['fee']) for lastTradeList_item in lastTradeList]
    lastTradeSide = [str(lastTradeList_item['side']) for lastTradeList_item in lastTradeList]
    lastTradeSettled = [lastTradeList_item['settled'] for lastTradeList_item in lastTradeList]
    print 'My Last Trade Price =',lastTradePrice
    print 'My Last Trade Size =',lastTradeSize
    print 'My Last Trade Fee =',lastTradeFee
    print 'My Last Trade Side =',lastTradeSide
    print 'My Last Trade Settled =',lastTradeSettled
    return

# Work out the value to use to trigger the trail loop
def calculateTrailTriggerValue():
    global triggerValue
    global stoplossValue
    if "buy" in lastTradeSide:
        triggerValue = lastTradePrice * ((100+trailTrigger)/100)
        stoplossValue = lastTradePrice * ((100-stoploss)/100)
    elif "sell" in lastTradeSide:
        triggerValue = lastTradePrice * ((100-trailTrigger)/100)
        stoplossValue = lastTradePrice * ((100+stoploss)/100)
    print 'Trail Trigger $=',triggerValue
    print 'Stop Loss $=',stoplossValue
    return

When I run the code I get the fault 运行代码时出现错误

triggerValue = lastTradePrice * ((100-trailTrigger)/100)
TypeError: can't multiply sequence by non-int of type 'float'

Its strange because I used this code on a different list and I was able to use the values but It doesn't work with the code i need help with 这很奇怪,因为我在不同的列表上使用了此代码,并且能够使用这些值,但不适用于需要帮助的代码

# Call 24hour stats
def coinBasePastDayStats():
    dayStats = requests.get('https://api.pro.coinbase.com/products/BTC-USDC/stats')
    dayHigh = float(dayStats.json()['high'])
    dayLow = float(dayStats.json()['low'])
    dayChange = dayHigh - dayLow
    global dayChangePercent
    dayChangePercent = (dayChange / dayHigh) * 100
    print 'Last 24hr High =', dayHigh
    print 'Last 24hr Low =', dayLow
    print 'Last 24hr Change =', dayChange
    print 'Last 24hr Change Percent =', {:.2f}".format(dayChangePercent),'%'
    return

If anyone could help me take a list value and use it in a basic math equation I would be very grateful. 如果有人可以帮助我取得列表值并将其用于基本数学方程式中,我将不胜感激。 Cheers 干杯

If you want to multiply an entire list like this I'd suggest using numpy arrays 如果您想像这样将整个列表相乘,建议使用numpy数组

import numpy as np
arr = np.array([ i for i in range(10)])
arr *= 2

It looks like you are on the right track. 看来您在正确的轨道上。 But as the others have said you are doing the operation on the list itself and python doesn't support that. 但是正如其他人所说,您正在对列表本身进行操作,而python不支持该操作。

Without the use of numpy you have to loop over the list and do the operation on each value in turn. 在不使用numpy的情况下,您必须遍历列表并依次对每个值进行操作。

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

相关问题 如何将 2 个列表的值输入数学方程式 - How can I input the values of 2 lists into a math equation 如何从 SymPy 方程中提取分母和分子列表? - How can I extract that list of denominator and numerator from a SymPy equation? 如何从 discord 获取用户输入并将其存储为变量以在方程式中使用? Discord.py - How do i take user input from discord and store it as a variable to use in an equation? Discord.py 如何使用str.replace从旋转框获取值,然后在文本文件中替换该值的实例? - How can I use a str.replace to take a value from a spinbox, and then replace an instance of that value in a text file? 如何使用 %r 作为等式替换? - How can I use %r as an equation replacement? 如何在公式中使用列表? - How can I use lists in an equation? 如何将数学方程式从函数写入文件? - How to write math equation from function to a file? 如何从 python 中的字符串中提取数学方程式? - How to extract Math Equation from a string in python? 如何从我的列表中取出 x 和 y,以便我可以使用它来创建图表 - How to take out x and y from my list so I can use it to create a graph 如何在字符串中的方程式中获取操作数并将它们重新应用于最后更改的方程式? - How can I take the operands in an equation within a string and reapply them to the changed equation at the end?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM