簡體   English   中英

字典列表中的唯一元素有效地

[英]Unique elements in a list of dictionary efficiently

我想根據字段的值從字典列表中獲取唯一元素並保留其他字段。

以下是我擁有的數據格式。

[ {id:"1000", text: "abc", time_stamp: "10:30"},
  {id:"1001", text: "abc", time_stamp: "10:31"},
  {id:"1002", text: "bcd", time_stamp: "10:32"} ]

我想要如下輸出:(基於文本唯一但保留其他字段)

[ {id:"1000", text: "abc", time_stamp: "10:30"}, # earlier time stamp
  {id:"1002", text: "bcd", time_stamp: "10:32"} ]

這里請注意唯一性是基於文本的,我想保留 id 和 time_stamp 值。 這個問題不同於之前問過的Python - List of unique dictionaries問題。

我試過:

方法一:僅從字典中收集文本值,將其轉換為列表,將其傳遞給集合,並獲取唯一的文本值,但我丟失了 id 和時間戳。

方法2:我也試過了,我遍歷了字典的列表並檢查了文本值是否存在於unique_list_of_text中,如果沒有附加到list_of_unique_dictionary。 但是這段代碼花費了很多時間,因為我正在處理一個包含 350,000 條記錄的數據集。 有沒有更好的方法來做到這一點? 方法 2 的代碼:

def find_unique_elements(list_of_elements):
    no_of_elements = len(list_of_elements)
        unique_list_of_text = []
        unique_list_of_elements = []
        for iterator in range(0, no_of_elements):
            if not list_of_elements[iterator]['text'] in unique_list_of_text:
                unique_list_of_full_text.append(list_of_elements[iterator]['text'])
                unique_list_of_elements.append(list_of_elements[iterator])
        return unique_list_of_elements

您可以創建一個新list並檢查該項目是否存在,

為了讓它更快一點,我可能會使用更好的數據結構

$ cat unique.py

id = 'id'
text = 'text'
time_stamp = 'time_stamp'

data = [ {id:"1000", text: "abc", time_stamp: "10:30"},
   {id:"1001", text: "abc", time_stamp: "10:31"},
   {id:"1002", text: "bcd", time_stamp: "10:32"} ]

keys = set()
unique_items = []
for item in data:
    if item['text'] not in keys:
        unique_items.append(item)
    keys.add(item['text'])

print(unique_items)

$ python data.py 
[{'text': 'abc', 'id': '1000', 'time_stamp': '10:30'}, {'text': 'bcd', 'id': '1002', 'time_stamp': '10:32'}]

您可以從反向列表創建字典並從該字典中獲取值:

id, text, time_stamp = 'id', 'text', 'timestamp'

l = [ {id:"1000", text: "abc", time_stamp: "10:30"},
  {id:"1001", text: "abc", time_stamp: "10:31"},
  {id:"1002", text: "bcd", time_stamp: "10:32"} ]

d = {i[text]: i for i in reversed(l)}
new_l = list(d.values())
print(new_l)
# [{'id': '1002', 'text': 'bcd', 'timestamp': '10:32'}, {'id': '1000', 'text': 'abc', 'timestamp': '10:30'}]

# if the order should be preserved
new_l.reverse()
print(new_l)
# [{'id': '1000', 'text': 'abc', 'timestamp': '10:30'}, {'id': '1002', 'text': 'bcd', 'timestamp': '10:32'}]

如果最終列表中的順序OrderedDict ,請在 Python 3.6 及更低版本中使用OrderedDict而不是dict

暫無
暫無

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

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