【问题标题】:Python/Pycharm: For loop fetching string instead of DictionaryPython/Pycharm:用于循环获取字符串而不是字典
【发布时间】:2020-04-08 12:18:01
【问题描述】:

我是 python 新手,所以这似乎是一个非常愚蠢的问题,请多多包涵。

我正在使用以下代码: (如果还附上调试图像)

for connector in la_details['properties']['parameters']['$connections']['value']:
    if connector['connectionName'] in allowed_connectors:
        result = True

现在,它不是在 'connector' 变量中获取 'dict' 值,而是获取一个 'string',因此 'if' 语句给出了错误:

string indices must be integers

有人可以指导我如何获取“dict”值而不是“string”吗?我试图用谷歌搜索,但找不到任何与这种情况相匹配的东西,可能是因为我使用了错误的术语。

编辑:print(la_details) 产生以下输出:

{u'name': u'REDACTED',
 u'tags': {},
 u'id': u'/subscriptions/REDACTED/resourceGroups/REDACTED/providers/Microsoft.Logic/workflows/REDACTED',
 u'location': u'REDACTED',
 u'type': u'Microsoft.Logic/workflows',
 u'properties': {u'definition': {u'parameters': {u'$connections': {u'defaultValue': {}, u'type': u'Object'}}, u'triggers': {u'Recurrence': {u'recurrence': {u'frequency': u'Month', u'interval': 1}, u'type': u'Recurrence'}}, u'outputs': {}, u'actions': {u'Send_an_email_(V2)': {u'inputs': {u'body': {u'Body': u'<p>TEST</p>', u'To': u'REDACTED', u'Subject': u'TEST'}, u'path': u'/v2/Mail', u'host': {u'connection': {u'name': u"@parameters('$connections')['office365']['connectionId']"}}, u'method': u'post'}, u'runAfter': {}, u'type': u'ApiConnection'}, u'Get_tables_(V2)': {u'inputs': {u'path': u"/v2/datasets/@{encodeURIComponent(encodeURIComponent('default'))},@{encodeURIComponent(encodeURIComponent('default'))}/tables", u'host': {u'connection': {u'name': u"@parameters('$connections')['sql']['connectionId']"}}, u'method': u'get'}, u'runAfter': {u'Send_an_email_(V2)': [u'Succeeded']}, u'type': u'ApiConnection'}}, u'contentVersion': u'1.0.0.0', u'$schema': u'https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#'}, u'version': u'REDACTED', u'parameters': {u'$connections': {u'value': {u'office365': {u'connectionId': u'/subscriptions/REDACTED/resourceGroups/REDACTED/providers/Microsoft.Web/connections/office365', u'id': u'/subscriptions/REDACTED/providers/Microsoft.Web/locations/eastus/managedApis/office365', u'connectionName': u'office365'}, u'sql': {u'connectionId': u'/subscriptions/REDACTED/resourceGroups/REDACTED/providers/Microsoft.Web/connections/sql', u'id': u'/subscriptions/REDACTED/providers/Microsoft.Web/locations/eastus/managedApis/sql', u'connectionName': u'sql'}}}}, u'integrationServiceEnvironment': {u'type': u'Microsoft.Logic/integrationServiceEnvironments', u'name': u'ise-demo-res', u'id': u'/subscriptions/REDACTED/resourceGroups/REDACTED/providers/Microsoft.Logic/integrationServiceEnvironments/ise-demo-res'}, u'endpointsConfiguration': {u'connector': {u'outgoingIpAddresses': [{u'address': u'40.71.11.80/28'}, {u'address': u'REDACTED'}, {u'address': u'REDACTED'}, {u'address': u'REDACTED'}, {u'address': u'REDACTED'}, {u'address': u'REDACTED'}, {u'address': u'REDACTED'}, {u'address': u'REDACTED'}, {u'address': u'REDACTED'}]}, u'workflow': {u'outgoingIpAddresses': [{u'address': u'REDACTED'}, {u'address': u'REDACTED'}], u'accessEndpointIpAddresses': [{u'address': u'REDACTED'}]}}, u'state': u'Enabled', u'accessEndpoint': u'REDACTED', u'createdTime': u'2020-03-12T14:45:34.6908438Z', u'changedTime': u'2020-04-08T10:35:32.3388762Z', u'provisioningState': u'Succeeded'}}

【问题讨论】:

  • 能否在您的问题中添加 la_details 的打印件
  • 添加了 la_details 的打印。

标签: python dictionary iteration


【解决方案1】:

在 python for ... in ... 中,返回列表中的值或字典中的键(如您的示例中所示),以下是执行循环的正确方法。

la_details = {
  'properties': {
    'parameters': {
      '$connections': {
        'value': {
          'office365': { 'connectionName': 'office365' },
          'sql': { 'connectionName': 'sql' },
        }
      }
    }
  }
}

allowed_connectors = ['sql']

#for connector in la_details['properties']['parameters']['$connections']['value']:
#  if connector['connectionName'] in allowed_connectors:
#    return True
#  else:
#    return 'False

values = la_details['properties']['parameters']['$connections']['value']

for connector in values:
  if values[connector]['connectionName'] in allowed_connectors:
    print('{0} is allowed'.format(values[connector]['connectionName']))
  else:
    print('{0} is not allowed'.format(values[connector]['connectionName']))

【讨论】:

  • “字典案例中的属性名称” => 它被命名为“键” - 属性为something totally different
  • 另外您可能对dict.items() 方法感兴趣。
  • 哦,是的,您的意思可能是“列表”而不是“数组”;-)
  • 我的错误,我没有做太多 python 大声笑我去的是 JavaScript/C#。请随时编辑我的答案并改进它
猜你喜欢
  • 2023-01-31
  • 1970-01-01
  • 2016-05-30
  • 2014-10-26
  • 2013-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多