小编V A*_*non的帖子

检查字符串的字母是否在另一个字符串中按顺序排列

如果它只是检查test_string中的字母是否也在control_string中,

我不会遇到这个问题.

我将简单地使用下面的代码.

if set(test_string.lower()) <= set(control_string.lower()):
    return True
Run Code Online (Sandbox Code Playgroud)

但我也面临着一个相当复杂的任务,即辨别是否重叠的字母

control_string与test_string中的顺序相同.

例如,

test_string = 'Dih'
control_string = 'Danish'
True

test_string = 'Tbl'
control_string = 'Bottle'
False
Run Code Online (Sandbox Code Playgroud)

我想过使用for迭代器来比较字母表的索引,但是很难想到合适的算法.

for i in test_string.lower():
    for j in control_string.lower():
        if i==j:
            index_factor = control_string.index(j)
Run Code Online (Sandbox Code Playgroud)

我的计划是将主要指标因子与下一个因子进行比较,如果主要指数因子大于另一个,则该函数返回False.

我被困在如何比较for循环中的那些index_factors.

我该如何处理这个问题?

python string for-loop python-2.7 python-3.x

8
推荐指数
2
解决办法
199
查看次数

查找嵌套列表中元素的索引

我正在尝试创建一个程序,该程序将查找并存储嵌套列表中每个元素的索引。

到目前为止,我已经尝试使用嵌套的迭代器来实现这一点。

下面是我的代码。

table = [[1, 1, 1], [2, 2, 2], [3, 3, 3]]
def coordinates(table):
    target_cell_list = []
    for row in table:
        for column in row:
            target_cell = (table.index(row), row.index(column))
            target_cell_list.append(target_cell)
    return target_cell_list

>>> table = [[1, 1, 1], [2, 2, 3], [2, 3, 3]]
>>> coordinates(table)
# current output
[(0, 0), (0, 0), (0, 0), (1, 0), (1, 0), (1, 2), (2, 0), (2, 1), (2, 1)]

# desired output
[(0, 0), (0, 1), (0, 2), (1, 0), …
Run Code Online (Sandbox Code Playgroud)

python python-2.7 python-3.x

4
推荐指数
1
解决办法
1219
查看次数

标签 统计

python ×2

python-2.7 ×2

python-3.x ×2

for-loop ×1

string ×1