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 8844993
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:37:41+00:00 2026-06-14T11:37:41+00:00

I thought I was clever in storing the results of a recursive clustering algorithm

  • 0

I thought I was clever in storing the results of a recursive clustering algorithm as a nest of nested tuples. The data stores all relationsips between IDs like this:

((((((((8953L, 3409L), (8334L, 7375L)), ((7375L, 7220L), (8420L, 8556L))), (((7375L, 7220L), (8420L, 8556L)), ((8420L, 8556L), (8556L, 10089L)))), ((((11021L, 11462L), (6778L, 6854L)), ((10691L, 6652L), (11061L, 11230L))), (((6652L, 5660L), (10691L, 6652L)), ((8390L, 6032L), (10984L, 11061L))))), (((((7406L, 11878L), (8398L, 7493L)), ((10419L, 10235L), (6377L, 6439L))), (((8367L, 6199L), (7263L, 7406L)), ((6199L, 7900L), (8367L, 6199L)))), ((((8667L, 9142L), (6491L, 7771L)), ((10391L, 8808L), (8667L, 9142L))), (((10391L, 8808L), (8667L, 9142L)), ((5882L, 9575L), (7008L, 6048L)))))), ((((((11087L, 9623L), (9013L, 9969L)), ((11294L, 9923L), (8390L, 6032L))), (((10656L, 11087L), (11087L, 9623L)), ((11087L, 9623L), (9013L, 9969L)))), ((((6590L, 10794L), (12483L, 6590L)), ((10794L, 8997L), (6590L, 10794L))), (((12386L, 12544L), (8196L, 11139L)), ((11266L, 11269L), (10751L, 12192L))))), (((((11266L, 11269L), (10751L, 12192L)), ((6905L, 8811L), (11180L, 9732L))), (((12338L, 12701L), (12474L, 12569L)), ((9948L, 10073L), (8577L, 10217L)))),
((((8997L, 11091L), (11091L, 11210L)), ((10751L, 12192L), (12543L, 12143L))), (((961L, 12029L), (9262L, 11900L)), ((3825L, 7779L), (10500L, 11781L))))))), (((((((11318L, 10181L), (12334L, 12414L)), ((12292L, 11221L), (11221L, 9262L))), (((12721L, 961L), (11245L, 5132L)), ((12414L, 11245L), (12721L, 961L)))), ((((11248L, 12034L), (9972L, 11248L)), ((10948L, 12013L), (10823L, 5602L))), (((10839L, 10948L), (6673L, 10839L)), ((10729L, 9687L), (1300L, 12274L))))), (((((11087L, 9623L), (9013L, 9969L)), ((11294L, 9923L), (8390L, 6032L))), 
 (((10656L, 11087L), (11087L, 9623L)), ((11087L, 9623L), (9013L, 9969L)))), ((((6590L, 10794L), (12483L, 6590L)), ((10794L, 8997L), (6590L, 10794L))), (((12386L, 12544L), (8196L, 11139L)), ((11266L, 11269L), (10751L, 12192L)))))), ((((((11162L, 9208L), (6992L, 5965L)), ((9208L, 11317L), (10834L, 11318L))), (((12705L, 12769L), (3825L, 7779L)), ((12334L, 12414L), (12769L, 7059L)))), ((((11318L, 10181L), (12334L, 12414L)), ((12292L, 11221L), (11221L, 9262L))), (((12721L, 961L), (11245L, 5132L)), ((12414L, 11245L), (12721L, 961L))))), (((((11318L, 10181L), (12334L, 12414L)), ((12292L, 11221L), (11221L, 9262L))), (((12721L, 961L), (11245L, 5132L)), ((12414L, 11245L), (12721L, 961L)))), ((((11248L, 12034L), (9972L, 11248L)), ((10948L, 12013L), (10823L, 5602L))), (((10839L, 10948L), (6673L, 10839L)), ((10729L, 9687L), (1300L, 12274L)))))))), ((((((((9386L, 8168L), (8876L, 7622L)), ((6311L, 5727L), (7174L, 3611L))), (((8225L, 8804L), (8804L, 6369L)), ((8289L, 8953L), (8225L, 8804L)))), ((((9380L, 7698L), (6450L, 8876L)), ((9386L, 8168L), (8876L, 7622L))), (((9386L, 8168L), (8876L, 7622L)), ((6311L, 5727L), (7174L, 3611L))))), (((((9225L, 9777L), (6895L, 8167L)), ((10686L, 5395L), (12384L, 6816L))), (((5395L, 10211L), (10686L, 5395L)), ((10891L, 10127L), (6816L, 5622L)))), ((((9175L, 7918L), (6780L, 8004L)), ((6780L, 8004L), (10831L, 9175L))), (((6908L, 11020L), (10419L, 10235L)), ((11200L, 9756L), (11021L, 11462L)))))), 
((((((10031L, 8445L), (6165L, 8329L)), ((8445L, 12689L), (10031L, 8445L))), (((5350L, 6189L), (7374L, 5782L)), ((8355L, 7054L), (6536L, 9380L)))), ((((5395L, 10211L), (10686L, 5395L)), ((10891L, 10127L), (6816L, 5622L))), (((8355L, 7054L), (6536L, 9380L)), ((9380L, 7698L), (6450L, 8876L))))), (((((7613L, 11184L), (11184L, 5673L)), ((8929L, 5318L), (8378L, 8929L))), (((10419L, 10235L), (6377L, 6439L)), ((8378L, 8929L), (5363L, 5910L)))), ((((7406L, 11878L), (8398L, 7493L)), ((10419L, 10235L), (6377L, 6439L))), (((8367L, 6199L), (7263L, 7406L)), ((6199L, 7900L), (8367L, 6199L))))))), (((((((8953L, 3409L), (8334L, 7375L)), ((7375L, 7220L), (8420L, 8556L))), (((7375L, 7220L), (8420L, 8556L)), ((8420L, 8556L), (8556L, 10089L)))), ((((11021L, 11462L), (6778L, 6854L)), ((10691L, 6652L), (11061L, 11230L))), (((6652L, 5660L), (10691L, 6652L)), ((8390L, 6032L), (10984L, 11061L))))), (((((7406L, 11878L), (8398L, 7493L)), ((10419L, 10235L), (6377L, 6439L))), (((8367L, 6199L), (7263L, 7406L)), ((6199L, 7900L), (8367L, 6199L)))), 
((((8667L, 9142L), (6491L, 7771L)), ((10391L, 8808L), (8667L, 9142L))), (((10391L, 8808L), (8667L, 9142L)), ((5882L, 9575L), (7008L, 6048L)))))), ((((((11087L, 9623L), (9013L, 9969L)), ((11294L, 9923L), (8390L, 6032L))), (((10656L, 11087L), (11087L, 9623L)), ((11087L, 9623L), (9013L, 9969L)))), ((((6590L, 10794L), (12483L, 6590L)), ((10794L, 8997L), (6590L, 10794L))), (((12386L, 12544L), (8196L, 11139L)), ((11266L, 11269L), (10751L, 12192L))))), (((((11266L, 11269L), (10751L, 12192L)), ((6905L, 8811L), (11180L, 9732L))), (((12338L, 12701L), (12474L, 12569L)), ((9948L, 10073L), (8577L, 10217L)))), 
 ((((8997L, 11091L), (11091L, 11210L)), ((10751L, 12192L), (12543L, 12143L))), (((961L, 12029L), (9262L, 11900L)), ((3825L, 7779L), (10500L, 11781L))))))))

