简体   繁体   中英

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 . 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. 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? And one last thing I am adding each item of L as keys and count as values in a dictionary .

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:

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 :

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.

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'. So i am assuming you want to compare 'AB' with 'BC', 'CD' , 'EF'. I can give you the logic in java. May be you can use it in python. List 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
Comparing AB with BC
Comparing AB with CD
Comparing AB with EF
Match failed!!!

Comparing BC with the rest of the elements
Comparing BC with CD
Comparing BC with EF
Comparing BC with GH
Match failed!!!

Comparing CD with the rest of the elements
Comparing CD with EF
Comparing CD with GH
Comparing CD with JK
Match failed!!!

Comparing EF with the rest of the elements
Comparing EF with GH
Comparing EF with JK
Comparing EF with LM
Match failed!!!

Comparing GH with the rest of the elements
Comparing GH with JK
Comparing GH with LM
Match failed!!!

Comparing JK with the rest of the elements
Comparing JK with LM
Match failed!!!

Comparing LM with the rest of the elements
Match failed!!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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