【问题标题】:Python .Index of a Multidimentional ArrayPython .Index 多维数组
【发布时间】:2018-05-07 12:14:17
【问题描述】:
arrrSample =

 {'art_other_country_names': None, 
  'pk_ba_country_id': 186,
  'sin_active': 1,
  'txt_remarks': '',
  'vhr_common_country_name': 'Uzbekistan',
  'vhr_formal_country_name': 'Republic of Uzbekistan',
  'vhr_nationality': '',
  'vhr_short_country_name': '',
  'vhr_transaction_currency_code': 'UZS'},


 {'art_other_country_names': None,
  'pk_ba_country_id': 185,
  'sin_active': 1,
  'txt_remarks': '',
  'vhr_common_country_name': 'Uruguay',
  'vhr_formal_country_name': 'Oriental Republic of Uruguay',
  'vhr_nationality': '',
  'vhr_short_country_name': '',
  'vhr_transaction_currency_code': 'UYU'},


 {'art_other_country_names': None,
  'pk_ba_country_id': 184,
  'sin_active': 1,
  'txt_remarks': '',
  'vhr_common_country_name': 'United States',
  'vhr_formal_country_name': 'United States of America',
  'vhr_nationality': '',
  'vhr_short_country_name': '',
  'vhr_transaction_currency_code': 'USD'},

这是我从数据库中获取的数据样本。 如何在单个语句中找到索引?

例如,如果第三个条目的国籍被更新,我需要这样的东西:

arrrSample[index]['vhr_nationality'] ="American"

如何找到索引?我有 'pk_ba_country_id': 184 的价值 这是arrrSample[2]['pk_ba_country_id'] =184

如何在不循环的情况下从 184 中找到这个索引 2?

我需要从值中找到索引,我不希望使用循环数组,因为数据库可以包含大量数据。

【问题讨论】:

  • 您定义的不是多维数组。您将多个字典分配给一个变量,(我认为)这将导致一个元组。在任何人可以帮助您之前,您可能需要解释您在做什么。有关如何在字典列表中定义和搜索的示例,请参阅 Python list of dictionaries search
  • 你没有多维数组,因为它在 Python 中通常被理解为一个 numpy.ndarray,或者 也许 一个嵌套列表。您的示例“数组”不是数组,它会给出语法错误。假设您有一个字典列表,那么没有循环就无法找到该索引,即使它至少需要一次构建从感兴趣值到索引的映射。

标签: python


【解决方案1】:

我假设您的目标是这样的(arrrSample 格式不正确)?

arrrSample =[{'art_other_country_names': None,'pk_ba_country_id': 186,
  'sin_active': 1,
  'txt_remarks': '',
  'vhr_common_country_name': 'Uzbekistan',
  'vhr_formal_country_name': 'Republic of Uzbekistan',
  'vhr_nationality': '',
  'vhr_short_country_name': '',
  'vhr_transaction_currency_code': 'UZS'},
 {'art_other_country_names': None,
  'pk_ba_country_id': 185,
  'sin_active': 1,
  'txt_remarks': '',
  'vhr_common_country_name': 'Uruguay',
  'vhr_formal_country_name': 'Oriental Republic of Uruguay',
  'vhr_nationality': '',
  'vhr_short_country_name': '',
  'vhr_transaction_currency_code': 'UYU'},
 {'art_other_country_names': None,
  'pk_ba_country_id': 184,
  'sin_active': 1,
  'txt_remarks': '',
  'vhr_common_country_name': 'United States',
  'vhr_formal_country_name': 'United States of America',
  'vhr_nationality': '',
  'vhr_short_country_name': '',
  'vhr_transaction_currency_code': 'USD'}]

for data in arrrSample:
     if data['pk_ba_country_id'] == 184:
        print(data['vhr_common_country_name'])

【讨论】:

    猜你喜欢
    • 2014-05-21
    • 2016-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-15
    • 1970-01-01
    • 1970-01-01
    • 2013-07-06
    相关资源
    最近更新 更多