Python。多维数组的索引

时间:2018-05-07 12:14:17

标签: python

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?

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

1 个答案:

答案 0 :(得分:0)

我认为您的目标是这样的(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'])