String 传递一个字符串并将其与列表进行比较

String 传递一个字符串并将其与列表进行比较,string,String,我正在通过机器人框架传递一个论点。参数是一个字符串。底特律。 我希望代码将该字符串分解为D、De、Det、Detr、Detro、Detroi和Detroit。当然,如果输入另一个字符串,比如Flint,它只会将其分解为5个元素。F、 弗林特,弗林特,弗林特 伪码 def checkCity (self, x): (take x which is the string, and make it a list of elements containing the letters as ab

我正在通过机器人框架传递一个论点。参数是一个字符串。底特律。 我希望代码将该字符串分解为D、De、Det、Detr、Detro、Detroi和Detroit。当然,如果输入另一个字符串,比如Flint,它只会将其分解为5个元素。F、 弗林特,弗林特,弗林特

伪码

def checkCity (self, x):
     (take x which is the string, and make it a list of elements containing the letters as above).
     (Then take each element and check it against data provided by the device(using a loop for each iteration)
     (Once any of the elements are matched to the data, return another function that acts as a key press)

一般来说,我对python和编程都很熟悉,有了这个理论,只是不知道如何编写代码。

我不熟悉您正在使用的编程语言,但我会尽我所能提供帮助

为了分解字符串,您可以使用while循环或For循环,以您喜欢的为准。结束条件是放入第二个参数的字符串的长度。在循环中,可以使用substring方法分解字符串并将每个元素存储到数组列表中


然后,为了检查是否有任何元素匹配,正如您所说的,在每次迭代中使用循环。

在python中,您可以使用

string[5:7]
这将给出第5和第6个字符

python中的这个函数将返回一个与您想要的列表类似的列表

def toSubLists(string):
    sublists = []
    for i in range(1, len(string)+1):
        sublists.append(string[0:i])
    return sublists

这应该是一个简单的循环。。。你能展示一下你已经尝试过的吗?