如何为python列表的每个元素实现一个计数器?

时间:2021-10-12 00:18:29

Using Python 3.4

使用Python 3.4

I've got a way that works, but I think there might be a better way. I want to have a list with a method expand() which chooses a random element from the list, but every time that element is chosen, a counter is incremented. I tried subclassing str to be able to add attributes but it didn't work.

我有一种方法可行,但我认为可能有更好的方法。我希望有一个方法expand()的列表,它从列表中选择一个随机元素,但每次选择该元素时,计数器都会递增。我尝试继承str以便能够添加属性,但它不起作用。

My main problem with what I've got is that the expression random.randint(0,len(self)-1) and using a local variable doesn't seem very Pythonic. Before I added the counter, I could just type random.choice(self)

我得到的主要问题是表达式random.randint(0,len(self)-1)和使用局部变量似乎不是Pythonic。在我添加计数器之前,我可以输入random.choice(self)

class clauses(list):
    def __init__(self):
        self.uses = []

    def __setitem__(self,key,value):
        self.uses[key]=value
        super().__setitem__(self,key,value)

    def __delitem__(self,key):
        del(self.uses[key])
        super().__delitem__(key)

    def append(self,value):
        self.uses.append(0)
        super().append(value)

    def extend(self,sequence):
        for x in sequence:
            self.uses.append(0)
            super().append(x)

    def expand(self):

        n = random.randint(0,len(self)-1)

        self.uses[n] += 1
        return(self[n])

1 个解决方案

#1


Initializing an empty dictionary, along with your list should solve this, assuming there are no duplicate entries within the list.

假设列表中没有重复的条目,初始化空字典以及列表应解决此问题。

When adding an element to the list, you can also add it to the dictionary by myDict[element]=0 where myDict is the initialized dictionary, and element is the item being added to the list.

将元素添加到列表时,您还可以通过myDict [element] = 0将其添加到字典中,其中myDict是已初始化的字典,element是要添加到列表中的项目。

Then, when the item is selected, you can simply do: myDict[element]+=1.

然后,当选择该项时,您可以简单地执行:myDict [element] + = 1。

When dealing with an instance of duplicate entries, you could create a dictionary of dictionaries in which each key in the dictionary is a word, and the nested dictionary keys for each word are, say, index positions of the duplicate word (the values of course being the actual counts). This does add substantial complication, however, as when you remove an item from your list you will need to also update index positions. This nested data structure would like something like this though: { word1: {position1: count1}, word2: {position1: count1, position 2: count2}....}

处理重复条目的实例时,您可以创建字典字典,其中字典中的每个键都是一个单词,并且每个单词的嵌套字典键是,例如,重复单词的索引位置(当然的值)是真正的计数)。但是,这会增加实质性的复杂性,因为当您从列表中删除项目时,您还需要更新索引位置。这个嵌套的数据结构虽然如此:{word1:{position1:count1},word2:{position1:count1,position 2:count2} ....}

#1


Initializing an empty dictionary, along with your list should solve this, assuming there are no duplicate entries within the list.

假设列表中没有重复的条目,初始化空字典以及列表应解决此问题。

When adding an element to the list, you can also add it to the dictionary by myDict[element]=0 where myDict is the initialized dictionary, and element is the item being added to the list.

将元素添加到列表时,您还可以通过myDict [element] = 0将其添加到字典中,其中myDict是已初始化的字典,element是要添加到列表中的项目。

Then, when the item is selected, you can simply do: myDict[element]+=1.

然后,当选择该项时,您可以简单地执行:myDict [element] + = 1。

When dealing with an instance of duplicate entries, you could create a dictionary of dictionaries in which each key in the dictionary is a word, and the nested dictionary keys for each word are, say, index positions of the duplicate word (the values of course being the actual counts). This does add substantial complication, however, as when you remove an item from your list you will need to also update index positions. This nested data structure would like something like this though: { word1: {position1: count1}, word2: {position1: count1, position 2: count2}....}

处理重复条目的实例时,您可以创建字典字典,其中字典中的每个键都是一个单词,并且每个单词的嵌套字典键是,例如,重复单词的索引位置(当然的值)是真正的计数)。但是,这会增加实质性的复杂性,因为当您从列表中删除项目时,您还需要更新索引位置。这个嵌套的数据结构虽然如此:{word1:{position1:count1},word2:{position1:count1,position 2:count2} ....}