在Python中查找列表中列表的最高元素

时间:2014-11-10 19:08:38

标签: python algorithm data-structures

我试图创建一个方法来返回" bucket"其中包含最多的键。例如

如果我有一个包含此数据的列表列表

[['Smith', 17], ['Josh', 4], ['Steve', 19], ['Josh', 4], ['Josh', 4], ['Steve', 19]]

我的方法LargestNumberBucket()将返回3,因为键或值出现3次。 我的代码如下:

class hashTable:
    def __init__(self, size):
        self.size = size
        self.hash = []

    def insertItem(self, key):
        sum = 0 
        for x in key:
            sum = sum + ord(x)
        newSum = sum % self.size
        self.hash.append([key, newSum]) #Using append makes it to where it uses linear probing

    def printTable(self):
            print(self.hash)


    def search(self, key):
        for searchedKey in self.hash:
            if (searchedKey[0] == key):
                print("The record", searchedKey, "was found.")
                break
        else:
            print("The item was not found")

    def deleteItem(self, key):
        sum = 0 
        for x in key:
            sum = sum + ord(x)
        newSum = sum % self.size

        self.hash.remove([key, newSum])

    def numberOfRecords(self):
        x = 0 
        while (x < len(self.hash)):
            x = x + 1
        print("The total number of records in the table are ", x)
#figure out how to count collisions and largest bucket  
    def numOfCollisions(self):
        for x in self.hash:
            y = self.hash.count(x)
        print("The number of collisions are", y)
#This does not work.
    def largestNumberBucket(self):
        print("The largest bucket contains",
              max(set(self.hash[1]), key=self.hash.count), "buckets")

2 个答案:

答案 0 :(得分:2)

您可以使用Counter执行此操作。让我们考虑一下这个简单的例子

使用zip,我们通过从同一位置选择它们来构建两个元组。

a,b=zip(*d)

输出:

a==> ('Smith', 'Josh', 'Steve', 'Josh', 'Josh', 'Steve') 
b==> (17, 4, 19, 4, 4, 19)

然后,使用计数器,我们找到项目的频率

Counter(a)

会给。

Counter({'Josh': 3, 'Steve': 2, 'Smith': 1})

然后我们只使用Counter(a).values()选择值:

[2, 3, 1]

并获得最大值

max(Counter(a).values())

我们对b做同样的事情,b是第二个位置的值列表。然后我们在两个结果中选择最大值

d= [['Smith', 17], ['Josh', 4], ['Steve', 19], ['Josh', 4], ['Josh', 4], ['Steve', 19]]

from collections import Counter
a,b=zip(*d)
print max(max(Counter(a).values()),max(Counter(b).values())) #Output:3

print max(Counter(a).values()+Counter(b).values()) #Output:3, as it's the max of the union

您的代码如下:

def largestNumberBucket(self):
  a,b=zip(*self.hash)
  #print("The largest bucket contains", max(max(Counter(a).values()),max(Counter(b).values())), "buckets")
  print("The largest bucket contains", max(Counter(a).values()+Counter(b).values()), "buckets")

<强>更新

根据@nneonneo的建议,我们可以使用Counter(a).most_common(1)[0][1]

获取最大值
def largestNumberBucket(self):
  a,b=zip(*self.hash)
  print("The largest bucket contains", max(Counter(a).most_common(1)[0][1],Counter(b).most_common(1)[0][1]), "buckets")

答案 1 :(得分:0)

我只使用了countmax

>>> a = [['Smith', 17], ['Josh', 4], ['Steve', 19], ['Josh', 4], ['Josh', 4], ['Steve', 19]] 
>>> max(a.count(x) for x in a)
3