首页 > 解决方案 > Python字典将值插入为元组而不是浮点数

问题描述

我正在尝试获取不同加密货币的代码,并将这些加密货币的相关值添加到平面字典中,以便稍后我可以将它们的值和项目保存为 2 个单独的列表。

我需要我的字典的所有值都是 int 或 float 类型。但是,我遇到了这个问题,当添加到我的字典时,python 正在将我的值的类型转换为元组,无论我是否在将其插入字典之前将其解析为浮点数。

我拥有的股票代码上的值类型如下:

代码值

代码的 json 值,如果有人想运行它:

{'symbol': 'BTCUSDT', 'priceChange': '1170.82000000', 'priceChangePercent': '4.519', 'weightedAvgPrice': '27116.45253287', 'prevClosePrice': '25909.19000000', 'lastPrice': '27080.01000000', 'lastQty': '0.00126000', 'bidPrice': '27080.00000000', 'bidQty': '2.34551500', 'askPrice': '27080.01000000', 'askQty': '1.48636700', 'openPrice': '25909.19000000', 'highPrice': '28422.00000000', 'lowPrice': '25810.56000000', 'volume': '153836.68308900', 'quoteVolume': '4171505114.79728177', 'openTime': 1609012754503, 'closeTime': 1609099154503, 'firstId': 527277428, 'lastId': 529816566, 'count': 2539139}

这是我生成列表的代码:

crypto_value_inputs = {}
for crypto in cryptos:
    s = crypto['symbol']
    ticker = tickers[s]
    crypto_value_inputs[s + "_last_price"] = float(ticker['lastPrice'])
    crypto_value_inputs[s + "_change_percent"] = float(ticker['priceChangePercent']),
    crypto_value_inputs[s + "_average_price"] = float(ticker['weightedAvgPrice']),
    crypto_value_inputs[s + "_open_price"] = float(ticker['openPrice']),
    crypto_value_inputs[s + "_last_close_price"] = float(ticker['prevClosePrice']),
    crypto_value_inputs[s + "_last_quantity"] = float(ticker['lastQty']),
    crypto_value_inputs[s + "_bid_price"] = float(ticker['bidPrice']),
    crypto_value_inputs[s + "_bid_quantity"] = float(ticker['bidQty']),
    crypto_value_inputs[s + "_ask_price"] = float(ticker['askPrice']),
    crypto_value_inputs[s + "_ask_quantity"] = float(ticker['askQty']),
    crypto_value_inputs[s + "_high_price"] = float(ticker['highPrice']),
    crypto_value_inputs[s + "_low_price"] = float(ticker['lowPrice']),
    crypto_value_inputs[s + "_volume"] = float(ticker['volume']),
    crypto_value_inputs[s + "_quote_volume"] = float(ticker['quoteVolume']),
    crypto_value_inputs[s + "_open_time"] = float(ticker['openTime']),
    crypto_value_inputs[s + "_close_time"] = float(ticker['closeTime'])
return crypto_value_inputs

以下是值类型:

加密值类型

输出字典的实际 JSON 值:

{'BTCUSDT_last_price': 27080.01, 'BTCUSDT_change_percent': (4.519,), 'BTCUSDT_average_price': (27116.45253287,), 'BTCUSDT_open_price': (25909.19,), 'BTCUSDT_last_close_price': (25909.19,), 'BTCUSDT_last_quantity': (0.00126,), 'BTCUSDT_bid_price': (27080.0,), 'BTCUSDT_bid_quantity': (2.345515,), 'BTCUSDT_ask_price': (27080.01,), 'BTCUSDT_ask_quantity': (1.486367,), 'BTCUSDT_high_price': (28422.0,), 'BTCUSDT_low_price': (25810.56,), 'BTCUSDT_volume': (153836.683089,), 'BTCUSDT_quote_volume': (4171505114.7972817,), 'BTCUSDT_open_time': (1609012754503.0,), 'BTCUSDT_close_time': 1609099154503.0, 'ETHUSDT_last_price': 705.41, 'ETHUSDT_change_percent': (9.269,), 'ETHUSDT_average_price': (665.33076424,), 'ETHUSDT_open_price': (645.57,), 'ETHUSDT_last_close_price': (645.57,), 'ETHUSDT_last_quantity': (0.61474,), 'ETHUSDT_bid_price': (705.39,), 'ETHUSDT_bid_quantity': (0.61474,), 'ETHUSDT_ask_price': (705.41,), 'ETHUSDT_ask_quantity': (3.71429,), 'ETHUSDT_high_price': (717.13,), 'ETHUSDT_low_price': (625.0,), 'ETHUSDT_volume': (1747976.61878,), 'ETHUSDT_quote_volume': (1162982619.6446111,), 'ETHUSDT_open_time': (1609012754510.0,), 'ETHUSDT_close_time': 1609099154510.0, 'ETCUSDT_last_price': 5.9665, 'ETCUSDT_change_percent': (3.603,), 'ETCUSDT_average_price': (5.85436613,), 'ETCUSDT_open_price': (5.759,), 'ETCUSDT_last_close_price': (5.7553,), 'ETCUSDT_last_quantity': (10.62,), 'ETCUSDT_bid_price': (5.9645,), 'ETCUSDT_bid_quantity': (100.0,), 'ETCUSDT_ask_price': (5.9662,), 'ETCUSDT_ask_quantity': (11.54,), 'ETCUSDT_high_price': (6.1197,), 'ETCUSDT_low_price': (5.4914,), 'ETCUSDT_volume': (4916271.75,), 'ETCUSDT_quote_volume': (28781654.820338,), 'ETCUSDT_open_time': (1609012751019.0,), 'ETCUSDT_close_time': 1609099151019.0, 'XRPUSDT_last_price': 0.29512, 'XRPUSDT_change_percent': (-0.727,), 'XRPUSDT_average_price': (0.29250503,), 'XRPUSDT_open_price': (0.29728,), 'XRPUSDT_last_close_price': (0.29746,), 'XRPUSDT_last_quantity': (138.3,), 'XRPUSDT_bid_price': (0.29512,), 'XRPUSDT_bid_quantity': (10419.5,), 'XRPUSDT_ask_price': (0.29513,), 'XRPUSDT_ask_quantity': (485.6,), 'XRPUSDT_high_price': (0.312,), 'XRPUSDT_low_price': (0.266,), 'XRPUSDT_volume': (1478190048.7,), 'XRPUSDT_quote_volume': (432378019.148203,), 'XRPUSDT_open_time': (1609012754189.0,), 'XRPUSDT_close_time': 1609099154189.0}

我稍后将这些数据作为 input_layer 传递给我的 REST 服务。我只取值,将其转换为列表并将其发送出去。但它没有通过验证,因为该列表出于某种原因包含元组而不是所有数值。

    aggregate_input = {
        "currency": currency['_id'],
        "time_of_aggregation": time_of_aggregation,
        "network_version": version['_id'],
        "input_layer": list(inputs.values())
    }

谁能解释发生了什么?我已经做了一些搜索。在将其发送到我的宁静服务之前,我还尝试使用 numpy 将所有值转换为浮点数,但由于元组值,即使 numpy 也存在问题。

标签: pythondictionary

解决方案


,在某些行的末尾有,这使您的值成为一个元组


推荐阅读