【问题标题】:Use sets and lists in python to create a new list在python中使用集合和列表创建一个新列表
【发布时间】:2021-11-04 07:29:31
【问题描述】:

我有以下动态数据,但以下是可能的示例:

# 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]

然后,如果任何传入列表数据在其索引中,我需要获取传入列表中的第一个 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

我试图用一组列表组合来做到这一点,但我认为 List of List 部分搞砸了:

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

有什么方法可以让这些干净优雅吗?

【问题讨论】:

  • “因为incoming_list 在第一个索引中有一个2,它的第一个值为1,并且因为传入列表在第二个索引中有一个3,它的第一个也是唯一的值是3”我实际上无法理解从这个描述来看,规则是什么。请尝试更清楚地沟通
  • 如果没有包含该值的列表怎么办,如果有多个怎么办。请提供一个涵盖所有可能情况的一般示例。

标签: python python-3.x python-3.7


【解决方案1】:

在一般情况下,将列表预处理为 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]

【讨论】:

  • 好奇为什么反转?
  • 如果一个值 x 出现在多个列表中,这可以确保我们选择第一次出现的列表(最后一个 dict 分配“获胜”)
【解决方案2】:

为问题中的给定输入实现所需结果的一种简单方法。如果您正在寻找 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]:  

对于我们在最终结果中有重复更改的情况,您可以更新单行语句。

【讨论】:

    【解决方案3】:

    虽然不是很清楚你的意思,但我认为这应该可以工作。

    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]
    

    【讨论】:

    • 您可以将 check_list 转换为一组以便更好地查找
    猜你喜欢
    • 2020-01-01
    • 2013-10-15
    • 2014-08-15
    • 2021-05-13
    • 2020-04-03
    • 1970-01-01
    • 2019-06-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多