Is there a way to look for a part of a string ('USD' in 'USDSEK') in the keys of a dictionary and if found return the value?

huangapple go评论57阅读模式
英文:

Is there a way to look for a part of a string ('USD' in 'USDSEK') in the keys of a dictionary and if found return the value?

问题

可以用提供的字典来模拟当前的汇率:

exchange_rates['USDSEK']

使用该字典来定义类中的一个方法,该方法将给定的货币转换为基础货币:

mca.conversion_rate('USD')

以下是你尝试访问的字典:

exchange_rates = {'USDSEK': 10, 'EURSEK': 10}

你的逻辑主要是,如果找到给定的货币在字典的键中,就返回相同键位置的值:

def conversion_rate(self, given_currency):
    self.given_currency = given_currency
    for key in exchange_rates.keys():
        if given_currency in exchange_rates.keys():
            er = exchange_rates[key]
            return er

你断言应该返回 USD 货币,根据字典,它应该是 10:

mca = MultiCurrencyAccount('SEK')
assert mca.conversion_rate('USD') == 10, f"{mca.conversion_rate('USD')} != 10"

但是,当你运行它时,出现了以下错误:

AssertionError: mca.conversion_rate('USD')=None != 10
英文:

We can model current exchange rates with a provided dictionary
>>> exchange_rates['USDSEK']
10

Use that dictionary to define a method in the class that converts a given currency to the base currency
>>> mca.conversion_rate('USD')
10

#This is the dictionary I'm trying to access

exchange_rates = {'USDSEK': 10, 'EURSEK': 10}

#Here my logic is mostly that if I find the given currency in the key I will return the value of the same key position

def conversion_rate(self, given_currency):
    self.given_currency = given_currency
    for key in exchange_rates.keys():
        if given_currency in exchange_rates.keys():
            er = (exchange_rates[key]).values()
            return er

#Assertion gives the USD currency and according to the dictionary it should be 10

mca = MultiCurrencyAccount('SEK')
assert mca.conversion_rate('USD') == 10, f"{mca.conversion_rate('USD')=} != 10"

#But when I run it it appears the following

AssertionError Traceback (most recent call last)
Input In [193], in <cell line: 2>()
1 mca = MultiCurrencyAccount('SEK')
----> 2 assert mca.conversion_rate('USD') == 10, f"{mca.conversion_rate('USD')=} != 10"

AssertionError: mca.conversion_rate('USD')=None != 10

答案1

得分: 0

如果可能的话,我建议你重新构建你的数据。有两个单独的字典,一个用于从某种货币转换,另一个用于转换到某种货币,并在插入之前拆分字符串。这可能会占用更多内存,但如果数据库足够大,速度会快得多。

如果不可能的话,你可以这样做:

for key, value in exchange_rate.items():
    if currency in key:
        return value

(你可以使用不同的比较方式,比如 .startswith

英文:

If possible, I'd advise you to restructure your data. Have two separate dicts for conversion from and conversion to, and split the string before insertion. This might take more memory, but it will be considerably faster if the database is large enough.

If that's not possible, you can do this:

for key, value in exchange_rate.items():
    if currency in key:
        return value

(You might use a different comparison like .startswith)

huangapple
  • 本文由 发表于 2023年2月17日 23:36:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75486308.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定