我有下面的字典
test = [ { 'id': '195', 'Name': 'i', 'Email': '[email protected]', 'role': 'Product' },
{ 'id': '219', 'Name': 'umar', 'Email': '[email protected]', 'role': 'Product' },
{ 'id': '74', 'Name': 'Are', 'Email': '[email protected]', 'role': 'Tester' },
{ 'id': '24', 'Name': 'Mee', 'Email': '[email protected]', 'role': 'Tester' },
{ 'id': '230', 'Name': 'abc', 'Email': '[email protected]', 'role': 'Tester' },
{ 'id': '220', 'Name': 'Sc', 'Email': '[email protected]', 'role': 'Product' },
{ 'id': '230', 'Name': 'Sn', 'Email': '[email protected]', 'role': 'Tester' } ]
- 我需要從上面的串列字典中提取唯一的電子郵件
- 我需要給予角色偏好
Product然后Tester
我的代碼如下
dict([(d['Email'], d) for d in test]).values()
我的出局:
dict_values([{'id': '195', 'Name': 'i', 'Email': '[email protected]', 'role': 'Product'},
{'id': '219', 'Name': 'umar', 'Email': '[email protected]', 'role': 'Product'},
{'id': '74', 'Name': 'Are', 'Email': '[email protected]', 'role': 'Tester'},
{'id': '24', 'Name': 'Mee', 'Email': '[email protected]', 'role': 'Tester'},
{'id': '230', 'Name': 'Sn', 'Email': '[email protected]', 'role': 'Tester'}])
在我的外面
{'id': '230', 'Name': 'Sn', 'Email': '[email protected]', 'role': 'Tester'}
必須替換為
{ 'id': '220', 'Name': 'Sc', 'Email': '[email protected]', 'role': 'Product' }
因為“產品”有更高的偏好。
如何更新我的代碼?dict([(d['Email'], d) for d in test]).values()
uj5u.com熱心網友回復:
如果您想堅持使用字典,可以這樣做。我們從一排走到另一排。檢查電子郵件是否已經在新字典中作為鍵。
- 如果沒有,我們將其添加為新的。
- 如果是這樣,我們檢查我們的新行。如果我們的新角色是“產品”,我們將洗掉字典中已有的內容,并添加新行。
new_dict = {}
for row in test:
if row["Email"] not in new_dict.keys():
new_dict.update({row["Email"]: row})
else:
if row["role"]=="Product":
new_dict.pop(row["Email"])
new_dict.update({row["Email"]: row})
uj5u.com熱心網友回復:
也許您可以嘗試兩個回圈;一次是為了獲得獨特的電子郵件,第二次是為了確保優先考慮“產品”。
不清楚如果重復的“電子郵件”沒有“產品”會發生什么,因此在下面的回圈中,在這種情況下選擇了第一封電子郵件。
tmp = {}
for d in test:
tmp.setdefault(d['Email'], []).append(d)
out = []
for k, lst in tmp.items():
if len(lst) == 1:
out.append(lst[0])
else:
for d in lst:
if d['role'] == 'Product':
out.append(d)
break
else:
out.append(lst[0])
輸出:
[{'id': '195', 'Name': 'i', 'Email': '[email protected]', 'Account': 'Product'},
{'id': '219', 'Name': 'umar', 'Email': '[email protected]', 'Account': 'Product'},
{'id': '74', 'Name': 'Are', 'Email': '[email protected]', 'role': 'Tester'},
{'id': '24', 'Name': 'Mee', 'Email': '[email protected]', 'role': 'Tester'},
{'id': '220', 'Name': 'Sc', 'Email': '[email protected]', 'role': 'Product'}]
uj5u.com熱心網友回復:
在對列進行排序后使其成為 adata frame和drop_duplicatesby 。Emailrole
test = [ { 'id': '195', 'Name': 'i', 'Email': '[email protected]', 'role': 'Product' },
{ 'id': '219', 'Name': 'umar', 'Email': '[email protected]', 'role': 'Product' },
{ 'id': '74', 'Name': 'Are', 'Email': '[email protected]', 'role': 'Tester' },
{ 'id': '24', 'Name': 'Mee', 'Email': '[email protected]', 'role': 'Tester' },
{ 'id': '230', 'Name': 'abc', 'Email': '[email protected]', 'role': 'Tester' },
{ 'id': '220', 'Name': 'Sc', 'Email': '[email protected]', 'role': 'Product' },
{ 'id': '230', 'Name': 'Sn', 'Email': '[email protected]', 'role': 'Tester' } ]
df = pd.DataFrame(test)
df1 = df.sort_values(by = ["Email", "role"], ascending = True)
res_df = df1.drop_duplicates(["Email"])
output_list = []
for i in res_df.values :
output_list.append(dict([("id", i[0]), ("Name", i[1]), ("Email", i[2]), ("role", i[3])]))
> output_list
[{'id': '195', 'Name': 'i', 'Email': '[email protected]', 'role': 'Product'},
{'id': '219', 'Name': 'umar', 'Email': '[email protected]', 'role': 'Product'},
{'id': '74', 'Name': 'Are', 'Email': '[email protected]', 'role': 'Tester'},
{'id': '220', 'Name': 'Sc', 'Email': '[email protected]', 'role': 'Product'},
{'id': '24', 'Name': 'Mee', 'Email': '[email protected]', 'role': 'Tester'}]
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/431517.html
