簡體   English   中英

比較列表並提取唯一值

[英]Comparing lists and extracting unique values

我有兩個清單:

l1:38510 個條目 l2:6384 個條目

我只想提取兩個列表中都存在的值。

到目前為止,這是我的方法:

equals = []

for quote in l2:
   for quote2 in l1:
      if quote == quote2:
         equals.append(quote)

len(equals)) = 4999
len(set(equals))) = 4452

首先,我覺得這種方法效率很低,因為我要檢查 l1 中的每個值幾次..

此外,似乎我仍然得到重復。 這是由於 l1 的內循環嗎?

謝謝!!

您可以使用list comprehensionin運算符。

a = [1, 2, 3, 4, 5, 6, 7, 8, 9]
b = [2, 4, 6, 8, 0]

[x for x in a if x in b]
#[2, 4, 6, 8]

通過使用集合,您走在了正確的軌道上。 set 最酷的功能之一是您可以獲得兩組之間的交集 交集是另一種表示出現在兩個集合中的值的方式。 您可以在文檔中了解更多信息

這是我的例子:

l1_set = set(l1)
l2_set = set(l2)

equals = l1_set & l2_set

#If you really want it as a list
equals = list(equals)

print(equals)

&運算符告訴 python 返回一個只有兩個集合中的值的新集合。 最后,我繼續將 equals 轉換回列表,因為這就是您最初的示例想要的。 如果你不需要它,你可以省略它。

1. 這是最簡單的方法,我們沒有使用任何內置函數。

# Two lists in most simple way of showing the intersection
def intersection(list_one, list_two):
    temp_list = [value for value in list_one if value in list_two]
    return temp_list
  
# Illustrate the intersection
list_one = [4, 9, 1, 17, 11, 26, 28, 54, 69]
list_two = [9, 9, 74, 21, 45, 11, 63, 28, 26]
print(intersection(list_one, list_two))

# [123, 3, 23, 15]

2. 可以使用 python set()方法。

# Two lists using set() method
def intersection(list_one, list_two):
    return list(set(list_one) & set(list_two))
  
# Illustrate the intersection
list_one = [15, 13, 123, 23, 31, 10, 3, 311, 738, 25, 124, 19]
list_two = [12, 14, 1,  15, 36, 123, 23, 3, 315, 87]
print(intersection(list_one, list_two))

# [123, 3, 23, 15]

3. 在這種技術中,我們可以使用內置的 function 調用intersection()來計算相交列表。

首先,我們需要使用set()來獲取更大的列表,然后計算交集。

# Two lists using set() and intersection()
def intersection_list(list_one, list_two):
    return list(set(list_one).intersection(list_two))
      
# Illustrate the intersection
list_one = [15, 13, 123, 23, 31, 10, 3, 311, 738, 25, 124, 19]
list_two = [12, 14, 1,  15, 36, 123, 23, 3, 315, 87, 978, 4, 13, 19, 20, 11]

if len(list_one) < len(list_two):
    list_one, list_two = list_two, list_one
    
print(intersection_list(list_one, list_two))

# [3, 13, 15, 19, 23, 123]

另外,您可以按照以下教程進行操作

  1. 極客們
  2. docs.python.org
  3. 快速學習編碼

假設您的兩個列表中的所有條目都是整數。 如果是這樣,計算兩個列表之間的交集將比使用列表推導更有效:

import timeit
l1 = [i for i in range(0, 38510)]
l2 = [i for i in range(0, 6384)]

st1 = timeit.default_timer()
# Using list comprehension
l3 = [i for i in l1 if i in l2]
ed1 = timeit.default_timer()

# Using set
st2 = timeit.default_timer()
l4 = list(set(l1) & set(l2))
ed2 = timeit.default_timer()

print(ed1-st1) # 5.7621682 secs
print(ed2-st2) # 0.004478600000000554 secs

由於您有這么長的列表,您可能想要使用numpy ,它專門為 Python 提供有效的列表處理。

您可以使用它的 numpy function 享受快速處理。 對於您的情況,您可以使用numpy.intersect1d()來獲取輸入 arrays 中的排序的唯一值,如下所示:

import numpy as np

l1 = [1, 3, 5, 10, 11, 12]
l2 = [2, 3, 4, 10, 12, 14, 16, 18]

l_uniques = np.intersect1d(l1, l2)


print(l_uniques)

[ 3 10 12]

您可以將結果列表保留為 numpy 數組,以便進一步快速處理或通過以下方式將其進一步轉換回 Python 列表:

l_uniques2 = l_uniques.tolist()

暫無
暫無

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

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