2017-12-20 11 views
-1

でアレイから複数の値を単一の値を比較すると、私のコードは次のようになります。のpython

import requests 
import re 
import mechanize 
import urllib 
import json 

htmltext = urllib.urlopen("https://www.binance.com/api/v1/klines?symbol=BCDBTC&interval=4h") 

data = json.load(htmltext) 

current_price= data[len(data)-1][4] 
last_prices= (data[len(data)-2][4],data[len(data)-3][4],data[len(data)-4][4],data[len(data)-5][4],data[len(data)-6][4]) 
last_volumes= (data[len(data)-2][5],data[len(data)-3][5],data[len(data)-4][5],data[len(data)-5][5],data[len(data)-6][5]) 
current_volume= data[len(data)-1][5] 

print current_price 
print last_prices 
if current_price > last_prices: 
    print "the current price is greater than the last 5" 
print current_volume 
print last_volumes 
if current_volume > last_volumes: 
    print "the current volume is higher than the last 5" 

しかし、私の出力はこれです:ここでの問題は、現在のボリュームは確かではないです

0.00495600 
(u'0.00492500', u'0.00366300', u'0.00332800', u'0.00333800', u'0.00308000') 
the current price is greater than the last 5 
938.01000000 
(u'29687.32500000', u'14740.03800000', u'9366.77400000', u'10324.83200000', u'44953.53400000') 
the current volume is higher than the last 5 

最後の5よりも大きいが、それはまだ私がここに https://www.binance.com/tradeDetail.html?symbol=BCD_BTC

からデータをつかんだにかかわらず

それをプリントアウトしています

+1

なぜ 'データ[-2]'の代わりに 'データ[LEN(データの)-2] 'はどこにでもありますか? – tonypdmtr

答えて

1

まず、データがすべて数値に変換されるわけではなく、その多くは文字列形式です。浮動小数点数にそれらのすべてを変換するには、次の式を使用することができます。

data = [[float(x) for x in row] for row in data] 

次に、if文では、あなたは数字の配列と数を比較しています。一緒にすべてを置く

if current_price > max(last_prices): 
    print "the current price is greater than the last 5" 

:何が欲しいの順序で最大でこの数を比較することである

# code to obtain data is the same 

PRICE = 4 
VOLUME = 5 
LAST_ROW = -1 

data = [[float(x) for x in row] for row in data] 

current_price = data[LAST_ROW][PRICE] 
current_volume = data[LAST_ROW][VOLUME] 
last_prices = tuple(row[PRICE] for row in data[-6:-1]) 
last_volumes = tuple(row[VOLUME] for row in data[-6:-1]) 

print 'current_price =', current_price 
print 'current_volume =', current_volume 
print 'last_prices =', last_prices 
print 'last_volumes =', last_volumes 

if current_price > max(last_prices): 
    print "the current price is greater than the last 5" 

if current_volume > max(last_volumes): 
    print "the current volume is higher than the last 5" 
+0

'data [-6:-1]' ==>直前の5行、 'data [-6:]' ==>最後の6行 –