Python函数改变了元组列表,每个元组都有两个错误的元组

时间:2017-11-10 11:15:47

标签: python python-3.x

def calculate_cross_rate(currency0, currency1, data):
    conversion = float(data['USD%s' % currency1]) / float(data['USD%s' % currency0])
    return float("{:.2f}".format(conversion))

def get_cross_rates(currencies_combinations, data):
    """
    currencies_combination is 
    [({'symbol': '€', 'name': 'Euro', 'code': 'EUR'}, {'symbol': '\u200e₸', 'name': 'Kazakhstani Tenge', 'code': 'KZT'}),
     ({'symbol': '€', 'name': 'Euro', 'code': 'EUR'}, {'symbol': 'Bs', 'name': 'Bolivian Boliviano', 'code': 'BOB'}),
     ({'symbol': '\u200e₸', 'name': 'Kazakhstani Tenge', 'code': 'KZT'}, {'symbol': 'Bs', 'name': 'Bolivian Boliviano', 'code': 'BOB'})]
    """
    for currencies in currencies_combinations:
        currency0, currency1 = currencies[0]['code'], currencies[1]['code']
        output0 = calculate_cross_rate(currency0, currency1, data)
        currencies[0]['conversion'] = output0
        print(currency0, currency1, output0)
        output1 = calculate_cross_rate(currency1, currency0, data)
        currencies[1]['conversion'] = output1
        print(currency1, currency0, output1)
    for currencies in currencies_combinations:
        print(currencies[0]['code'], currencies[1]['code'], currencies[0]['conversion'])
        print(currencies[1]['code'], currencies[0]['code'], currencies[1]['conversion'])
    return currencies_combinations



Output
EUR KZT 0.75
KZT EUR 1.33
EUR BOB 0.67
BOB EUR 1.5
KZT BOB 0.89
BOB KZT 1.12

EUR KZT 0.67
KZT EUR 0.89
EUR BOB 0.67
BOB EUR 1.12
KZT BOB 0.89
BOB KZT 1.12

我们正在添加一个新密钥'转换'。但是,当我们打印出相同的键和值时,它是不同的。

0 个答案:

没有答案