小编vas*_*sor的帖子

获取包含字符串元素的列表,不包括前缀为初始列表中任何其他元素的元素

我在过滤字符串列表时遇到了一些麻烦.我在这里发现了类似的问题 但不是我需要的.

输入列表是:

l = ['ab', 'xc', 'abb', 'abed', 'sdfdg', 'abfdsdg', 'xccc']
Run Code Online (Sandbox Code Playgroud)

而预期的结果是

['ab', 'xc', 'sdfdg']
Run Code Online (Sandbox Code Playgroud)

结果中项目的顺序并不重要

过滤器功能必须快,因为列表的大小很大

我目前的解决方案是

l = ['ab', 'xc', 'abb', 'abed', 'sdfdg', 'abfdsdg', 'xccc']
for i in range(0, len(l) - 1):
    for j in range(i + 1, len(l)):
        if l[j].startswith(l[i]):
            l[j] = l[i]
        else:
            if l[i].startswith(l[j]):
                l[i] = l[j]

print list(set(l)) 
Run Code Online (Sandbox Code Playgroud)

编辑

在使用大输入数据进行多次测试后,列出1500000个字符串,我的最佳解决方案是:

def filter(l):
    if l==[]:
        return []
    l2=[]
    l2.append(l[0])
    llen = len(l)
    k=0
    itter = 0
    while k<llen:
        addkelem = ''
        j=0 …
Run Code Online (Sandbox Code Playgroud)

python string filtering list python-2.7

13
推荐指数
2
解决办法
586
查看次数

批处理文件:检查是否存在带有模式的文件

我有一个奇怪的情况,我不知道有什么问题

我需要检查目录中是否存在至少一个带模式的文件.

IF EXIST d:\*Backup*.* (
   ECHO "file exist"
) ELSE (
   ECHO "file not exist"
)
Run Code Online (Sandbox Code Playgroud)

如果在d:\我有一个文件x_Backup.txt和一个文件夹Backup我得到file exist但是如果我只有文件夹Backup我再次得到file exist,似乎路径中的点被忽略.

windows cmd path batch-file filter

6
推荐指数
3
解决办法
3万
查看次数

使用REGEXP进行MYSQL查询 - make-it非贪心

我需要一些关于MYSQL查询的正则表达式的帮助来搜索包含具有精确模式的单元格的行.我是MYSQL正则表达式的新手.

这是一个名为test_table的示例表(json_value是数组的json字符串)

|id |    json_value                                                                                 
-----------------------------------------------------------------------------------------------
| 1 | {"field_198":false,"field_4":"From quality office","field_9":"product with high quality","field_10":"comment"}    
| 2 | {"field_198":true,"field_4":"From ordering office","field_9":"back to quality office","field_10":"comment"}   
| 3 | {"field_198":true,"field_4":"From ordering office","field_9":"cancelled","field_10":"comment"}                    
| 4 | {"field_198":true,"field_4":"Return to quality office","field_9":"product ok","field_10":"comment"}
Run Code Online (Sandbox Code Playgroud)

如果我想获得所有行:

- field_4 containing "quality" string, the query should to return id 1 and 4
- field_9 containing "quality" string, the query should to return id 1 and 2
- field_4 containing "ordering" string, the query should to return id 2 and …
Run Code Online (Sandbox Code Playgroud)

regex mysql

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

标签 统计

batch-file ×1

cmd ×1

filter ×1

filtering ×1

list ×1

mysql ×1

path ×1

python ×1

python-2.7 ×1

regex ×1

string ×1

windows ×1