Python Forum
The issue with compare 2D lists
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
The issue with compare 2D lists
#11
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
#12
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
#13
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
Download my project scripts


Reply
#14
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
#15
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
Download my project scripts


Reply
#16
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
#17
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
#18
dvcdscds
Reply
#19
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 2,426 May-23-2024, 07:23 PM
Last Post: deanhystad
  Compare two lists (with intersections). wnesbv 0 1,767 Jul-06-2022, 09:07 AM
Last Post: wnesbv
  Split dict of lists into smaller dicts of lists. pcs3rd 3 3,989 Sep-19-2020, 09:12 AM
Last Post: ibreeden
  Compare Two Lists and Replace Items In a List by Index nagymusic 2 4,299 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 3,988 Apr-22-2020, 10:22 AM
Last Post: Sutsro
  how to compare a list to a list of lists kevthew 1 2,796 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 6,503 Mar-20-2019, 08:01 PM
Last Post: stillsen
  loop to compare lists KHas 1 2,564 Jan-31-2019, 10:17 PM
Last Post: Larz60+
  Lists-compare lists elhetch 7 7,810 Mar-01-2017, 02:50 PM
Last Post: buran

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.