I am now trying to convert this object into a list of edges to visualize with networkx. So each pair of IDs is easy to connect — such as 10500 and 11781. But I also need to connect each nest to its parent, so (10500 and 11781) would each need an edge connection to a upper node that branches to that pair and to (3825, 7779). Am I going about this all wrong?

The best template I found was for flattening any data structure. It at least has some logic about walking through the object that I understand:

def flatten(l, ltypes=(list, tuple)):
""" stolen from http://rightfootin.blogspot.com/2006/09/more-on-python-flatten.html
AKA  Mike C. Fletcher's BasicTypes library"""
ltype = type(l)
l = list(l)
i = 0
while i < len(l):
    while isinstance(l[i], ltypes):
        if not l[i]:
            l.pop(i)
            i -= 1
            break
        else:
            l[i:i + 1] = l[i]
    i += 1
return ltype(l)

to clarify, each upper level has an ID made up of the previous levels. Here are 6 levels for example:

((((((9386L, 8168L), (8876L, 7622L)), ((6311L, 5727L), (7174L, 3611L))), (((8225L, 8804L), (8804L, 6369L)), ((8289L, 8953L), (8225L, 8804L)))), ((((9380L, 7698L), (6450L, 8876L)), ((9386L, 8168L), (8876L, 7622L))), (((9386L, 8168L), (8876L, 7622L)), ((6311L, 5727L), (7174L, 3611L))))), (((((9225L, 9777L), (6895L, 8167L)), ((10686L, 5395L), (12384L, 6816L))), (((5395L, 10211L), (10686L, 5395L)), ((10891L, 10127L), (6816L, 5622L)))), ((((9175L, 7918L), (6780L, 8004L)), ((6780L, 8004L), (10831L, 9175L))), (((6908L, 11020L), (10419L, 10235L)), ((11200L, 9756L), (11021L, 11462L))))))

