Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.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_Python 3.x_Linked List_Doubly Linked List - Fatal编程技术网

在双链表的字符串元素中查找字符索引,而不使用python中的类

在双链表的字符串元素中查找字符索引,而不使用python中的类,python,python-3.x,linked-list,doubly-linked-list,Python,Python 3.x,Linked List,Doubly Linked List,我正在编写一些有用函数的脚本来编辑双链接列表,我没有使用OOP。所以没有使用类 以下是双链接列表的示例: dll = ['So true', ['^very much', ['try it', ['Not yet', None, [...]], [...]], [...]], None] 注意,ddl元素的结构如下:[字符串,指向上一个节点的指针,指向下一个节点的指针] 我试图创建一个变量,该变量在双链接列表中将字符'^'的值作为“游标”(只是一个虚拟游标,但它实际上只是'^'字符的索引),而

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

以下是双链接列表的示例:

dll = ['So true', ['^very much', ['try it', ['Not yet', None, [...]], [...]], [...]], None]
注意,ddl元素的结构如下:[字符串,指向上一个节点的指针,指向下一个节点的指针]

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

我想要创建它的原因是使用它来创建有用的函数,例如:将光标向左移动一个字符,将光标移动到行的开头,…等等

游标=带字符串的双链接列表中“^”的索引


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

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

然后用
print(join\u nested\u strings(dll).index(“^”)
查找索引

编辑:

如果希望结果以元组(字符串、索引)的形式出现,递归函数如下所示:

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

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

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

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
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
如果运行上面的function builder,双链接列表(双向可编辑)将是:

dll = dll = ['A lot', ['Very ^much', ['Try it', ['Nop', None, [...]], [...]], [...]], None]
我昨天已经做了,但是今天早上我被困在解决光标问题上。过了一段时间,我才开始工作(终于!)。以下是创建光标作为指向节点(包含当前行)和该行内位置的指针的函数,如下所示:

# 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
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()
真正返回节点引用可能是个好主意。这样,您就可以通过
cursor()[0][2]
轻松访问上一个节点,通过
cursor()[0][1]
轻松访问下一个节点。