Hello guys,i have been stuck for days while trying to find how to solve this python problem. here is how the code looks like:

    set1=['78','18','79','56']
    set2=['13','40','16','04']
    set3=['63','70','78','60']
    set4=['10','35','66','13']
    set5=['32','41','71','70']
    set6=['50','58','02','11']

    set7=['13','40','41','05']
    set8=['12','52','50','60']
    set9=['71','13','66','12']
    set10=['50','90','73','41']
    set11=['09','18','44','54']
    set12=['12','41','32','67']

    Big_Set= [set1, set2, set3, set4, set5, set6, set7, set8, set9, set10, set11, set12]


    def occur(N):
        Done=[]
        dic={}
        i = -1
        for sets in Big_Set[:-1]:
            i += 1
            for item in sets:
                if item not in Done and not dic.has_key(i):
                    #an exception catching block
                    #in case the set run out of data (IndexError)
                    j=i
                    try:
                        if item in Big_Set[j+N+1]:
                            #checks if any item reoccurs after jumping N sets
                            print("%s reoccurs after %d sets at:" %(item,N))
                            print("set%d: %s" %((j+1),str(Big_Set[j])))
                            dic[j]=0
                            try:
                                while item in Big_Set[j+N+1]:
                                    #keeps checking if the item exists after N sets
                                    sets=Big_Set[j+N+1]
                                    print ("set%d: %s" %((j+N+2),str(sets)))
                                    j= j+N+1
                                    dic[j]=0
                            except IndexError: #in case we run out of data
                                pass
                            if item not in Done: Done.append(item)

                    except IndexError: #in case we run out of data
                        pass

        return Done

    def occur2(N):
        Done=[]
        dic={}
        i = -1
        for sets in Big_Set[:-1]:
            i += 1
            for item in sets:
                if item not in Done and not dic.has_key(i):
                    #an exception catching block
                    #in case the set run out of data (IndexError)
                    j=i
                    try:
                        if int(item)-20 in Big_Set[j+N+1]:
                            #checks if the item minus 20 exist after N number of sets 
                            print("%s minus 20 exists  after %d sets at:" %(item,N))
                            print("set%d: %s" %((j+1),str(Big_Set[j])))
                            dic[j]=0
                            try:
                                while int(item)-20 in Big_Set[j+N+1]:
                                    #keeps checking if the item exists after N sets
                                    sets=Big_Set[j+N+1]
                                    print ("set%d: %s" %((j+N+2),str(sets)))
                                    j= j+N+1
                                    dic[j]=0
                            except IndexError: #in case we run out of data
                                pass
                            if item not in Done: Done.append(item)

                    except IndexError: #in case we run out of data
                        pass

        return Done

the first issue is that my first function doesn work the way i want it to. For example when i run it in IDLE thus:

occur(4)
here is what i get:
13 reoccurs after 4 sets at:
set2: ['13', '40', '16', '04']
set7: ['13', '40', '66', '05']
60 reoccurs after 4 sets at:
set3: ['63', '70', '78', '60']
set8: ['12', '50', '77', '60']
66 reoccurs after 4 sets at:
set4: ['10', '35', '66', '13']
set9: ['71', '13', '66', '12']
41 reoccurs after 4 sets at:
set5: ['32', '41', '71', '70']
set10: ['50', '90', '73', '41']
['13', '60', '66', '41']
the issue is that it does not return the result for an item twice, for example when i run the function occur(4), i expected it to return (set2, set7) and (set4, set9) for item 13 but i got only (set2, set7) as seen in the result above. i also expected it to return (set5, set10) and (set7, set12) for item 41 but i only got (set5, set10).

(2) the second issue is that i wanted to modify the program i.e occur2, so that it can look up for an item as well as (the item minus 20) simultaneously with the both items separated by N sets.for example if i run the second function like this..

occur2(4). here is what i got
set2: ['13', '40', '16', '04']
set7: ['13', '40', '66', '05']
60 reoccurs after 4 sets at:
set3: ['63', '70', '78', '60']
set8: ['12', '50', '77', '60']
66 reoccurs after 4 sets at:
set4: ['10', '35', '66', '13']
set9: ['71', '13', '66', '12']
41 reoccurs after 4 sets at:
set5: ['32', '41', '71', '70']
set10: ['50', '90', '73', '41']
['13', '60', '66', '41']
i get the same answer as before, but i was expecting the program to return set3 and set8. this is becuase we have item=70 in set3 and if you subtraact 20 from 70, you will get 50. if you count 4 sets (N=4) you have set8 which contains the item 50.

pls can someone put me through on how to do this or if there is a better way to do this.

Please state precisely what your code is meant to achieve as I've no intention of making wild guesses or wading through it to try and find out.

@megaflo...please do find time to help me if you can.

