在列表中搜索数组中的某些字符串

时间:2019-05-25 16:21:02

标签: python

我正在做的是从.txt文件中加载URL列表,该文件应能正常工作:

def load_urls():
    try:
        input_file = open("filters\\inputLinks.txt", "r")
        for each_line in input_file:
            link = each_line.rstrip('\n')
            identify_platform(link)
    except Exception as e: 
        print("Loading URLs error: ", e)

def identify_platform(link):
    try:
        SEARCH_FOR = ["/node/", "/itemlist/"]
        if any(found in link for found in SEARCH_FOR):
            print(link)
    except Exception as e: 
        print("Identifying URLs error: ", e)

if __name__ == "__main__":
    load_urls()

然后它将检查URL是否包含SEARCH_FOR数组元素之一。如果是这样,我们将其打印到屏幕上。是否有可能使用类似以下的命令打印出找到的数组元素之一:

print(element_found + "|" + link)

2 个答案:

答案 0 :(得分:2)

如果将您分成多行,则可能会使其更具可读性:

for found in SEARCH_FOR:
    if found in link:
        print(found + '|' + link)

答案 1 :(得分:1)

仅使用列表推导即可代替使用#include <intrin.h> #include <stdio.h> #include <stdlib.h> #include <time.h> #include <math.h> #include <vector> #include <assert.h> #include <omp.h> using namespace std; void convolution(float* output, float* input, float* filter, int width, int height, int r) { assert(output!=NULL && input!=NULL && filter!=NULL && width>0 && height>0 && r>0); int w1=width-1; int h1=height-1; int fwidth=2*r+1; int i, j, di, dj, ii, jj; float sum; for (i=0;i<height;++i) { for (j=0;j<width;++j) { sum=0; for (di=-r;di<=r;++di) { ii=i+di; ii=max(min(ii,h1),0); for (dj=-r;dj<=r;++dj) { jj=j+dj; jj=max(min(jj,w1),0); sum+=filter[dj+r+(di+r)*fwidth]*input[jj+ii*width]; } } output[j+i*width]=sum; } } } int main() { // load the image int width=1920; // width of the image int height=1080; // height of the image int len=width*height; // pixels in the image int i, j, ii, jj, i2; float* data=(float*)malloc(sizeof(float)*len); // buffer to load the image float* output=(float*)malloc(sizeof(float)*len); // output buffer FILE* fp=fopen("../image.dat", "rb"); // open the image, assume that the bld directory is a subdirectory to the src directory fread(data, sizeof(float), width*height, fp); // load the float values, the image is gray. fclose(fp); // set the filter int radius=3; // filter radius float sigma=(float)(radius/3.0); // standard deviation of the Gaussian filter float beta=(float)(-0.5/(sigma*sigma)); // coefficient exp(beta*x*x) int fwidth=2*radius+1; // width of the filter int flen=fwidth*fwidth; // number of elements in the filter float* filter=(float*)malloc(sizeof(float)*flen); // filter buffer float sum_weight=0; // we want to normalize the filter weights for (i=-radius;i<=radius;++i) { ii=(i+radius)*fwidth; i2=i*i; for (j=-radius;j<=radius;++j) { jj=j+radius+ii; filter[jj]=exp(beta*(i2+j*j)); sum_weight+=filter[jj]; } } sum_weight=(float)(1.0/sum_weight); for (i=0;i<flen;++i) filter[i]*=sum_weight; // now the weights are normalized to sum to 1 clock_t start=clock(); convolution(output, data, filter, width, height, radius); clock_t finish=clock(); double duration = (double)(finish - start) / CLOCKS_PER_SEC; printf( "convolution naive: %2.3f seconds\n", duration ); float* output2=(float*)malloc(sizeof(float)*len); // output buffer start=clock(); your_convolution(output2, data, filter, width, height, radius); finish=clock(); double duration2 = (double)(finish - start) / CLOCKS_PER_SEC; printf( "your convolution: %2.3f seconds\n", duration2 ); double sum=0; for (i=0;i<len;++i) sum+=fabs(output[i]-output2[i]); printf("difference of the outputs=%lf\n", sum); printf( "The performance of your convolve is %2.1f times higher than convolution naive.\n", duration/duration2); free(data); free(filter); free(output); return 0; } 运算符。请注意,如果两个搜索项都在目标中,这可能会给您2条结果。

any