Collaps arrays/ list of intergers
Hi All, I do hope somebody can help me with the following: I have the followings lists which represent the upper and lower value of a range/array. a = [1,50] b = [75,150] c = [25,42] d = [120,149] e = [35,55] What I would like to happen is that overlapping range will "collapse" to a single range meaning the above list would become: as list a,c and e overlap they can be represented by f = [1,55] as list b and d overlap they can be represented by g = [75,150] I have sort of got working solution using networkx and numpy and they work perfect for the above example the problem I have no is my true data set looks more like the following: x = [135098,19854736] y = [135098,41639553] z = [11818998,12587339] To give only three examples using networkx and numpy does not work as this results in memory error due to the ranges being so large. I would appreciate any help with this. Thanks -- https://mail.python.org/mailman/listinfo/python-list
Re: Please help with Threading
On Saturday, 18 May 2013 10:58:13 UTC+2, Jurgens de Bruin wrote: > This is my first script where I want to use the python threading module. I > have a large dataset which is a list of dict this can be as much as 200 > dictionaries in the list. The final goal is a histogram for each dict 16 > histograms on a page ( 4x4 ) - this already works. > > What I currently do is a create a nested list [ [ {} ], [ {} ] ] each inner > list contains 16 dictionaries, thus each inner list is a single page of 16 > histograms. Iterating over the outer-list and creating the graphs takes to > long. So I would like multiple inner-list to be processes simultaneously and > creating the graphs in "parallel". > > I am trying to use the python threading for this. I create 4 threads loop > over the outer-list and send a inner-list to the thread. This seems to work > if my nested lists only contains 2 elements - thus less elements than > threads. Currently the scripts runs and then seems to get hung up. I monitor > the resource on my mac and python starts off good using 80% and when the > 4-thread is created the CPU usages drops to 0%. > > > > My thread creating is based on the following : > http://www.tutorialspoint.com/python/python_multithreading.htm > > > > Any help would be create!!! Thanks to all for the discussion/comments on threading, although I have not been commenting I have been following. I have learnt a lot and I am still reading up on everything mentioned. Thanks again Will see how I am going to solve my senario. -- http://mail.python.org/mailman/listinfo/python-list
Re: Please help with Threading
I will post code - the entire scripts is 1000 lines of code - can I post the threading functions only? -- http://mail.python.org/mailman/listinfo/python-list
Please help with Threading
This is my first script where I want to use the python threading module. I have a large dataset which is a list of dict this can be as much as 200 dictionaries in the list. The final goal is a histogram for each dict 16 histograms on a page ( 4x4 ) - this already works. What I currently do is a create a nested list [ [ {} ], [ {} ] ] each inner list contains 16 dictionaries, thus each inner list is a single page of 16 histograms. Iterating over the outer-list and creating the graphs takes to long. So I would like multiple inner-list to be processes simultaneously and creating the graphs in "parallel". I am trying to use the python threading for this. I create 4 threads loop over the outer-list and send a inner-list to the thread. This seems to work if my nested lists only contains 2 elements - thus less elements than threads. Currently the scripts runs and then seems to get hung up. I monitor the resource on my mac and python starts off good using 80% and when the 4-thread is created the CPU usages drops to 0%. My thread creating is based on the following : http://www.tutorialspoint.com/python/python_multithreading.htm Any help would be create!!! -- http://mail.python.org/mailman/listinfo/python-list
Re: Compare tuples of different lenght
On Aug 20, 12:17 pm, Steven D'Aprano wrote: > Jurgens de Bruin wrote: > > Hi, > > > I have a list of tuples: > > > [(2,),(12,13),(2,3,4),(8,),(5,6),(7,8,9),] > > > I would like to compare all the tuples to each other and if one > > element if found two tuples the smallest tuples is removed from the > > list. > > It's not clear what you mean by "smallest" tuple. Is (8,) smaller than > (7,8,9)? > > I'm going to guess you care only about the length of the tuple, and not the > items themselves. > > Let's start with a couple of helper functions. > > def compare(t1, t2): > 'Return -1 if t1 is "smaller" than t2, 0 if equal, and +1 if "bigger".' > if len(t1) < len(t2): return -1 > elif len(t1) > len(t2): return 1 > else: return 0 > > def match_any_item(t1, t2): > try: > s1 = set(t1) > s2 = set(t2) > return bool(s1 & s2) > except TypeError: > # Can't convert to sets because at least one item is mutable. > # Let's do this the slow(?) way. > matched = [x for x in t1 if x in t2] > return bool(matched) > > list_of_tuples = [(2,),(12,13),(2,3,4),(8,),(5,6),(7,8,9),] > flags = [True]*len(list_of_tuples) > for i,t1 in enumerate(list_of_tuples): > for j in range(i+1, len(list_of_tuples)): > t2 = list_of_tuples[j] > if match_any_item(t1, t2): > n = compare(t1, t2) > if n == -1: > # Flag t1 to be removed. > flags[i] = False > elif n == 1: > # Flag t2 to be removed. > flags[j] = False > > saved_tuples = [] > for t,flag in zip(list_of_tuples, flags): > if flag: saved_tuples.append(t) > > This gives: > > >>> saved_tuples > > [(12, 13), (2, 3, 4), (5, 6), (7, 8, 9)] > > which matches what you wanted: > > > [(12,13),(2,3,4),(5,6),(7,8,9),] > > -- > Steven Thanks Steven. This works great!!! Appreciated very much!!! -- http://mail.python.org/mailman/listinfo/python-list
Re: Compare tuples of different lenght
On Aug 20, 10:45 am, Chris Rebert wrote: > On Sat, Aug 20, 2011 at 1:25 AM, Jurgens de Bruin wrote: > > > Hi, > > > I have a list of tuples: > > > [(2,),(12,13),(2,3,4),(8,),(5,6),(7,8,9),] > > > I would like to compare all the tuples to each other and if one > > element if found two tuples the smallest tuples is removed from the > > list. > > So, would [(5,6), (6,7,8)] become [(6,7,8)] ? > > If no, then I believe you're trying to solve the set covering > problem:http://en.wikipedia.org/wiki/Set_cover_problem > > Cheers, > Chris > --http://rebertia.com [(5,6), (6,7,8)] will indeed become [(6,7,8)] Tanks!! -- http://mail.python.org/mailman/listinfo/python-list
Re: Compare tuples of different lenght
On Aug 20, 10:45 am, Chris Rebert wrote: > On Sat, Aug 20, 2011 at 1:25 AM, Jurgens de Bruin wrote: > > > Hi, > > > I have a list of tuples: > > > [(2,),(12,13),(2,3,4),(8,),(5,6),(7,8,9),] > > > I would like to compare all the tuples to each other and if one > > element if found two tuples the smallest tuples is removed from the > > list. > > So, would [(5,6), (6,7,8)] become [(6,7,8)] ? > > If no, then I believe you're trying to solve the set covering > problem:http://en.wikipedia.org/wiki/Set_cover_problem > > Cheers, > Chris > --http://rebertia.com [(5,6), (6,7,8)] would become [(6,7,8)]. Thanks for the response -- http://mail.python.org/mailman/listinfo/python-list
Compare tuples of different lenght
Hi, I have a list of tuples: [(2,),(12,13),(2,3,4),(8,),(5,6),(7,8,9),] I would like to compare all the tuples to each other and if one element if found two tuples the smallest tuples is removed from the list. example if tuple 1 and tuple 3 are compare it should find that a single element in each are the same and tuple 1 should be removed resulting in [(12,13),(2,3,4),(8,),(5,6),(7,8,9),] the same for tuple 4 and 6 resulting in [(12,13),(2,3,4),(5,6),(7,8,9),] is this possible as I am having no success. Thanks -- http://mail.python.org/mailman/listinfo/python-list