简体   繁体   English

如何从 Python 的链表中的节点中提取数据?

[英]How do I extract data from a node in a linked list in Python?

How do you access data in a linked list?如何访问链表中的数据? Mainly, I have two specific questions.主要是,我有两个具体的问题。 I'm a beginner in python and just learning linked lists so bear with me if this seems like a stupid question.我是 python 的初学者,只是学习链表,如果这看起来像一个愚蠢的问题,请耐心等待。

  1. I have a linked list of nodes where each node is an instance of a class.我有一个节点链接列表,其中每个节点都是 class 的一个实例。
    How do I retrieve the node in the linked list with a certain instance attribute = x?如何检索具有某个实例属性 = x 的链表中的节点?

  2. I have a linked list of nodes where each node is a dictionary.我有一个节点链接列表,其中每个节点都是一个字典。 How do I search for a node in the linked list with a certain key in Python? Python中的某个key如何在链表中搜索节点?

Currently, I am unable to access the information in the node except for printing it using the str method.目前,除了使用str方法打印之外,我无法访问节点中的信息。

Usually you just need to define a recursive function.通常你只需要定义一个递归的 function。 Assuming your linked list is defined as follows:假设您的链表定义如下:

class Node:
    def __init__(self, val):
        self.val = val
        self.next = None

Now traversing the list and finding a node with a specific value is fairly easy:现在遍历列表并找到具有特定值的节点相当容易:

def find(node: Node, search_val):
    if not node:
        return None
    else:
        if node.val == search_val:
            return node
        else:
            return find(node.next, search_val)

Similarly you can modify this function to check whether node.val is a dictionary and whether it has a certain key.同样你可以修改这个function来检查node.val是否是一个字典,是否有某个key。

Question 2 is phrased in a strange way.问题 2 的措辞很奇怪。 Node cannot be a dictionary.节点不能是字典。 It has to be some form of Node class.它必须是某种形式的节点 class。 It can contain a dictionary, but it can't be one.它可以包含字典,但不能是字典。

Data in a linked list is accessed by traversing the list.通过遍历链表来访问链表中的数据。 You need to start at the head, check if the data it contains is what you're looking for, and traverse to the next node if it isn't.您需要从头开始,检查它包含的数据是否是您要查找的数据,如果不是,则遍历到下一个节点。

So, making some assumptions about your implementation of linked lists:因此,对链表的实现做出一些假设:

  1. You will need to start at the head node and traverse your list until you find the desired data attribute value.您将需要从头节点开始并遍历您的列表,直到找到所需的数据属性值。

You can do this with a function that returns None if the desired data attribute value isn't in the linked list.您可以使用 function 执行此操作,如果所需的数据属性值不在链接列表中,则返回 None。 I'll assume your linked list nodes have a method, next_node, which returns None if it's the tail instance.我假设您的链表节点有一个方法 next_node,如果它是尾部实例,则返回 None。

def traverse_to_desired_node(head : LinkedListNode, desired : Any):
    node = head
    while node.data != desired:
        node = node.next_node()
        if node is None:
            return None
    return node
  1. If each node in a linked list is a dictionary, that means your linked list is really a nested dict with each nested dict contained within a key, 'next' or something, and the tail has None as the value of 'next'.如果链表中的每个节点都是一个字典,这意味着您的链表实际上是一个嵌套字典,每个嵌套字典都包含在一个键、'next' 或其他内容中,并且尾部具有 None 作为 'next' 的值。

A similar approach will work:类似的方法将起作用:

def traverse_to_desired_node(head : dict, desired : str):
    node = head
    while desired not in node:
        node = node['next']
        if node is None:
            return None
    return node

Now, with either approach, you can set a variable to the node:现在,无论采用哪种方法,您都可以为节点设置一个变量:

node = traverse_to_node(head, 'desired')

Then, if it's a class instance, you can access the data attribute of that node:然后,如果它是 class 实例,您可以访问该节点的数据属性:

print(node.data)

Or, if it's a dict, access the desired key:或者,如果它是一个 dict,则访问所需的密钥:

print(node['desired'])

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

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