首页 > 解决方案 > AttributeError:比较数据时,“NoneType”对象没有属性“数据”

问题描述

我有一个任务:“比较两个字符串并获取它们的位置和关键字的差异百分比”。我正在使用 python 3.9.5

这是我的代码:

#Create a Node
class Node:
    #Function to initialize a Node
    def __init__(self, data = None):
        self.data = data
        self.next = None

#Create a Singly Linked List
class SinglyLinkedList:
    #Function to initialize a Singly Linked List with a head node and a last node
    def __init__(self):
        self.head = None
        self.last_node = None

    #Function to add a new Node
    def Add_Node(self, data):
        if self.last_node is None:
            self.head = Node(data)
            self.last_node = self.head
        else:
            self.last_node.next = Node(data)
            self.last_node = self.last_node.next 
    
    #Function to get the length of Linked List
    def Len_List(self):
        temp = self.head 
        count = 0 
 
        while temp is not None:
            count += 1
            temp = temp.next
        return count

    #Function to print the Linked List
    def Display(self):
        current = self.head
        while current is not None:
            print(current.data, end = ' ')
            current = current.next
    
    #Function to remove a Node
    def Remove_Node(self, Removedata):
        temp_node = self.head

        if temp_node is not None:
            if temp_node.data == Removedata:
                self.temp_node = temp_node.next
                temp_node = None
                return
        
        while temp_node is not None:
            if temp_node.data == Removedata:
                break
            prev_node = temp_node
            temp_node = temp_node.next

        if temp_node == None:
            return

        prev_node.next = temp_node.next

        temp_node = None

    #Function to delete a Node with a given position
    def Delete_Node(self, position):

        if self.head == None:
            return

        temp_node = self.head

        if position == 0:
            self.head = temp_node.next
            temp_node = None
            return

        for i in range(position-1):
            temp_node = temp_node.next
            if temp_node == None:
                break

        if temp_node is None:
            return
        
        if temp_node.next is None:
            return

        next = temp_node.next.next

        temp_node.next = None

        temp_node.next = next


    #Compare the words of two Lists
    def Compare_Key(self, other):
        head1 = self.head
        head2 = other.head

        answer = 0

        if head1 == None or head2 == None:
            return 0

        temp = ''

        while head1 is not None and head2 is not None:
            if head2 is None:
                break
            while head2 is not None:
                if head1.data == head2.data:
                    answer += 1
                    temp = head1.data
                    other.Remove_Node(temp)
                    break
                head2 = head2.next
                head1 = head1.next
        
        return answer


    #Compare the words' position of two Lists
    def Compare_Position(self, other):
        head1 = self.head
        head2 = other.head

        answer = 0

        if head1 == None or head2 == None:
            return 0

        if self.Len_List() > other.Len_List():
            max = self.Len_List()
        else:
            max = other.Len_List()

        if head1 is not None and head2 is not None:
            for i in range(max):
                if head1.data == head2.data:
                    answer += 1
                head2 = head2.next
                head1 = head1.next

        return answer


#Main Program
print("THIS IS THE PROGRAM USED TO COMPARE TWO STRINGS UNPUTED FROM USERS BASE ON THEIR WORDS AND POSITIONS")

#Get data from users
input_string_1 = input('Enter the string 1: ').upper().split()
input_string_2 = input('Enter the string 2: ').upper().split()

#Pust data into Linked Lists
Linked_List_1 = SinglyLinkedList()
for i in range(len(input_string_1)):
    Linked_List_1.Add_Node(input_string_1[i])

Linked_List_2 = SinglyLinkedList()
for i in range(len(input_string_2)):
    Linked_List_2.Add_Node(input_string_2[i])


#Get percentage
if len(input_string_1) == len(input_string_2):
    total = len(input_string_1)
elif len(input_string_1) > len(input_string_2):
    total = len(input_string_1)
else:
    total = len(input_string_2)

#Percentage for position
position_percentage = (Linked_List_1.Compare_Position(Linked_List_2)/total)*100
print("The different percentage about position of two strings:", position_percentage)

#Percentage for words
keywords_percentage = (Linked_List_1.Compare_Key(Linked_List_2)/total)*100
print("The different percentage about words of two strings:", keywords_percentage)

它引发了这个错误:

line 139, in Compare_Position
    if head1.data == head2.data:

AttributeError: 'NoneType' object has no attribute 'data'

标签: pythonlinked-list

解决方案


我没有研究算法,但错误意味着要么head1or head2isNone在你执行的那一刻head1.data == head2.data

查看周围的代码时,我们有这样的:

    if head1 is not None and head2 is not None:
        for i in range(max):
            if head1.data == head2.data:
                answer += 1
            head2 = head2.next
            head1 = head1.next

尽管您首先检查了 bothhead1head2are not ,但在更改后并在内部循环中None不要重复该检查。因此,您确实需要将该检查放在内部循环中,这意味着交换该代码的前两行:head1head2

    for i in range(max):
        if head1 is not None and head2 is not None:
            if head1.data == head2.data:
                answer += 1
            head2 = head2.next
            head1 = head1.next

或者更好的是,在相反的情况下退出循环:

    for i in range(max):
        if head1 is None or head2 is None:
            break
        if head1.data == head2.data:
            answer += 1
        head2 = head2.next
        head1 = head1.next

推荐阅读