按字母搜索词过滤列表

时间:2018-10-28 23:38:29

标签: java android arrays search

我有一些列表,例如下面的示例,其中包含(Car&Racoon)单词。如果我想搜索“ cr”,我希望它返回“ Car”而不是“ Racoon”。下面是我目前的做法,但它不考虑字母顺序,也返回“ Racoon”。我想要一个对所有用空格分隔的搜索词/字母尽可能灵活的解决方案。

kernel void dilation(
    texture2d<uint, access::read> inTexture [[texture(0)]],
    texture2d<uint, access::write> outTexture [[texture(1)]],
    device uint *kernelSize [[buffer(0)]],
    uint2 gid [[thread_position_in_grid]]
) {
    uint halfKernel = kernelSize[0] / 2;
    uint minX = gid.x >= halfKernel ? gid.x - halfKernel : 0;
    uint minY = gid.y >= halfKernel ? gid.y - halfKernel : 0;
    uint maxX = gid.x + halfKernel < inTexture.get_width() ? gid.x + halfKernel : inTexture.get_width();
    uint maxY = gid.y + halfKernel < inTexture.get_height() ? gid.y + halfKernel : inTexture.get_height();
    uint maxValue = 0;
    for (uint i = minX; i <= maxX; i++)
    {
        for (uint j = minY; j <= maxY; j++)
        {
            uint4 value = inTexture.read(uint2(i, j));
            if (maxValue < value[0])
                maxValue = value[0];
        }
    }
    outTexture.write(maxValue, gid);
}

1 个答案:

答案 0 :(得分:1)

这是正则表达式的一个很好的用例。 尝试这样的事情:

import glob
import numpy as np
import matplotlib.pyplot as plt

msd_files = (glob.glob('MSD_no_fs*'))
msd_all=[]

for msd_file in msd_files:
    with open(msd_file) as f:
        for line in f:
            # msd_all.append([float(v) for v in line.strip().split(',')])
            msd_all.append(float(line.strip()))

msa_array = np.array(msd_all)
x=np.split(msa_array,99)
x=np.array(x)
result=np.mean(x,axis=0)
print(result.shape)
print(len(result))

如您所见,我找到每个空格并将其替换为 String[] wordsList = {"Car", "Racoon"}; String searchInput = "c r"; String searchRegEx = searchInput.replace(" ", ".{1}"); Pattern pattern = Pattern.compile(searchRegEx, Pattern.CASE_INSENSITIVE); System.out.println("RegEx (case-insensitive) is: " + searchRegEx); for (String word : wordsList){ Matcher matcher = pattern.matcher(word); boolean match = matcher.matches(); System.out.println("Test word '"+word + "' and match was: " + match); } ,这意味着任何字符都只是一个实例。如果您想开放一个或多个字符的匹配范围,可以改用.{1}之类的东西。或者,您可以更具体一些,并指定只匹配字母a-z大写和小写:.+[a-zA-Z]{1}[a-zA-Z]+很重要,因为否则您的Pattern.CASE_INSENSTIVE的大写C单词列表将与小写输入不匹配。

在这种情况下,编译模式是一项重要的优化措施。如您所知,RegEx可能很慢,如果在for循环中内联它,它将为每个测试编译您的正则表达式,这将很慢且效率低。