如何使用`.format`解析字典中的键。

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

How to parse key's uisng .format in dictionary

问题

  1. 试图使用 .format 从字典中获取值
  2. ~~~
  3. test_dict={'name':'John'}
  4. input_value='name'
  5. value= test_dict[input_value]
  6. print(value)
  7. ~~~
  8. 但它会抛出错误
  9. ~~~
  10. TypeError: unhashable type: 'dict'
  11. ~~~
  12. 我无法找到这个简单程序的解决方法
英文:

I am trying to fetch the value from a dictionary using .format

  1. test_dict={'name':'John'}
  2. input_value='name'
  3. value= test_dict[{}].format(input_value)
  4. print(value)

But it throw the error says

  1. TypeError: unhashable type: 'dict'

i am not able to find a solution to this simple progam

答案1

得分: 1

这是正确的格式:

  1. test_dict={'name':'John'}
  2. input_value='name'
  3. value= test_dict['{}'.format(input_value)]
  4. print(value)
  5. #John
英文:

This is the correct format:

  1. test_dict={'name':'John'}
  2. input_value='name'
  3. value= test_dict['{}'.format(input_value)]
  4. print(value)
  5. #John

答案2

得分: 0

我不知道为什么需要以这种方式构建密钥,但我进行的效率测试显示这是最低效的方式。

  1. import sys
  2. sys.version # 3.10.11 (main, Apr 5 2023, 14:15:10) [GCC 9.4.0]

测试1(format):

  1. %%timeit
  2. test_dict={'name':'John'}
  3. input_value='name'
  4. value = test_dict['{}'.format(input_value)]
  5. # 191 ns ± 32.4 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)

测试(% 运算符):

  1. %%timeit
  2. test_dict={'name':'John'}
  3. input_value='name'
  4. value = test_dict['%s' % input_value]
  5. # 154 ns ± 43.3 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)

测试(f-string):

  1. %%timeit
  2. test_dict={'name':'John'}
  3. input_value='name'
  4. value = test_dict[f'{input_value}']
  5. # 123 ns ± 19.7 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)

测试(无字符串格式化):

  1. %%timeit
  2. test_dict={'name':'John'}
  3. input_value='name'
  4. value = test_dict[input_value]
  5. # 96.8 ns ± 1.48 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)
英文:

I don't know why it was necessary to build the key in this way, but the efficiency tests I conducted showed that this is the most ineffective way.

  1. import sys
  2. sys.version # 3.10.11 (main, Apr 5 2023, 14:15:10) [GCC 9.4.0]

Test1 (format):

  1. %%timeit
  2. test_dict={'name':'John'}
  3. input_value='name'
  4. value = test_dict['{}'.format(input_value)]
  5. # 191 ns ± 32.4 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)

Test (% Operator):

  1. %%timeit
  2. test_dict={'name':'John'}
  3. input_value='name'
  4. value = test_dict['%s' % input_value]
  5. # 154 ns ± 43.3 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)

Test (f-string):

  1. %%timeit
  2. test_dict={'name':'John'}
  3. input_value='name'
  4. value = test_dict[f'{input_value}']
  5. # 123 ns ± 19.7 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)

Test (without string formating):

  1. %%timeit
  2. test_dict={'name':'John'}
  3. input_value='name'
  4. value = test_dict[input_value]
  5. # 96.8 ns ± 1.48 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)

huangapple
  • 本文由 发表于 2023年5月29日 14:24:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76355085.html
匿名

发表评论

匿名网友

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

确定