(((((9386L, 8168L), (8876L, 7622L)), ((6311L, 5727L), (7174L, 3611L))), (((8225L, 8804L), (8804L, 6369L)), ((8289L, 8953L), (8225L, 8804L)))), ((((9380L, 7698L), (6450L, 8876L)), ((9386L, 8168L), (8876L, 7622L))), (((9386L, 8168L), (8876L, 7622L)), ((6311L, 5727L), (7174L, 3611L)))))

((((9386L, 8168L), (8876L, 7622L)), ((6311L, 5727L), (7174L, 3611L))), (((8225L, 8804L), (8804L, 6369L)), ((8289L, 8953L), (8225L, 8804L))))

(((9386L, 8168L), (8876L, 7622L)), ((6311L, 5727L), (7174L, 3611L)))

((9386L, 8168L), (8876L, 7622L))

(9386L, 8168L)

9386L

Generated with this little recursive walker:

def pluck(data_in,out):
    if not isinstance(data_in[0], tuple):
        return out
    print data_in[0]
    out.append(data_in[0])
    pluck(data_in[0],out)

Does that clear it up? I don’t want a flat list – I was a list of connected members like a tree fractal. Suggestions?

UPDATE
Some nice person posted a useful function that kinda worked- but it is missing from SO now, so I reposting. The good part is that it pushes all tuples into one list of tuples. The bad part is that networkx still doesn’t connect all the members in the final map:

def flatten(t, out):
    if isinstance(t[0], tuple):
        for p in t:
            flatten(p, out)
    else:
        out.append(t)
    return out

