##### The issue with compare 2D lists
 The issue with compare 2D lists Posts: 6,477 Threads: 18 Joined: Feb 2020 Reputation: Jul-15-2024, 03:21 PM (This post was last modified: Jul-15-2024, 04:00 PM by deanhystad.) Provide some example data and corresponding example output. I think there's a lot of confusion about what you are trying to do. For example, why do you have multiple lists of items? Normally I would expect a list of bins/compartments and a list of items. Binning would sort through the list of items and place each item in the correct bin. Are the item lists in your list_of_items pre-binned, i.e. do all items in a list fit in one container? Changing my previous post to work with item number/text tuples. ```list_of_items = [ [(171, "text"), (172, "text"), (173, "text")], [(567, "text"), (568, "text"), (569, "text")], ] list_of_compartments = [(472, 588), (110, 260),] def get_items(compartment): min_, max_ = compartment for items in list_of_items: if min_ <= items[0][0] <= max_: return items return items compartments = {c: get_items(c) for c in sorted(list_of_compartments)} print(*compartments.items(), sep="\n")`````````Output:((110, 260), [(171, 'text'), (172, 'text'), (173, 'text')]) ((472, 588), [(567, 'text'), (568, 'text'), (569, 'text')])`````` Reply przonak007 Programmer named Tim Posts: 7 Threads: 1 Joined: Jul 2024 Reputation: Jul-15-2024, 03:23 PM This is a part of list list_of_items = [[171, 'Herbert'], [172, 'Williams'], [173, '34'], [174, 'brown'],[175, '{medium}'], [176, 'FGO'], [177, '{53227650}'], [567, 'Edgar'], [568, 'Davis'], [569, '21'], [570, 'green'], [571, '{medium}'], [572, 'JRV'],[ 573, '{42965307}']] Reply menator01 Da Bishop Posts: 1,037 Threads: 108 Joined: Sep 2019 Reputation: Jul-15-2024, 03:34 PM Are you wanting the numbers grouped together? I welcome all feedback. The only dumb question, is one that doesn't get asked. My Github How to post code using bbtags Reply przonak007 Programmer named Tim Posts: 7 Threads: 1 Joined: Jul 2024 Reputation: Jul-15-2024, 05:45 PM (This post was last modified: Jul-15-2024, 05:54 PM by przonak007.) Yes, I would like the numbers to be grouped like this The compartment: (110, 260) 171, Herbert 172, Williams 173, 34 ... ... The compartment: (472, 588) 567, Edgar 568, Davis 569, 21 ..... .... Reply menator01 Da Bishop Posts: 1,037 Threads: 108 Joined: Sep 2019 Reputation: Jul-15-2024, 05:52 PM The example I posted should give that. Or one of the other examples. I welcome all feedback. The only dumb question, is one that doesn't get asked. My Github How to post code using bbtags Reply przonak007 Programmer named Tim Posts: 7 Threads: 1 Joined: Jul 2024 Reputation: Jul-15-2024, 06:03 PM Your code on the post works fine but only with numbers. When I added text as second parameter then the code stopped working. I edited my last post. I added how should be like gruped with new data Reply Posts: 6,477 Threads: 18 Joined: Feb 2020 Reputation: Jul-15-2024, 06:03 PM This assumes the items are not already grouped together in compartments. ```items = [ (173, "text"), (3, "text"), (171, "text"), (567, "text"), (172, "text"), (600, "text"), (569, "text"), (568, "text"), ] bins = {(110, 260): [], (472, 588): []} def bin_items(items, bins): bins = {b: [] for b in bins} default = [] for item in items: for (min_, max_), bin_ in bins.items(): if min_ <= item[0] <= max_: bin_.append(item) break else: default.append(item) if default: bins["Misfits"] = default return bins bins = bin_items(items, ((110, 260), (472, 588))) for b, items in bins.items(): print("The compartment", b) for i in sorted(items): print(*i) print()`````````Output:The compartment (110, 260) 171 text 172 text 173 text The compartment (472, 588) 567 text 568 text 569 text The compartment Misfits 3 text 600 text`````` Reply Apptunix10 Unladen Swallow Posts: 1 Threads: 0 Joined: Jul 2024 Reputation: Jul-16-2024, 06:59 AM dvcdscds Reply Pedroski55 Giant Foot Posts: 993 Threads: 141 Joined: Jul 2017 Reputation: Jul-17-2024, 07:31 AM Just for fun I made this: ```from random import randint, choices, choice from string import ascii_lowercase, ascii_uppercase # doesn't matter how many compartments compartments = [(100, 200), (300, 400), (500, 600)] items_dict = {f'compartment{i + 1}': [] for i in range(len(compartments))} # fake some names def make_a_name(): first = choice(ascii_uppercase) second = ''.join(choices(ascii_lowercase, k=5)) name = first + second return name # run all lists of items together by adding # all_items = items1 + items2 + items3 # doesn't matter what the second element in each tuple of all_items is # here we only have tuples of this format (number, text) but the tuple can be (number, anything) all_items = [(randint(20, 700), make_a_name()) for i in range(randint(200, 400))] len(all_items) # like 350 or 236 # a generator in case there are a lot of compartments range_gen = (comp for comp in compartments) # check all items against all low and high # maybe the lists are not clearly separated # the list all_items may contain a number or a name more than once def check_num(key, low, high): # reset the generator items_gen each time around items_gen = (i for i in all_items) for item in items_gen: if low <= item[0] <= high: items_dict[key].append(item) # for each key in items_dict check all elements of all_items # the function check_num will assign the element to the correct compartment in items_dict for key in items_dict.keys(): low, high = next(range_gen) check_num(key, low, high) # sort the lists for key in items_dict.keys(): items_dict[key].sort() # print 10 elements from each result list for key in items_dict.keys(): for i in range(10): print(items_dict[key][i])``` Reply

 Possibly Related Threads… Thread Author Replies Views Last Post Compare lists w_i_k_i_d 6 717 May-23-2024, 07:23 PM Last Post: deanhystad Compare two lists (with intersections). wnesbv 0 1,138 Jul-06-2022, 09:07 AM Last Post: wnesbv Split dict of lists into smaller dicts of lists. pcs3rd 3 2,751 Sep-19-2020, 09:12 AM Last Post: ibreeden Compare Two Lists and Replace Items In a List by Index nagymusic 2 3,186 May-10-2020, 05:28 AM Last Post: deanhystad Lists first item is a number however i cant compare it with an int getting syntax err Sutsro 4 2,648 Apr-22-2020, 10:22 AM Last Post: Sutsro how to compare a list to a list of lists kevthew 1 1,976 Dec-22-2019, 11:43 AM Last Post: ibreeden sort lists of lists with multiple criteria: similar values need to be treated equal stillsen 2 3,707 Mar-20-2019, 08:01 PM Last Post: stillsen loop to compare lists KHas 1 1,845 Jan-31-2019, 10:17 PM Last Post: Larz60+ Lists-compare lists elhetch 7 6,062 Mar-01-2017, 02:50 PM Last Post: buran

Forum Jump:

### User Panel Messages

##### Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020