簡體   English   中英

我們如何使用Python中的參數迭代字典的值?

[英]How can we iterate over values of a Dictionary using a parameter in Python?

我有如下的字典元組(對於value ,我定義了許多參數)

 [('keyA', {'site': 'Site1', 'machine': 'null', 'state': 'unprocessed', 'date_populated': '2017-09-27 04:52:48.357235', 'date_accessed': '2017-09-27 04:52:48.357242'})]

當我從字典中選擇一個值(我有元組的數量)時,我不知道鍵。 我想使用**Value**字段中定義的"**state**"參數來選擇一個項目。

例如,我想選擇一個包含**state**作為'**unprocessed**' ,選擇該項目后,我將更新**state**參數。(例如:unprocessed->已處理)如何在Python中執行此操作?

ps:我想首先為包含“狀態”參數的元組迭代字典(請注意,我在談論python字典數據結構)。

我的樣本字典:

self.cacheDictionary = {}
print self.cacheDictionary.items(); print

[('key4', {'site': 'Site4', 'machine': 'null', 'state': 'unprocessed', 'date_populated': '2017-09-27 05:58:08.451829', 'date_accessed': '2017-09-27 05:58:08.451832'}), ('key3', {'site': 'Site3', 'machine': 'null', 'state': 'unprocessed', 'date_populated': '2017-09-27 05:58:08.451522', 'date_accessed': '2017-09-27 05:58:08.451527'}), ('key2', {'site': 'Site2', 'machine': 'null', 'state': 'unprocessed', 'date_populated': '2017-09-27 05:58:08.451294', 'date_accessed': '2017-09-27 05:58:08.451297'}), ('key1', {'site': 'Site1', 'machine': 'null', 'state': 'unprocessed', 'date_populated': '2017-09-27 05:58:08.451139', 'date_accessed': '2017-09-27 05:58:08.451145'})]

print self.cacheDictionary  

給出輸出:

{'key4': {'site': 'Site4', 'machine': 'null', 'state': 'unprocessed', 'date_populated': '2017-09-27 06:09:33.485721', 'date_accessed': '2017-09-27 06:09:33.485724'}, 'key3': {'site': 'Site3', 'machine': 'null', 'state': 'unprocessed', 'date_populated': '2017-09-27 06:09:33.485425', 'date_accessed': '2017-09-27 06:09:33.485427'}, 'key2': {'site': 'Site2', 'machine': 'null', 'state': 'unprocessed', 'date_populated': '2017-09-27 06:09:33.485260', 'date_accessed': '2017-09-27 06:09:33.485264'}, 'key1': {'site': 'Site1', 'machine': 'null', 'state': 'unprocessed', 'date_populated': '2017-09-27 06:09:33.485124', 'date_accessed': '2017-09-27 06:09:33.485131'}}

你可以做這樣的事情

a=[('keyA', {'site': 'Site1', 'machine': 'null', 'state': 'unprocessed', 'date_populated': '2017-09-27 04:52:48.357235', 'date_accessed': '2017-09-27 04:52:48.357242'})]

首先,從包含元組的列表中提取字典

data_dict=a[0][1]

print data_dict

for key,value in data_dict.items():
    if key=='state' and value=='unprocessed':
        data_dict[key]='processed' # changing the state


print data_dict

如果將給定的表達式分配給變量a,則“狀態”將更新如下:

a[0][1].update({'state': 'processed'})

得到您的完整答復后更新了代碼。

for key, value in a.items(): 
    if value.has_key('state') and value['state'] == 'unprocessed': 
        temp = {key: value} 
        break

我的詞典中有很多元組,有些元組包含'state':'unprocessed',有些元組包含'state':'processed'。 我想使用狀態參數來迭代該字典,並選擇包含“狀態”為“未處理”的第一個元組

根據您的評論進行編輯,如果您只需要更改第一個字典,則使用next獲取第一個項目:

使用運算符

import operator

list_of_items = [('keyA',
  {'date_accessed': '2017-09-27 04:52:48.357242',
   'date_populated': '2017-09-27 04:52:48.357235',
   'machine': 'null',
   'site': 'Site1',
   'state': 'unprocessed'})]

# get first item in list that has the `state` == `unprocessed`
# raises StopIteration if item is not found, use a default value if you don't want this.

first_item = next(
    i for i in map(operator.itemgetter(1), list_of_items)
    if i.get('state') == 'unprocessed'
)

first_item['state'] = 'processed'

那應該只更改匹配的第一項:

print(list_of_items)

[('keyA',
  {'date_accessed': '2017-09-27 04:52:48.357242',
   'date_populated': '2017-09-27 04:52:48.357235',
   'machine': 'null',
   'site': 'Site1',
   'state': 'processed'})]

如果要更改列表中的所有項目:

for items in map(operator.itemgetter(1), list_of_items):
    item = items.get('state')
    if item == 'unprocessed':
        items['state'] = 'processed'

正如@puspendra提出的解決方案一樣,這確實是有效的,我也將提出相同的解決方案,如果您願意,也可以嘗試以下解決方案:

data=[('keyA', {'site': 'Site1', 'machine': 'null', 'state': 'unprocessed', 'date_populated': '2017-09-27 04:52:48.357235', 'date_accessed': '2017-09-27 04:52:48.357242'})]

retrive=data[0][1]

for i in retrive.keys():
    if retrive[i]=="unprocessed":
        retrive[i] = "processed"


print(retrive)

更新資料

在粘貼了帶有更新的新列表之后,這是我的新列表代碼:

data=[('key4', {'site': 'Site4', 'machine': 'null', 'state': 'unprocessed', 'date_populated': '2017-09-27 05:58:08.451829', 'date_accessed': '2017-09-27 05:58:08.451832'}), ('key3', {'site': 'Site3', 'machine': 'null', 'state': 'unprocessed', 'date_populated': '2017-09-27 05:58:08.451522', 'date_accessed': '2017-09-27 05:58:08.451527'}), ('key2', {'site': 'Site2', 'machine': 'null', 'state': 'unprocessed', 'date_populated': '2017-09-27 05:58:08.451294', 'date_accessed': '2017-09-27 05:58:08.451297'}), ('key1', {'site': 'Site1', 'machine': 'null', 'state': 'unprocessed', 'date_populated': '2017-09-27 05:58:08.451139', 'date_accessed': '2017-09-27 05:58:08.451145'})]

for tuple_a in data:
    for dic_a in tuple_a:
        if isinstance(dic_a,dict):
            for keys in dic_a.keys():
                if dic_a[keys]=='unprocessed':
                    dic_a[keys]="processed"



            print(dic_a)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM