Python Forum
Get 5 most unique combinations of elements in a 2D list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Get 5 most unique combinations of elements in a 2D list
#1
Hi,
I have a problem with this particular question. I will have randomly generated 2D lists e.g.: list=[[2],[0,3],[0,1],[1,2,4,5],[1,6,10],[],[7,8],[],[],[8],[9]] and each index of the element in this 2d list can be seen as a social media user's ID. So index 0 in list which corresponds to element [2] would be seen as USER 0 and index 1 in that list which corresponds to element [0,3] would be seen as USER 1. The elements can be interpreted such that for USER 0 which corresponds to the 0 position in List and is element [2], it means that USER 2 follows USER 0.

The question wants me to select 5 users from a randomly generated 2D list to make a post such that from those 5 users, the posts will be seen by the most number of unique users (the selected 5 users are also counted as having viewed the post). Currently, I can only figure out how to get the first selected user by appending the 2D list and adding a number at the start of each element which corresponds to its index to use as the USER ID. So my example list on top would look something like list=[[0,2],[1,0,3]...[10,9]]. Then I sorted the list by length and in descending order so the USER with the most followers would be in the first position. From there onwards, I would append an empty list with the USER ID in the sorted list to get the USER with the most followers in that list. This is because after sorting in descending order, the first positioned USER in that new list will have the highest amount of followers.

However, I am unsure on how to proceed to get the next 4 users which will give me the most unique total number of users who will view the post made by the 5 selected members. Can anyone help me? I've been stuck on this for a while.

My current(incomplete) code looks like this:
def select_user(followers):
  newList=[]
  i=o
  while i<len(followers):
    followers[i].insert(0,i)
    i+=1
  followers.sort(key=len,reverse=True)
  newList.append(followers[0][0])
  return newList
Reply
#2
look at itertools.combinations and note that there might be repeating followers among the 5 users (i.e. you need to use set in order to get unique followers). Then you need to find combinations with the largest number of unique followers
This is brute-force approach and it may take some time if the list is really long
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to sort a list with duplicates and return their unique indices. Echoroom 3 3,355 Sep-23-2022, 07:53 AM
Last Post: deanhystad
  help with adding duplicates elements together in a list 2ECC3O 5 1,972 Sep-10-2022, 07:11 AM
Last Post: 2ECC3O
  Unexpected behavior accessing list elements. tonyflute 2 2,222 Apr-09-2021, 02:36 PM
Last Post: tonyflute
  How to find difference between elements in a list? Using beginner Basics only. Anklebiter 8 4,256 Nov-19-2020, 07:43 PM
Last Post: Anklebiter
  Loop through elements of list and include as value in the dictionary Rupini 3 2,588 Jun-13-2020, 05:43 AM
Last Post: buran
  How can I print the number of unique elements in a list? AnOddGirl 5 3,196 Mar-24-2020, 05:47 AM
Last Post: AnOddGirl
  Extracting elements in a list to form a message using for loop Tony04 2 2,325 Oct-25-2019, 05:55 PM
Last Post: ichabod801
  Sum of elements on the list recursive sev 3 2,517 Jun-20-2019, 02:14 PM
Last Post: sev
  Combinations mnnewick 1 2,572 Dec-17-2018, 02:35 PM
Last Post: ichabod801
  reach the elements in the list ptah 7 4,965 Oct-04-2017, 08:12 PM
Last Post: ptah

Forum Jump:

User Panel Messages

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