1

I have nested functions that are meant to partition the indices of a list of lists, corresponding to elements (lists) that are the same.

My first attempt wasn't returning the list I needed from an inside function. i thought it was because the output list was created inside the function only. So I modified the script. I now define my output list, liketissuesets, exernally. It is passed to, and modified by, the internal functions. And yet, the correctly modified list is not passing from the function ind_list_renew to the function find_like_tissues_set!

#!/usr/bin/env python

import numpy as np

l1 = [1,2,3]

l2=[2,3]

l3=[1,2,3]

l4=[2,3,4]

l5=[2,3]

mylist = [l1,l2,l3,l4,l5]
liketissuesets = []

def listidentity(v,b,f):
    if len(v) != len(b):
        return []
    else:
        for j in range(len(b)):
            if v[j]!=b[j]:
                return []
            else:
                return [f]


def ind_list_renew(changinglist, liketissuesets):
    a=changinglist[0]
    b=mylist[a]
    common = []

    for f,v in enumerate(mylist):
        common = common + listidentity(v,b,f)

    print(common)
    liketissuesets = liketissuesets + [common]
    print(liketissuesets)
    changinglist = changinglist.tolist()
    indtodelete = [j for j,k in enumerate(changinglist) if k in common]
    changinglist = np.delete(changinglist, indtodelete)

    if len(changinglist) != 0:
        ind_list_renew(changinglist, liketissuesets)
    else:
        print('yay', liketissuesets)
        return liketissuesets


def find_like_tissues_set(mylist, liketissuesets):

    indoriginal = np.arange(len(mylist))
    c=ind_list_renew(indoriginal, liketissuesets)
    print(c)
    return c

b=find_like_tissues_set(mylist, liketissuesets)

print(b)
3
  • Your recursive call should be: return ind_list_renew(changinglist, liketissuesets). Commented Aug 9, 2019 at 9:56
  • @quamranaI have changed one function to def find_like_tissues_set(mylist, liketissuesets): return ind_list_renew(np.arange(len(mylist)), liketissuesets) But I still get None return! Commented Aug 9, 2019 at 10:01
  • That's not the recursive call. See my answer below. Commented Aug 9, 2019 at 10:03

1 Answer 1

1

Really, you don't need the global liketissuesets. You can restructure your code like this:

def ind_list_renew(changinglist, liketissuesets):
    # stuff omitted

    if len(changinglist) != 0:
        return ind_list_renew(changinglist, liketissuesets)
    else:
        print('yay', liketissuesets)
        return liketissuesets


def find_like_tissues_set(mylist):
    indoriginal = np.arange(len(mylist))
    c = ind_list_renew(indoriginal, [])
    print(c)
    return c

b = find_like_tissues_set(mylist)

print(b)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.