like i said the first function doesnt do exactly what i want it to. i want it to to be able to return multiple results for any item in the sets if the item matches the search condition. it is only returning one result per item and leaving out other results.

the second issue is that i want to modify the first function so that it simultaneously search for let say any number in the sets (lets say this number is K) as well as the number minus 20(that is K-20). the number of sets between the set where the number k is present as well as where the number ( k-20) is present is denoted by N. N can be any number.
so if run the function>>> occur2(4)
the function searches for any number K and as well as K-20 separated by N=4 sets.

I suggest that you use actual sets instead of lists for your working data. That way I'd guess you could throw away 95% of the code by using standard set operations. See set-types

Comments
indeed

Here is how you could write occur(n) more simply

big_set = [
    ['78','18','79','56'],
    ['13','40','16','04'],
    ['63','70','78','60'],
    ['10','35','66','13'],
    ['32','41','71','70'],
    ['50','58','02','11'],
    ['13','40','41','05'],
    ['12','52','50','60'],
    ['71','13','66','12'],
    ['50','90','73','41'],
    ['09','18','44','54'],
    ['12','41','32','67'],
]

def gen_sublist(n):
    for i, seq in enumerate(big_set[n+1:]):
        yield i, i+n+1, big_set[i], big_set[i+n+1]

def occur(n):
    for i, j, a, b in gen_sublist(n):
        s = set(a) & set(b)
        for item in s:
            print("%s reoccurs after %d sets" % (item, n))
            print("set%d: %s" % (i+1, a))
            print("set%d: %s" % (j+1, b))

if __name__ == '__main__':
    occur(4)

Here is my result (with python 3)

13 reoccurs after 4 sets
set2: ['13', '40', '16', '04']
set7: ['13', '40', '41', '05']
40 reoccurs after 4 sets
set2: ['13', '40', '16', '04']
set7: ['13', '40', '41', '05']
60 reoccurs after 4 sets
set3: ['63', '70', '78', '60']
set8: ['12', '52', '50', '60']
13 reoccurs after 4 sets
set4: ['10', '35', '66', '13']
set9: ['71', '13', '66', '12']
66 reoccurs after 4 sets
set4: ['10', '35', '66', '13']
set9: ['71', '13', '66', '12']
41 reoccurs after 4 sets
set5: ['32', '41', '71', '70']
set10: ['50', '90', '73', '41']
41 reoccurs after 4 sets
set7: ['13', '40', '41', '05']
set12: ['12', '41', '32', '67']

How come you have almost exactly the same issues as one year ago ? Do you attend the same course for the second time ?

Edited 1 Year Ago by Gribouillis

@Gribouillis...Thank you for your reply. Your code is actually simpler and do return more than one result for a given integer. this is what I expected my code to do.
Am going to study your code and then find out how to rewrite it my own way.

please can you modify this your code to another function such that it works this way:
it takes N as an argument just like before but N now becomes the difference in sets between where an interger( let's say K ) exist in Big_sets and where another integer less than K ( K-20) also exist in Big_sets.
for clarification let's say the function now becomes Occur2(N)
from the data in your example
let's say we run occur2(2)

the function should return or print set4 and set7 as well as set6 and set9. ( because the data contains 70 in set4 and then after 2 sets (N=2), there is integar 50 (in set7) which is 20 less than 70.

As for your observation, lol....just resumed learning Python after about 10 months. I was already getting frustrated . But I wanna continue now

Here is function occur2()

def occur2(n):
    for i, j, a, b in gen_sublist(n):
        s = [x for x in a if str(int(x) - 20) in b]
        for item in s:
            print("%s reoccurs after %d sets" % (item, n))
            print("%s in set%d: %s" % (item, i+1, a))
            print("%s in set%d: %s" % (str(int(item) - 20), j+1, b))

if __name__ == '__main__':
    occur2(2)

The output is

70 reoccurs after 2 sets
70 in set3: ['63', '70', '78', '60']
50 in set6: ['50', '58', '02', '11']
78 reoccurs after 2 sets
78 in set3: ['63', '70', '78', '60']
58 in set6: ['50', '58', '02', '11']
32 reoccurs after 2 sets
32 in set5: ['32', '41', '71', '70']
12 in set8: ['12', '52', '50', '60']
70 reoccurs after 2 sets
70 in set5: ['32', '41', '71', '70']
50 in set8: ['12', '52', '50', '60']

thank you sir....you made my day
I wish I can have someone like you teach me python.

Unfortunately you're unlikely to learn much as you've been spoon fed the answer. That is because when somebody shows you how to do something, the route in your brain that is taken is not the same as the one as when you have to at least try and find the solution for yourself, with the latter being better at helping you to remember.

This question has already been answered. Start a new discussion instead.