Srinivas Iyyer wrote:
Dear Alan and tutors, I apologize for careless description of my problem that lead to confusion. the problem arises due to large range of numbers and range has direction (both ascending and descending).I am giving an example from my real problem. What I want to do: I want to check if a[.][1] and a[.][2] are 'within the range' of any element of b[.][1] or b[.][2]. (here '.' means any sub-element of list b. a[.][1] and a[.][2] or b[.][1] b[.][2] can be either ascending or descending order Ascending example from list a and b: 'xa','1511255', '1511279' 'xb','7516599','7516623' 'G1','1511200','1511325' 'G2','7516500','7516625' descending order from list a and b: 'xc','98356290','98356266' 'G3','98356335','98356126' a = [['xa','1511255', '1511279'],['xb','7516599','7516623'],['xc','98356290','98356266']] b = [['G1','1511200','1511325'],['G2','7516500','7516625'],['G3','98356335','98356126']]for item1 in a:... i1st = int(item1[1]) ... i1en = int(item1[2]) ... for item2 in b: ... i21 = int(item2[1]) ... i22 = int(item2[2]) ... if i1st and i1en in xrange(i21,i22): ... print "\t".join(item1)+'\t'+"\t".join(item2) ... if i1st and i1en in xrange(i22,i21): ... print "\t".join(item1)+'\t'+"\t".join(item2) ... xa 1511255 1511279 G1 1511200 1511325 xb 7516599 7516623 G2 7516500 7516625 xc 98356290 98356266 G3 9835i6335 98356126 Issue 1: a. Range of numbers is too high and xrange is also too slow. I used xrange instead of range - a slow process You do not need range or xrange! Issue 2: Is this is a correct way to tackle this kind of problem. Correct in that is solves the problem but is very inefficient. I would make a copy of each list; take a pass thru each copy, convert the strings to integers and reverse the descending pairs. a1 = [['xa', 1511255, 1511279],['xb', 7516599, 7516623],['xc', 98356266, 98356290]] b1 = [['G1', 1511200, 1511325],['G2', 7516500, 7516625],['G3', 98356126, 98356335]] Then compare lower and upper limits (no need for range):for a2 in a1: for b2 in b1: if a2[1] >= b2[1] and a2[2] < b2[2]: print the matched result break else: print the unmatched result. -- Bob Gailer Chapel Hill NC 919-636-4239 When we take the time to be aware of our feelings and needs we have more satisfying interatctions with others. Nonviolent Communication provides tools for this awareness. As a coach and trainer I can assist you in learning this process. What is YOUR biggest relationship challenge? |
_______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor