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
#1
Hello,

I have pairs list:
pairs=[[A,B],[C,D],[Z,X]]
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?
Reply
#2
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
Reply
#3
(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))
...
True
False
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  unable to remove all elements from list based on a condition sg_python 3 415 Jan-27-2024, 04:03 PM
Last Post: deanhystad
Question mypy unable to analyse types of tuple elements in a list comprehension tomciodev 1 461 Oct-17-2023, 09:46 AM
Last Post: tomciodev
  Why do I have to repeat items in list slices in order to make this work? Pythonica 7 1,316 May-22-2023, 10:39 PM
Last Post: ICanIBB
  Checking if a string contains all or any elements of a list k1llcod3 1 1,082 Jan-29-2023, 04:34 AM
Last Post: deanhystad
  help me to make my password list in python >>> Oktay34riza 0 572 Dec-23-2022, 12:38 PM
Last Post: Oktay34riza
  How to change the datatype of list elements? mHosseinDS86 9 1,950 Aug-24-2022, 05:26 PM
Last Post: deanhystad
  ValueError: Length mismatch: Expected axis has 8 elements, new values have 1 elements ilknurg 1 5,099 May-17-2022, 11:38 AM
Last Post: Larz60+
  Ldap Search for finding user Groups ilknurg 1 1,748 Mar-11-2022, 12:10 PM
Last Post: DeaD_EyE
  Why am I getting list elements < 0 ? Mark17 8 3,107 Aug-26-2021, 09:31 AM
Last Post: naughtyCat
  Looping through nested elements and updating the original list Alex_James 3 2,111 Aug-19-2021, 12:05 PM
Last Post: Alex_James

Forum Jump:

User Panel Messages

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