小编Mar*_*kF6的帖子

Startswith():更多参数

python中是否有一个函数来检查字符串是否以一定数量的可能性开头?例如,我想检查字符串A是否以“ Ha”,“ Ho”,“ Hi”开头。我以为可以使用string_A.startswith("Ha", "Ho", "Hi"),但是很遗憾,这是不可能的:(

感谢您的任何建议!:)

python string

2
推荐指数
1
解决办法
914
查看次数

三个字符串的最长公共子序列

我已经编写了这些函数(可以工作)来找到两个字符串中最长的公共子序列.

def lcs_grid(xs, ys):
    grid = defaultdict(lambda: defaultdict(lambda: (0,"")))
    for i,x in enumerate(xs):
        for j,y in enumerate(ys):
            if x == y:
                grid[i][j] = (grid[i-1][j-1][0]+1,'\\')
            else:
                if grid[i-1][j][0] > grid[i][j-1][0]:
                    grid[i][j] = (grid[i-1][j][0],'<')
                else:
                    grid[i][j] = (grid[i][j-1][0],'^')

    return grid

def lcs(xs,ys):
    grid = lcs_grid(xs,ys)
    i, j = len(xs) - 1, len(ys) - 1

    best = []
    length,move = grid[i][j]
    while length:
        if move == '\\':
            best.append(xs[i])
            i -= 1
            j -= 1
        elif move == '^':
            j -= 1
        elif move …
Run Code Online (Sandbox Code Playgroud)

python

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

Python:删除软连字符

在 html 文件中,我有包含软连字符的单词,例如

"Schilde rung"
repr(word) = "Schilde\\xc2\\xadrung"
Run Code Online (Sandbox Code Playgroud)

我怎样才能删除它们?

由于我的文件还包含变音符号和其他特殊字符,因此带有 printable 或 with 的解决方案words.decode('ascii', 'ignore')并不是很好......

我已经尝试过使用words.replace('\xc2\xad', ''); 但这没有用。

谢谢你的帮助 :)

html python

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

Python:按列表对列表中的列表进行数字排序

我有一个像这样的列表:

list1 = [['art_25', 'title', 'author'], ['art_12', 'title', 'author'], ['art_4', 'title', 'author']]
Run Code Online (Sandbox Code Playgroud)

如何对输出等于此列表进行排序

[['art_4', 'title', 'author'], ['art_12' ...], ['art_25', ...]] ?
Run Code Online (Sandbox Code Playgroud)

谢谢你的帮助!

python sorting

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

如何在一组字符串中找到所有常用字母?

我编写代码来查找3个给定字符串的所有常用字母.不幸的是,有一些我无法找到的错误.如果有人能告诉我这样的代码是什么样的,我会很感激.

谢谢你的帮助!

python

-1
推荐指数
1
解决办法
2631
查看次数

标签 统计

python ×5

html ×1

sorting ×1

string ×1