Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/366.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我正在尝试按降序排列链表_Python - Fatal编程技术网

Python 我正在尝试按降序排列链表

Python 我正在尝试按降序排列链表,python,Python,您能帮助我吗?我正在尝试根据add函数中的文档对链接列表进行排序,但我收到一个错误,它显示: f、 加上('b',2) 文件“”,第69行,添加 AttributeError:“非类型”对象没有属性“下一个” 我怎样才能避免这种情况? 谢谢 class Frequency(object): """ Stores a letter:frequency pair. >>> f = Frequency('c', 2) >>> f.

您能帮助我吗?我正在尝试根据add函数中的文档对链接列表进行排序,但我收到一个错误,它显示: f、 加上('b',2) 文件“”,第69行,添加 AttributeError:“非类型”对象没有属性“下一个” 我怎样才能避免这种情况? 谢谢

class Frequency(object):
    """

    Stores a letter:frequency pair.

    >>> f = Frequency('c', 2)
    >>> f.letter
    'c'
    >>> f.frequency
    2
    >>> f
    {c: 2}
    """
    def __init__(self, letter, frequency):
        self.letter = letter
        self.frequency = frequency
        self.next = None

    def __repr__(self):
        return '{%s: %d}' % (self.letter, self.frequency)

class SortedFrequencyList(object):
    """
    Stores a collection of Frequency objects as a sorted linked list.
    Items are sorted from the highest frequency to the lowest.
    """
    def __init__(self):
        self.head = None

    def add(self, letter, frequency):
        """
        Adds the given `letter`:`frequency` combination as a Frequency object
        to the list. If the given `letter` is already in the list, the given
        `frequency` is added to its frequency.

        >>> f = SortedFrequencyList()
        >>> f.add('a', 3)
        >>> f
        ({a: 3})
        >>> f.add('b', 2)
        >>> f
            ({a: 3}, {b: 2})
        >>> f.add('c', 4)
        >>> f
        ({c: 4}, {a: 3}, {b: 2})
        >>> f.add('b', 3)
        >>> f
        ({b: 5}, {c: 4}, {a: 3})
        """

        current = self.head
        found = False
        if self.head is None:
            self.head = Frequency(letter, frequency)
        else:
            prev = None
            while current is not None:
                if current.letter == letter:
                    current.frequency = current.frequency + frequency
                    found = True
                prev = current
                current = current.next

                next1 = current.next
                if next1 is None:
                    current = next1

                if current.frequency < next1.frequency:
                    temp = current
                    current = next1
                    next1 = temp
                else:
                    current = next1
                    next1 = current.next.next


            if found is False:
                prev.next = Frequency(letter, frequency)
类频率(对象):
"""
存储一个字母:频率对。
>>>f=频率('c',2)
>>>f.信
“c”
>>>频率
2.
>>>f
{c:2}
"""
定义初始(自我、字母、频率):
self.letter=字母
self.frequency=频率
self.next=无
定义报告(自我):
返回“{%s:%d}%”(self.letter,self.frequency)
类别分类频率列表(对象):
"""
将频率对象的集合存储为已排序的链表。
项目从最高频率到最低频率排序。
"""
定义初始化(自):
self.head=无
def添加(自我、字母、频率):
"""
将给定的“字母”:“频率”组合添加为频率对象
如果给定的“字母”已在列表中,则给定的
`频率`添加到其频率中。
>>>f=分类频率列表()
>>>f.添加('a',3)
>>>f
({a:3})
>>>f.添加('b',2)
>>>f
({a:3},{b:2})
>>>f.添加('c',4)
>>>f
({c:4},{a:3},{b:2})
>>>f.添加('b',3)
>>>f
({b:5},{c:4},{a:3})
"""
电流=自身磁头
发现=错误
如果self.head为无:
self.head=频率(字母、频率)
其他:
prev=无
虽然当前值不是“无”:
如果current.letter==字母:
current.frequency=current.frequency+频率
找到=真
prev=当前
当前=当前。下一步
next1=当前。下一个
如果next1为无:
电流=next1
如果current.frequency
在行中

current = current.next
next1 = current.next
如果
current.next==None
,会发生什么情况


我不知道你是在做Python练习,还是因为你真的需要这个功能;如果是后者,则已经有一个内置类为您执行此操作。它是(在Python2.7或3.x中);如果您使用的是早期版本,那么您可以通过子类化
collections.defaultdict
自己创建一个。它还使用Python字典,而不是将数据存储为键值对

例如:

>>> from collections import Counter
>>> x = Counter()
>>> x['a'] += 2
>>> x['b'] += 3
>>> x['c'] += 1
>>> x
Counter({'b': 3, 'a': 2, 'c': 1})
可以使用恢复数据的已排序键值对表示形式

x.most_common()

我想这就是错误发生的地方,但我不知道如何纠正。。我是否应该有这样一个语句:如果next1为None:将当前的一个作为最后一个。我该如何实现这一点?@Nirali:首先要弄清楚你想在伪代码中做什么。带有正方形和箭头的图表可以帮助我解决这种指针操作。(例如,如果我们已经到达列表的末尾,那么
current.next==None
,使
current.next==Frequency(字母,频率)