Python Forum
Make Groups with the List Elements
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Make Groups with the List Elements

I have pairs list:
And I have also a queue list which is includes:
queue =[A,B,C,D,E,F,G,H,I,Z,X,J,K,L]
And I want to make groups from list queue and this groups should be exactly the same with elements of pairs. I mean, I want to make [A,B] [C,D] [Z,X] groups if these elements[A,B,C,DZ,X] exist inside the queue list.
Note: here A,B,C,D,Z,X are just an example. In my code they correspond objects

How can I do that?
loop over the pairs, check if both pair items are in the list of queue items, if they are add the pair to a new list
quest likes this post
(Jul-11-2021, 08:35 AM)quest Wrote: How can I do that?

Figuring out solution and writing code.

Lot of ambiguity in problem description, but there is data structure in Python called sets which primary purpose is:

Quote:A set is an unordered collection with no duplicate elements. Basic uses include membership testing and eliminating duplicate entries. Set objects also support mathematical operations like union, intersection, difference, and symmetric difference.

So one can write something like that:

>>> names = ['spam', 'ham', 'eggs', 'bacon']
>>> pairs = [['ham', 'bacon'], ['spam', 'foo']]
>>> for pair in pairs:
...     print(set(pair).issubset(names))
quest likes this post
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to change the datatype of list elements? mHosseinDS86 9 468 Aug-24-2022, 05:26 PM
Last Post: deanhystad
  ValueError: Length mismatch: Expected axis has 8 elements, new values have 1 elements ilknurg 1 1,277 May-17-2022, 11:38 AM
Last Post: Larz60+
  Ldap Search for finding user Groups ilknurg 1 720 Mar-11-2022, 12:10 PM
Last Post: DeaD_EyE
  Why am I getting list elements < 0 ? Mark17 8 1,851 Aug-26-2021, 09:31 AM
Last Post: naughtyCat
  Looping through nested elements and updating the original list Alex_James 3 1,178 Aug-19-2021, 12:05 PM
Last Post: Alex_James
  Extracting Elements From A Website List knight2000 2 1,126 Jul-20-2021, 10:38 AM
Last Post: knight2000
  I cannot delete and the elements from the list quest 4 1,946 May-11-2021, 12:01 PM
Last Post: perfringo
  List of lists - merge sublists with common elements medatib531 1 2,325 May-09-2021, 07:49 AM
Last Post: Gribouillis
  Find Common Elements in 2 list quest 4 1,743 Apr-14-2021, 03:57 PM
Last Post: quest
  Sorting Elements via parameters pointing to those elements. rpalmer 3 1,697 Feb-10-2021, 04:53 PM
Last Post: rpalmer

Forum Jump:

User Panel Messages

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