简体   繁体   English

在python中的一定间隔内比较列表项

[英]comparing a list item within certain interval in python

Suppose, I have a list of string like following: 假设我有一个字符串列表,如下所示:

L = ['AB','BC','CD','EF','JK','LM']

Now I want to compare the first item that is AB to first three items of L that would be 'AB','BC','CD' and if find any match I will increase count by 1 and store in dictionary . 现在,我要比较的first itemABfirst three items升,这将是'AB','BC','CD' ,如果发现任何比赛,我会increase count by 1中并存储dictionary Following this, I wish to compare the second item of L that is 'BC' to the 'BC','CD','EF' that from 'BC' to next two items and so on. 在此之后,我希望将L的第二个项目“ BC”与从“ BC”到下两个项目的“ BC”,“ CD”,“ EF”进行比较,依此类推。 First item would be comparing to first three items of L, second item of L would be comparing to three items of L starting from index 1 and third item of L would be comparing to three items of L starting from L[2] to next three items of L. How can i do it in Python? 第一项与L的前三项进行比较,第二项L与从索引1开始的三项L进行比较,L第三项与从L [2]到下三项的L的三项进行比较L的各项。如何在Python中完成? And one last thing I am adding each item of L as keys and count as values in a dictionary . 最后一件事是,我将L的每个项添加为keys and countdictionary keys and count为值。

My codes seem not working. 我的代码似乎不起作用。

for i in range(0,len(L)-1):

    for ii in range(i,3+i):
        count = 0

        if L[i] == L[ii]:
            count = 1 + count
            dic[L[i]] = count

You can do it simply by slice technique: 您可以通过slice技术简单地做到这一点:

L = ['AB', 'AB', 'CD', 'EF', 'JK', 'LM']
count = {key: 0 for key in L}
for i in range(len(L)):
    count[L[i]] = count[L[i]] + len([ele for ele in L[i:i+3] if ele == L[i]])

print count

Output: 输出:

{'JK': 1, 'AB': 3, 'LM': 1, 'EF': 1, 'CD': 1}

I'm not quite sure what are you going to achieve, but here is a little bit more pythonic implementation with enumerate , list generator and slices instead of plain indexing via range : 我不太确定你要实现什么,但是这里有一些更多的pythonic实现,它带有enumeratelist generatorslices而不是通过range进行普通索引:

L = ['AB', 'AB', 'CD', 'EF', 'JK', 'LM']
stats = {key: 0 for key in L}
for index, value in enumerate(L):
    lookforward = L[index:index+2]
    lookforward = [item for item in lookforward if item == value]
    value_counter = len(lookforward)
    stats[value] += value_counter

Please note that the result of this code will be 请注意,此代码的结果将是

stats = {'AB': 3, 'EF': 1, 'JK': 1, 'CD': 1, 'LM': 1}

With 'AB': 3 since it follows the algorithm you described in text. 使用'AB': 3因为它遵循您在文本中描述的算法。

Just to be clear, if you are going to compare 'AB' with 'AB', 'BC', 'CD' , then always the first loop of comparison is going to return true as you are comparing 'AB' with 'AB'. 只是要清楚一点,如果您要比较“ AB”与“ AB”,“ BC”,“ CD”,那么当您将“ AB”与“ AB”进行比较时,总是第一个比较循环返回true 。 So i am assuming you want to compare 'AB' with 'BC', 'CD' , 'EF'. 所以我假设您想将“ AB”与“ BC”,“ CD”,“ EF”进行比较。 I can give you the logic in java. 我可以在Java中给您逻辑。 May be you can use it in python. 也许您可以在python中使用它。 List listToCompare = {'AB', 'BC', 'CD', 'EF', 'GH', 'JK', 'LM'} 列表listToCompare = {'AB','BC','CD','EF','GH','JK','LM'}

 int compStrIndex =0;
 for (String str : listToCompare){
        System.out.println("Comparing "+listToCompare.get(compStrIndex)+" with the rest of the elements");
        int x = compareString(compStrIndex, listToCompare);
        if (x==1){
            System.out.println("Got match!!!");
        }else{
            System.out.println("Match failed!!!");
        }
        compStrIndex++;
    }

private static int compareString(int compStrIndex, List<String> listToComp){
    String strToComp = listToComp.get(compStrIndex);
    int compIndex = ++compStrIndex;
    int cnt = 0;

    while(cnt<3){
        System.out.println("Comparing "+ strToComp + " with "+ listToComp.get(compIndex));
        if(strToComp.equals(listToComp.get(compIndex))){
            return 1;
        }
        compIndex++;
        cnt++;
    }

    return 0;       
}

Below is the output i received: 以下是我收到的输出:
Comparing AB with the rest of the elements 将AB与其余元素进行比较
Comparing AB with BC AB与BC比较
Comparing AB with CD 比较AB与CD
Comparing AB with EF AB与EF的比较
Match failed!!! 比赛失败!!!

Comparing BC with the rest of the elements 将BC与其余元素进行比较
Comparing BC with CD 比较BC和CD
Comparing BC with EF BC与EF的比较
Comparing BC with GH 比较BC与GH
Match failed!!! 比赛失败!!!

Comparing CD with the rest of the elements 将CD与其他元素进行比较
Comparing CD with EF 将CD与EF比较
Comparing CD with GH 将CD与GH比较
Comparing CD with JK 将CD与JK比较
Match failed!!! 比赛失败!!!

Comparing EF with the rest of the elements 将EF与其余元素进行比较
Comparing EF with GH EF与GH比较
Comparing EF with JK EF与JK的比较
Comparing EF with LM EF与LM的比较
Match failed!!! 比赛失败!!!

Comparing GH with the rest of the elements 将GH与其余元素进行比较
Comparing GH with JK GH与JK比较
Comparing GH with LM GH与LM比较
Match failed!!! 比赛失败!!!

Comparing JK with the rest of the elements 将JK与其余元素进行比较
Comparing JK with LM 将JK与LM比较
Match failed!!! 比赛失败!!!

Comparing LM with the rest of the elements 将LM与其余元素进行比较
Match failed!!! 比赛失败!!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM