@Gribouillis and other gurus in the house, i just did this code but seem i ssem to be having difficulties in modifying it to work the way i want it.Its a bit lenghty, pls pardon me because am still a beginner in python.This program prints out where a particular property occurs depending on whether or not the number and position is specified.
set1=['34','45','67','56']
set2=['12','71','78','20']
set3=['76','73','36','32']
set4=['34','57','67','88']
set5=['78','45','82','28']
set6=['52','10','67','41']
set7=['58','78','77','80']
set8=['57','45','68','20']
set9=['67','36','29','53']
set10=['78','79','69','21']
set11=['64','90','35','33']
set12=['45','90','52','72']
Big_Set=[set1, set2, set3, set4, set5, set6, set7, set8, set9, set10, set11, set12]
def occur(N):
Done=[]
i=0
for sets in Big_Set:
for item in sets:
if item not in Done:
#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])))
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
except IndexError: #in case we run out of data
pass
Done.append(item)
except IndexError: #in case we run out of data
pass
i+=1
return Done
def occur2(N, item): #when the number is specified
Done=[]
i=0
for sets in Big_Set:
if item in sets:
if item not in Done:
#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 the number specified reoccurs after jumping N sets
print("%s reoccurs after %d sets at:" %(item,N))
print("set%d: %s" %((j+1),str(Big_Set[j])))
try:
while item in Big_Set[j+N+1]:
#keeps checking if the numbr specified exists after N sets
sets=Big_Set[j+N+1]
print("set%d: %s" %((j+N+2),str(sets)))
j= j+N+1
except IndexError: #in case we run out of data
pass
Done.append(item)
except IndexError: #in case we run out of data
pass
i+=1
return Done, item #returns a tuple (Done,item)
def occur3(N, item, pos): #when both the number and position is specified
Done=[]
i=0
j=i
for sets in Big_Set:
if item in sets and (item == sets[pos]):
if item not in Done:
#an exception catching block
#in case the set run out of data (IndexError)
j=i
try:
if item in Big_Set[j+N+1] and item == Big_Set[j+N+1][pos]:
#checks if the number specified exists after N sets and at the specified position
print("%s reoccurs after %d sets at position %d in:"%(item,N,pos))
print("set%d: %s" %((j+1),str(Big_Set[j])))
try:
while item in Big_Set[j+N+1]and item == Big_Set[j+N+1][pos]:
#keeps checking if the item exists after N sets at the specified position
sets=Big_Set[j+N+1]
print("set%d: %s" %((j+N+2),str(sets)))
j= j+N+1
except IndexError: #in case we run out of data
pass
Done.append(item)
except IndexError: #in case we run out of data
pass
i+=1
return Done, item, pos #returns a tuple (Done,item,pos)
def run_occurs(N,i,p):#this program runs the functions above
N=N
item=i
position=p
if not item and type(position)!=type(3):#when number and position is unspecified
print("N is: %d" %(N))
y=occur(N)
if not y:
print("No item matches the search criteria")
elif type(position)!=type(3) and item:#when only position is unspecified
print("N is: %d" %(N))
print("item is %s: " %(item))
y=occur2(N,item)
if not y[0]:
print("%s does not match that search criteria" %(y[1]))
elif type(position)==type(3) and item:
#when number and position are specified
print("N is: %d" %(N))
print("item is: %s" %(item))
print("Position is %d: " %(position))
y=occur3(N,item,position)
if not y[0]:
print ("%s does not match the search criteria at position %d" %(y[1],y[2]))
if __name__== '__main__':
item=None
position= None
N=None
while not N: #this loops until a number is specified
print("Please insert the number of sets to jump: ")
try:
N= input()
except:
print("Invalid input. Input must be a number\n")
print("Do u want to specify the number to search for? (Y/N)")
print("Please type Y for yes or N for no")
search_number= raw_input().upper()
while search_number != 'Y' and search_number != 'N':
print("Invalid input")
print("Please type Y for yes or N for no")
search_number= raw_input().upper()
if search_number == 'Y':
while not item:
item= raw_input("type in the number: ")
try:
int(item)
except:
print("Invalid input. Input must be a number\n")
item= None
print("Do u want to specify the the position to search for? (Y/N)")
print("Please type Y for yes or N for no")
search_position= raw_input().upper()
while search_position != 'Y' and search_position != 'N':
print("Invalid input")
print("Please type Y for yes or N for no")
search_position= raw_input().upper()
if search_position == 'Y':
while type(position)!= type(3):
try:
position= int(raw_input("insert position here: "))
except:
print("Invalid input. Input must be a number\n")
else:
print("You have chosen not to specify any position\n")
else:
print("You have chosen not to specify a number\n")
run_occurs(N,item,position)
Ok, i ran the first function called occur(N).N is the number of sets an item must skip before re-occurring.so, i input N=2. here are the results:
N is: 2
34 reoccurs after 2 sets at:
set1: ['34', '45', '67', '56']
set4: ['34', '43', '67', '88']
67 reoccurs after 2 sets at:
set1: ['34', '45', '67', '56']
set4: ['34', '43', '67', '88']
78 reoccurs after 2 sets at:
set2: ['12', '71', '78', '20']
set5: ['78', '63', '82', '28']
the only problem i have with this function is that it only returns one result per integer.In addition to the results above i expected the function to return set7 and set10 for integer 78 and set9 and set12 for integer 34.can anyone help me with this modification.
i ran the second function called occur2(N, item).i input N=2 and item=34.here is the result
34 reoccurs after 2 sets at:
set1: ['34', '45', '67', '56']
set4: ['34', '43', '67', '88']
in addition to this result, i expected the function to return set9 and set12 according to the data above.i think its same modification that function 1 requires that this also requires.
i ran the third fuction called occur3(N, item, position).i used N=3, item=45 and position=1 and here is the reslut:
N is: 3
item is: 45
Position is 1:
45 reoccurs after 3 sets at position 1 in:
set1: ['34', '45', '67', '56']
set5: ['78', '45', '82', '28']
in addtion to this result, i expected the program to return set8 and set12 for item 45.after looking at the code again, i discovered that the function only works for an item that moves from a particular position after N sets to the same postion i.e for the example above, 45 is in postion 1 in both set1 and set5, but 45 in set8 is also in position 1 but not in postion 1 in set12(hence is not returned as a valid output).so, i need help in modifying the code so that the position characteristics only refers to the former position an item occupies in the former set and not the position it occupies in the latter set.
i.e from the example above (using 45 as item), position will refer to postion 1 in set1 only and not position 1 in set5.if this is done, then set8 and set12 will be included in the result if i input N=3, item=45 and position=1..pls gurus in the house, i need your help on these problems.
bright.silva 0 Newbie Poster
woooee 814 Nearly a Posting Maven
I am not an expert, and this is more code than I want to wade through, so am not positive about what you are trying to do. The first part, to get the difference between numbers that occur in more than one set, is below. The program breaks the test into two separate parts, first it finds all matches and adds to a dictionary, and since I am not sure if you want everything greater than "n" or equal to, it does both.
from collections import defaultdict
def occur(lists_in):
## key = search number pointing to a list of numbers of
## the "sets" that contain the search number
return_dictionary = defaultdict(list)
## loop though each list in order using a counter to reference the list
for outer in range(0, len(lists_in)):
for outer_num in lists_in[outer]:
##loop through other lists starting with outer+1
for inner in range(outer+1, len(lists_in)):
if outer_num in lists_in[inner]:
return_dictionary[outer_num].append([outer, inner])
return return_dictionary
list_of_lists=[['34','45','67','56'],
['12','71','78','20'],
['76','73','36','32'],
['34','57','67','88'],
['78','45','82','28'],
['52','10','67','41'],
['58','78','77','80'],
['57','45','68','20'],
['67','36','29','53'],
['78','79','69','21'],
['64','90','35','33'],
['45','90','52','72']]
return_dictionary = occur(list_of_lists)
n=2
for key in return_dictionary:
for each_list in return_dictionary[key]:
lit = ""
dif = each_list[1] - each_list[0] ## found-original "set"
if n+1 == dif:
lit="skips exactly the required number"
elif n < dif:
lit=" skips the required number"
print "%s occurs in %d and %d %s" % \
(key, each_list[0], each_list[1], lit)
Edited by woooee
bright.silva 0 Newbie Poster
@wooee:thanks for your reply. i your code and i will its cool, though it didnt solve my problems.Your code executes the function on all the item in the data and doesnt take argument N which allows a user to downsize results as well as making the program to be more specific in its outputs.
Though, i am ok with the type of results returned by my code,i just need a few tweaks in the code to make it work better.
the code has three functions.
(1)occur(N):this function takes only one argument called N. N is the number of sets in the data an item/ number must skip before re-occuring in another set.like i said in my first post,the only modification i need is to make this function return more than one result for an item if the item satisfies the function conditions more than once(pls check my first post to see the examples).
(2)occur2(N, item):this function takes two argument and the difference between this function and the first is that this function allows the user to specify the item/number the program should search for.Also, this function is working well as i expected but also has the same problem of not been able to return more than one result per item when the item in the data satisfies the search condition more than oncce(please check my first post to understand what i mean)
(3)occur3(N, item, position):this function allows the user to stae the item ,the postion of the item as well as the number of sets the item must skip.the limitation of this function is that an item must be in the same postion in the first where it first occurs as well as in the sets where it re-occurs before this function can return a postive result. all i want is for the postion argument to be restricted only to the first set where the specified item occurs and not where it re-occurs after N sets.(please check my first post understand what i mean)
so, if you have got time, please help go through my code and see what adjustments can be made to suit these modifications.
thanks.
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.