簡體   English   中英

在python中使用集合和列表來創建一個新列表

[英]Use sets and lists in python to create a new list

我有以下動態數據,但以下是它可能的示例:

# An incoming List of lists with only ints
# For exampe the incoming_list could be:
incoming_list = [[1,2]. [3]]

# The indexes are like so:
0: [1,2]
1: [3]

然后我有一個 check_list ,其中包含一些示例數據(將是動態的),如下所示:

# A List[int] for check_list
check_list= [3]

如果任何傳入列表數據在它的索引中,我然后需要獲取incoming_list 上的第一個int:

# If the following were input:
incoming_list = [[1,2]. [3]]
check_list= [3]

# Then a new_list would be:
new_list = [3] because incoming_list has a list with 3 in it, and the 
first element of that list is 3

##############################################################

# Another example ff the following were input:
incoming_list = [[1,2]. [3]]
check_list= [2,3]

# Then a new_list would be:
new_list = [1,3] because incoming_list has a 2 in the first index and 
its first value is 1 and because incoming list has a 3 in the second index 
and its first and only value is 3

我試圖用 set list 組合來做到這一點,但我認為 List 部分把它搞砸了:

new_list = list(set(v for k, v in incoming_lists if int(k) in check_list))

任何想法如何使這些干凈優雅?

雖然不是很清楚你的意思,但我認為這應該可以工作。

new_list = []
check = set(check_list)

for sublist in incoming_list:
    for i in sublist:
        if i in check and sublist[0] not in new_list:
            new_list.append(sublist[0])
            continue

print(new_list)

輸出

讓我們嘗試不同的incoming_listcheck_list值。

incoming_list = [[1, 2], [3]]
check_list = [2]

# Result: [1]
incoming_list = [[1, 2], [3], [2, 3, 5], [7, 8], [4, 3]]
check_list = [3]

# Result: [3, 2, 4]

在一般情況下,將列表預處理為O(1)查找結構可能是有意義的,該結構可以在線性時間內完成:

lookup = {}
for lst in reversed(incoming_list):
    for x in lst:
        lookup[x] = lst[0]

然后你可以簡單地使用這個:

result = [lookup[x] for x in check_list]

針對問題中的給定輸入實現所需結果的一種簡單方法。 如果您正在尋找 1 行語句。

[l[0] for v in check_list for l in incoming_list if v in l]

In [33]: # EXAMPLE 1                                                                                                                                                                                        

In [34]: incoming_list = [[1,2], [3]]                                                                                                                                                                       

In [35]: check_list= [3]                                                                                                                                                                                    

In [36]: [l[0] for v in check_list for l in incoming_list if v in l]                                                                                                                                        
Out[36]: [3]

In [37]: # EXAMPLE 2                                                                                                                                                                                        

In [38]: incoming_list = [[1,2], [3]]                                                                                                                                                                       

In [39]: check_list= [2,3]                                                                                                                                                                                  

In [40]: [l[0] for v in check_list for l in incoming_list if v in l]                                                                                                                                        
Out[40]: [1, 3]

In [41]:  

對於我們在最終結果中有重復項更改的情況,您可以更新單行語句。

暫無
暫無

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

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