out = []
out = flatten(data,out)
  • 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-06-14T11:37:43+00:00Added an answer on June 14, 2026 at 11:37 am
    import networkx as nx
    tree = (((((((((8953L, 3409L), (8334L, 7375L)), ((7375L, 7220L), (8420L, 8556L))), (((7375L, 7220L), (8420L, 8556L)), ((8420L, 8556L), (8556L, 10089L)))), ((((11021L, 11462L), (6778L, 6854L)), ((10691L, 6652L), (11061L, 11230L))), (((6652L, 5660L), (10691L, 6652L)), ((8390L, 6032L), (10984L, 11061L))))), (((((7406L, 11878L), (8398L, 7493L)), ((10419L, 10235L), (6377L, 6439L))), (((8367L, 6199L), (7263L, 7406L)), ((6199L, 7900L), (8367L, 6199L)))), ((((8667L, 9142L), (6491L, 7771L)), ((10391L, 8808L), (8667L, 9142L))), (((10391L, 8808L), (8667L, 9142L)), ((5882L, 9575L), (7008L, 6048L)))))), ((((((11087L, 9623L), (9013L, 9969L)), ((11294L, 9923L), (8390L, 6032L))), (((10656L, 11087L), (11087L, 9623L)), ((11087L, 9623L), (9013L, 9969L)))), ((((6590L, 10794L), (12483L, 6590L)), ((10794L, 8997L), (6590L, 10794L))), (((12386L, 12544L), (8196L, 11139L)), ((11266L, 11269L), (10751L, 12192L))))), (((((11266L, 11269L), (10751L, 12192L)), ((6905L, 8811L), (11180L, 9732L))), (((12338L, 12701L), (12474L, 12569L)), ((9948L, 10073L), (8577L, 10217L)))),((((8997L, 11091L), (11091L, 11210L)), ((10751L, 12192L), (12543L, 12143L))), (((961L, 12029L), (9262L, 11900L)), ((3825L, 7779L), (10500L, 11781L))))))), (((((((11318L, 10181L), (12334L, 12414L)), ((12292L, 11221L), (11221L, 9262L))), (((12721L, 961L), (11245L, 5132L)), ((12414L, 11245L), (12721L, 961L)))), ((((11248L, 12034L), (9972L, 11248L)), ((10948L, 12013L), (10823L, 5602L))), (((10839L, 10948L), (6673L, 10839L)), ((10729L, 9687L), (1300L, 12274L))))), (((((11087L, 9623L), (9013L, 9969L)), ((11294L, 9923L), (8390L, 6032L))),  (((10656L, 11087L), (11087L, 9623L)), ((11087L, 9623L), (9013L, 9969L)))), ((((6590L, 10794L), (12483L, 6590L)), ((10794L, 8997L), (6590L, 10794L))), (((12386L, 12544L), (8196L, 11139L)), ((11266L, 11269L), (10751L, 12192L)))))), ((((((11162L, 9208L), (6992L, 5965L)), ((9208L, 11317L), (10834L, 11318L))), (((12705L, 12769L), (3825L, 7779L)), ((12334L, 12414L), (12769L, 7059L)))), ((((11318L, 10181L), (12334L, 12414L)), ((12292L, 11221L), (11221L, 9262L))), (((12721L, 961L), (11245L, 5132L)), ((12414L, 11245L), (12721L, 961L))))), (((((11318L, 10181L), (12334L, 12414L)), ((12292L, 11221L), (11221L, 9262L))), (((12721L, 961L), (11245L, 5132L)), ((12414L, 11245L), (12721L, 961L)))), ((((11248L, 12034L), (9972L, 11248L)), ((10948L, 12013L), (10823L, 5602L))), (((10839L, 10948L), (6673L, 10839L)), ((10729L, 9687L), (1300L, 12274L)))))))), ((((((((9386L, 8168L), (8876L, 7622L)), ((6311L, 5727L), (7174L, 3611L))), (((8225L, 8804L), (8804L, 6369L)), ((8289L, 8953L), (8225L, 8804L)))), ((((9380L, 7698L), (6450L, 8876L)), ((9386L, 8168L), (8876L, 7622L))), (((9386L, 8168L), (8876L, 7622L)), ((6311L, 5727L), (7174L, 3611L))))), (((((9225L, 9777L), (6895L, 8167L)), ((10686L, 5395L), (12384L, 6816L))), (((5395L, 10211L), (10686L, 5395L)), ((10891L, 10127L), (6816L, 5622L)))), ((((9175L, 7918L), (6780L, 8004L)), ((6780L, 8004L), (10831L, 9175L))), (((6908L, 11020L), (10419L, 10235L)), ((11200L, 9756L), (11021L, 11462L)))))), ((((((10031L, 8445L), (6165L, 8329L)), ((8445L, 12689L), (10031L, 8445L))), (((5350L, 6189L), (7374L, 5782L)), ((8355L, 7054L), (6536L, 9380L)))), ((((5395L, 10211L), (10686L, 5395L)), ((10891L, 10127L), (6816L, 5622L))), (((8355L, 7054L), (6536L, 9380L)), ((9380L, 7698L), (6450L, 8876L))))), (((((7613L, 11184L), (11184L, 5673L)), ((8929L, 5318L), (8378L, 8929L))), (((10419L, 10235L), (6377L, 6439L)), ((8378L, 8929L), (5363L, 5910L)))), ((((7406L, 11878L), (8398L, 7493L)), ((10419L, 10235L), (6377L, 6439L))), (((8367L, 6199L), (7263L, 7406L)), ((6199L, 7900L), (8367L, 6199L))))))), (((((((8953L, 3409L), (8334L, 7375L)), ((7375L, 7220L), (8420L, 8556L))), (((7375L, 7220L), (8420L, 8556L)), ((8420L, 8556L), (8556L, 10089L)))), ((((11021L, 11462L), (6778L, 6854L)), ((10691L, 6652L), (11061L, 11230L))), (((6652L, 5660L), (10691L, 6652L)), ((8390L, 6032L), (10984L, 11061L))))), (((((7406L, 11878L), (8398L, 7493L)), ((10419L, 10235L), (6377L, 6439L))), (((8367L, 6199L), (7263L, 7406L)), ((6199L, 7900L), (8367L, 6199L)))), ((((8667L, 9142L), (6491L, 7771L)), ((10391L, 8808L), (8667L, 9142L))), (((10391L, 8808L), (8667L, 9142L)), ((5882L, 9575L), (7008L, 6048L)))))), ((((((11087L, 9623L), (9013L, 9969L)), ((11294L, 9923L), (8390L, 6032L))), (((10656L, 11087L), (11087L, 9623L)), ((11087L, 9623L), (9013L, 9969L)))), ((((6590L, 10794L), (12483L, 6590L)), ((10794L, 8997L), (6590L, 10794L))), (((12386L, 12544L), (8196L, 11139L)), ((11266L, 11269L), (10751L, 12192L))))), (((((11266L, 11269L), (10751L, 12192L)), ((6905L, 8811L), (11180L, 9732L))), (((12338L, 12701L), (12474L, 12569L)), ((9948L, 10073L), (8577L, 10217L)))),  ((((8997L, 11091L), (11091L, 11210L)), ((10751L, 12192L), (12543L, 12143L))), (((961L, 12029L), (9262L, 11900L)), ((3825L, 7779L), (10500L, 11781L)))))))))
    
    
    graph = nx.Graph()
    
    def create_graph(id,tree,graph):
        if id!=0:
            parent = (id-1)/2
            graph.add_edge(parent,id)
        if isinstance(tree[0],tuple) and isinstance(tree[1],tuple):
            create_graph(id*2+1,tree[0],graph)
            create_graph(id*2+2,tree[1],graph)
        else:
            graph.add_edge(id,tree[0])
            graph.add_edge(id,tree[1])
            graph.add_edge(tree[0],tree[1])
    
    create_graph(0,tree,graph)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I thought to be clever and just put an transparent UIButton over an UIImageView
I thought I was clever to switch from the memory intensive DataView to SortedDictionary
I had thought I was clever. But in light of recent discoveries, I'm not
Well when designing this site I thought it would be clever to lay everything
So, I thought I was being clever and using various hashes and permutations of
I thought that I'll be clever and use subquery to get my report in
I thought I would try and be clever and create a Wait function of
OK, I admit I tried to be clever: I thought if I overrode Shape's
Today I was experimenting with Objective-C's blocks so I thought I'd be clever and
I came up with this code, which I thought was rather clever (the requirement

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.