【问题标题】:find character index in a string element of doubly linked list without using classes in python在不使用python中的类的情况下在双向链表的字符串元素中查找字符索引
【发布时间】:2019-04-13 08:44:38
【问题描述】:

我正在编写一些有用函数的脚本来编辑双向链表,但我没有使用 OOP。所以没有使用任何类。

这是一个双向链表的示例:

dll = ['So true', ['^very much', ['try it', ['Not yet', None, [...]], [...]], [...]], None]

注意ddl元素的结构是这样的:[字符串,指向前一个节点的指针,指向下一个节点的指针]

我正在尝试创建一个变量,该变量的值为双向链表中的字符 '^' 作为“光标”(只是一个虚拟光标,但它实际上只是 '^' 字符的索引),不使用类(一个函数,或者几个函数?)。 现在“cursor”变量是指向节点(包含当前字符串)和该字符串中的位置的指针。

我想创建它的原因是用它来创建有用的功能,例如:将光标向左移动一个字符,将光标移动到行首,......等等。

cursor = '^' 在带字符串的双向链表中的索引。

所以我的问题是:如何在不使用类的情况下找到双向链表中字符串中字符的索引?

【问题讨论】:

    标签: python python-3.x linked-list doubly-linked-list


    【解决方案1】:

    要仅仅找到^ 字符的位置,您可以使用一个简单的递归函数来连接所有字符串(我假设所有字符串都位于嵌套列表中的位置 0):

    def join_nested_strings(list):
        if list[1]:
            return list[0] + join_nested_strings(list[1])
        else:
            return list[0]
    

    然后简单地找到带有print(join_nested_strings(dll).index("^"))的索引。

    编辑:

    如果你想要一个元组(字符串,索引)形式的结果,递归函数应该是这样的:

    def cursor(list):
        if "^" in list[0]:
            return list[0], list[0].index("^")
        else:
            return cursor(list[1])
    

    请注意,如果光标字符不包含在任何字符串中,这个简单的函数将引发错误。

    【讨论】:

    • “光标”变量是指向节点(包含当前字符串)和该字符串中的位置的指针。 “光标”字符可以是双向链表中任意字符串的任意位置。
    【解决方案2】:

    首先,这是从元素列表中创建双链表的函数。 假设我们有一个字符串元素列表:

    testList =['A lot', 'Very ^much', 'Try it', 'Nop']
    

    现在要从testList 中构建双向链表,我们这样做如下:

    # Build node function
    def getNode(data):
        return [data, None, None]
    
    # Build the doubly linked list
    def construct_double_list(testList):
        nxt, prvs = 1, 2
        start, end = None, None
        for line in testList:
            next_node = getNode(line)
            if start is not None:
                end[nxt] = next_node
                next_node[prvs] = end
                end = next_node
            else:
                start = next_node
                end = next_node
        return start, end
    

    如果你运行上面的函数构建器,双向链表(双向迭代)将是:

    dll = dll = ['A lot', ['Very ^much', ['Try it', ['Nop', None, [...]], [...]], [...]], None]
    

    我昨天已经这样做了,但是今天早上我在尝试解决光标问题时遇到了困难。一段时间后,我刚刚开始工作(终于!)。这是创建光标作为指向节点(包含当前行)的指针的函数以及该行中的位置,如下所示:

    def cursor():
        cursor = (None, None)
        start, end = construct_double_list(testList)
        node = start
        while node is not None:
            line = node[0]
            if '^' in line:
                pointer1 = node
                pointer2 = line.index('^')
            node = node[1]
        cursor = (pointer1, pointer2)
        return cursor
    

    如果打印上面的cursor()函数:

    print(cursor())
    

    你得到:

    (['Very ^much', ['Try it', ['Nop', None, [...]], [...]], ['A lot', [...], None]], 5)
    # Node 1 and string index 5
    

    现在我可以创建一些有用的函数来编辑数据、修改、添加、删除、交换元素、在双向链表的字符串元素中交换字符……等等。

    【讨论】:

    • 该元组仅由一个字符串和一个数字组成。该字符串实际上并不指向或引用节点。重写代码以使cursor() 真正返回节点引用可能是个好主意。这样,您可以通过cursor()[0][2] 轻松访问上一个节点,通过cursor()[0][1] 访问下一个节点。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-20
    • 1970-01-01
    • 2020-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多