>>> input_symbol = "ETHBTC"
>>> target_dictionary = [d for d in My_dic if d.get("symbol") == input_symbol][0]
>>> print((target_dictionary.get("bidPrice"), target_dictionary.get("askPrice")))
('0.03589300', '0.03589600')
包装在函数中,如果找不到符号,还应考虑:
def get_prices_for_symbol(sbl):
target_dictionaries = [d for d in My_dic if d.get("symbol") == sbl]
if target_dictionaries:
target_dictionary = target_dictionaries[0]
return (target_dictionary.get("bidPrice"), target_dictionary.get("askPrice"))
else:
raise Exception(f"Symbol {sbl} was not found.")
>>> get_prices_for_symbol("ETHBTC")
('0.03589300', '0.03589600')
>>> get_prices_for_symbol("LTCBTC")
('0.00539200', '0.00539300')
>>> get_prices_for_symbol("test")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 7, in get_prices_for_symbol
Exception: Symbol test was not found.
你可以做:
以下是获得直觉的一种方法:
包装在函数中,如果找不到符号,还应考虑:
你可以试试下面的代码
相关问题 更多 >
编程相关推荐