简体   繁体   English

试图找到元素较少的列表

[英]Trying to find the list with fewer elements

I'm quite new with Python and programming in general.总的来说,我对 Python 和编程很陌生。 My problem concerns the operations through which I could find the list with the fewest elements in a dictionary.我的问题涉及我可以通过哪些操作找到字典中元素最少的列表。 To be clear I have a dictionary with about ten keys, and each key is a list with a lot of elements.明确地说,我有一个大约有十个键的字典,每个键都是一个包含很多元素的列表。 I need to iterate over the list with the fewest elements.我需要用最少的元素迭代列表。 To find it I tried to define a function that do this work:为了找到它,我尝试定义一个执行此工作的函数:

def minlist(*lists):
    smallest = min(len(lists))
    if len(lists) == smallest:
        return lists

But the response was TypeError: 'int' object is not iterable .但响应是TypeError: 'int' object is not iterable How can I manage that, taking in mind that in principle I don't know the numbers of keys?考虑到原则上我不知道密钥的数量,我该如何管理? Here a sample of my dictionary (as required)这是我的字典样本(根据需要)

{97: [1007928679693166,
      1007928798219684,
      1007928814680980,
      1007928891466688,
      1007928897515544,
      1007928997487142],
 98: [1007928837651593, 1007928889730933],
 99: [1007928797944536,
      1007928805518205,
      1007928870847877,
      1007929012532919,
      1007929030905896,
      1007929097107140],
 688: [1007928628309796,
       1007928724910684,
       1007928808626541,
       1007928866265101,
       1007928908312998,
       1007928982161920,
       1007929013746703,
       1007929055652413],
 734: [1007928687611100,
       1007928923969018,
       1007928933749030,
       1007928942892766,
       1007929021773704],
 1764: [1007928765771998, 1007928917743164],
 1765: [1007928894040229, 1007929021413611],
 1773: [1007929003959617]}

Here is a solution using a intermediate list of tuples for easy sort/access:这是一个使用中间元组列表以便于排序/访问的解决方案:

input_dict = {1: [1,2,3,4], 2: [2,3,4], 3:[1,2,3]}
#Get key/length(list) type tuples
helper = [(key, len(input_dict[key])) for key in input_dict.keys()]
#Sort list by the second element of the tuple(the length of the list) 
helper.sort(key=lambda x: x[1])

#Now the first position hold the key to the shortest list from the dicitonary and the length
print input_dict[helper[0][0]]

here is an even shorter version using list comprehension :这是使用列表理解的更短版本:

min_list=min([len(ls) for ls in dict.values()])

edit : this is also usable using generator comprehensions (surround the expresion in round brackets instead of square ones) for a more efficient version编辑:这也可以使用生成器推导式(将表达式括在圆括号中而不是方括号中)以获得更有效的版本

I suppose you wanna do this:我想你想这样做:

def minlist(lists_dict):
  min_list = None
  for list in lists_dict.values():
    if min_list == None: 
      min_list = list
    else:
      if len(list) < len(min_list):
        min_list = list

    return min_list

Why lists_dict.values() ?为什么lists_dict.values() By default you iterate over the keys of the dictionary.默认情况下,您遍历字典的键。 But you wanna check the length of the associated values => therefore you have to use them.但是您想检查关联值的长度 => 因此您必须使用它们。

The structure of the dictionary I assumed looks like this:我假设的字典结构如下所示:

# { int: list, int: list, ...}
# e.g.:
lists_dict = {1: [2,3], 2: [2,3,4,5], 3: [1], 4: [1,2,2]}

The structure you've described:您描述的结构:

# { list: list, list: list, ...}

wouldn't work, you can't use standard list as key for an dictionary.行不通,您不能使用标准列表作为字典的键。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM