Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7568839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:01:27+00:00 2026-05-30T15:01:27+00:00

I am having trouble implementing a python algorithm which does the following: (This is

  • 0

I am having trouble implementing a python algorithm which does the following:
(This is part of an attempt to implement a friend of friend algorithm)

Given a list of the form [[a,b],[c,d],[e,f],…]
I want to create a new list of the form [[a, a1,a2,a3,…], [b, b1,b2,b3,…], [c, c1,c2,c3,…],…].

An example to make this clearer is something like the following: given a list
[[0,1], [0,4], [0,3], [0,423], [1,232], [1,2], [2,444], [2,12]]

I want the output to group all the elements with the first integer so the output would be
[[0, 1,4,3,432],[1, 232,2], [2, 444,12]]

Notes: I have sorted the input list according to the first element in each item.

I have been stumped on how to implement this in a somewhat efficient manner for some time now, and would love to get some advice/suggestions as to how to implement this.

P.S. Ultimately I want this to combine all “liked” terms. What i mean is taking the above example, instead of getting the output [[0, 1,4,3,432],[1, 232,2], [2, 444,12]] I would get
[[0, 1,4,3,432],[1, 232,2, 444,12]], where the “2” term and its shared elements have joined the elements associated with the “1” term since 1 is associated with 2. This last part may be confusing, but if it makes sense advice would be welcomed as well! Otherwise ignore this last part. =] Thanks again!

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T15:01:28+00:00Added an answer on May 30, 2026 at 3:01 pm

    Try this:

    from collections import defaultdict
    
    friends = defaultdict(set)
    friendpairs =  [[0,1], [0,4], [0,3], [0,432], [1,232], [1,2], [2,444], [2,12]]
    
    for f1,f2 in friendpairs : friends[f1].add(f2)
    
    friendOfFriends = dict( (guy,fr.copy()) for guy,fr in friends.iteritems())
    
    for f1 in friendOfFriends:
        for f2 in friends[f1]:
            friendOfFriends[f1].update(friends[f2])
    

    UPD: You could also replace last line with

            friendOfFriends[f1].update(friends.get(f2,()))
    

    to prevet appearing of empty sets in friends collection

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having trouble implementing this step: Given I am logged in as a Facebook
I'm having trouble implementing this jQuery plugin for dynamic forms while following this tutorial
I'm having trouble implementing a script which should parse a json string from a
I am having trouble implementing the bootstrap carousel. Can anyone look at the following
I am having trouble implementing ADBannerView and its delegate protocol. I implemented the following
Related directly to this post , I am having trouble implementing some sound advice
I'm having a lot of trouble implementing this deque using a circular array; in
I realize this question already exists, but I am having trouble implementing the solutions.
I am having trouble implementing pagination onto a list of products held within a
I am having trouble implementing sorting on a list when DateTime is involved. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.