遍历非内插符号数组python等价物

时间:2019-04-11 15:00:18

标签: python ruby

我目前正在从ruby模块用python编写脚本。我在将ruby转换为python的这方面遇到麻烦。

Ruby:

plan_metrics[test_name]={ passed_count: 0, blocked_count: 0, untested_count: 0, failed_count: 0, reviewed_count: 0, test_harness_issue_count: 0, bug_failure_count: 0, defect_list: [] }
entry['runs'].each do |run|
      metric_hash = plan_metrics[test_name]
      %i[passed_count blocked_count untested_count failed_count].each do |key|
        metric_hash[key] = metric_hash[key] + run[key.to_s]
      end

在此代码中,entry['runs']保留passed_countblocked_countuntested_countfailed_count的实际值,但使用多个字典。应该对它们进行迭代,然后将所有值加起来并放入passed_count

中的一个符号(即metric_hash)中

现在,当我尝试翻译成python时,我不是在使用符号,而是这样做

我的Python翻译:

plan_metrics[test_name]={ "passed_count": 0, "blocked_count": 0, "untested_count": 0, "failed_count": 0, "reviewed_count": 0, "test_harness_issue_count": 0, "bug_failure_count": 0, "defect_list": [] }
for run in entry["runs"]:
       metric_hash = plan_metrics[test_name]
       for key in [metric_hash["passed_count"], metric_hash["blocked_count"], metric_hash["untested_count"], metric_hash["failed_count"]:
             metric_hash[key] = metric_hash[key] + run[str(key)]

但是为此,我在行KeyError: 0上获得了metric_hash[key] = metric_hash[key] + run[str(key)]

for key in [metric_hash["passed_count"], metric_hash["blocked_count"], metric_hash["untested_count"], metric_hash["failed_count"]:

的适当等价物

%i[passed_count blocked_count untested_count failed_count].each do |key|

,如果是,是什么导致KeyError:0?

如果不是这样,我该如何在python中通过插入符号数组来完成ruby示例所做的工作

如果您需要有关数据的更多信息,请让我知道print()谢谢

1 个答案:

答案 0 :(得分:2)

在python中,您可以这样做

console.log

这意味着# -*- coding: utf-8 -*- import ctypes from ctypes import wintypes _GetShortPathNameW = ctypes.windll.kernel32.GetShortPathNameW _GetShortPathNameW.argtypes = [wintypes.LPCWSTR, wintypes.LPWSTR, wintypes.DWORD] _GetShortPathNameW.restype = wintypes.DWORD def get_short_path_name(long_name): """ Gets the short path name of a given long path. http://stackoverflow.com/a/23598461/200291 """ output_buf_size = 0 while True: output_buf = ctypes.create_unicode_buffer(output_buf_size) needed = _GetShortPathNameW(long_name, output_buf, output_buf_size) if output_buf_size >= needed: return output_buf.value else: output_buf_size = needed print get_short_path_name(u"C:\\Users\\zvi\\Desktop\\אאאאא") 从列表 for key in [metric_hash["passed_count"], metric_hash["blocked_count"], metric_hash["untested_count"], metric_hash["failed_count"]: 中获取值。你知道为什么吗?