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
  Why am I getting list elements < 0 ? Mark17 8 607 Aug-26-2021, 09:31 AM
Last Post: naughtyCat
  Looping through nested elements and updating the original list Alex_James 3 349 Aug-19-2021, 12:05 PM
Last Post: Alex_James
  Extracting Elements From A Website List knight2000 2 461 Jul-20-2021, 10:38 AM
Last Post: knight2000
  I cannot delete and the elements from the list quest 4 920 May-11-2021, 12:01 PM
Last Post: perfringo
  List of lists - merge sublists with common elements medatib531 1 968 May-09-2021, 07:49 AM
Last Post: Gribouillis
  Find Common Elements in 2 list quest 4 864 Apr-14-2021, 03:57 PM
Last Post: quest
  Sorting Elements via parameters pointing to those elements. rpalmer 3 876 Feb-10-2021, 04:53 PM
Last Post: rpalmer
  Group List Elements according to the Input with the order of binary combination quest_ 19 1,930 Jan-28-2021, 03:36 AM
Last Post: bowlofred
Question How to make a 3D List of Excel Spreadsheets? chatguy 4 840 Jan-24-2021, 05:24 AM
Last Post: buran
  Understanding Regex Groups matt_the_hall 5 1,099 Jan-11-2021, 02:55 PM
Last Post: matt_the_hall

Forum Jump:

User Panel Messages

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