Python Forum

Full Version: Get 5 most unique combinations of elements in a 2D list
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
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