1

There are many suggestions but I can't make it stick. The closest solution I have below. The data is like:

my_list = ([1, [2, 3], 4, [5, [6 , [], [8, 9]], 10]], 8)

and a function

def convert(G, l, d):
    z = []
    if isinstance(x, list):
        print(x)
        z.append(convert(G, x, d))
    else:
        print(x)
        z.append([v[d] for n,v in G.nodes(data=True) if n == l])
    return z

execution:

print(convert(G, my_list, "name"))

It gives empty array but print(x) gets the source as is. I'm close I guess. The problem is I don't know how to pass l at if n == l as a integer not list.

EDIT

Outpit: (['a', ['b', 'c'], 'd', ['e', ['f' , [], ['g', 'h']], 'j']], 'g) in the same format (nested). The custom function just takes each element (int) and returns its attribute from dict (letter).

Another try:

my_list = ([1, [2, 3], 4, [5, [6 , [], [8, 9]], 10]], 8)
z = []
def convert(G, l, d):
    z.append([convert(G, x, d) if isinstance(x, list) else [v[d] for n,v in G.nodes(data=True) if n == x]])
    return z

z = convert(G, my_`list, "name")
print(z)

This part is a custom function:

[v[d] for n,v in G.nodes(data=True) if n == l]

so G can be whatever. You can take it as [...]

For example: G.nodes = {0: {'name': 'a'}, 1: {'name': 'b'}, 2: {'name': 'c'}, 3: {'name': 'd'}}

3
  • what's your expected outcome for the given data? Commented Feb 8, 2019 at 16:05
  • Can you make sure you've included an minimal reproducible example? For example, what's G? As @Peter.k mentioned, what do you expect the output to be? Commented Feb 8, 2019 at 16:11
  • So you want a map function for nested lists? Commented Feb 8, 2019 at 16:13

4 Answers 4

5

It seems like you are looking for a kind of map function that works for nested lists. Try this:

def deepmap(f, lst):
    return [deepmap(f, x) if isinstance(x, list) else f(x) for x in lst]

This will convert the outer tuple to another list, though. If you need that to be a tuple, or if there can be more tuples within, it should not be too hard to extend that function accordingly. Example:

>>> my_list = ([1, [2, 3], 4, [5, [6 , [], [8, 9]], 10]], 8)
>>> deepmap(str, my_list)
[['1', ['2', '3'], '4', ['5', ['6', [], ['8', '9']], '10']], '8']

In your case, the mapping function f might be something like this, not entirely sure though. (You might also want to change your G.nodes data structure so you don't have to iterate all the items but can access item x directly.)

f = lambda x: next(v[d] for n,v in G.nodes(data=True) if n == x)
Sign up to request clarification or add additional context in comments.

1 Comment

Yes. This solution works as well. Just needed to pass d argument to deepmap and lambda the rest is as is.
1

A simplified version, following your structure, might look like this:

def v(x):
    return x + 1

def convert(l):
    z = []
    for x in l:
        if isinstance(x, list):
            z.append(convert(x))
        else:
            z.append(v(x))
    return z

This results in the expected nesting:

>>> convert([[1, [2, 3], 4, [5, [6 , [], [8, 9]], 10]], 8])
[[2, [3, 4], 5, [6, [7, [], [9, 10]], 11]], 9]

1 Comment

Yes, thanks. This is oke. I forgot to iterate for x in l:.
0

You just need to make a recursive function to do the convert:

my_list = ([1, [2, 3], 4, [5, [6 , [], [8, 9]], 10]], 8)
mapping = {1: 'a', 2: 'b', 3: 'c', 4: 'd', 5: 'e', 6: 'f', 7: 'g', 8: 'h', 9: 'i', 10: 'j'}

def convert(inlist):
    def transform(x):
        if isinstance(x, list):
            return convert(x)
        else:
            return mapping.get(x)
    return [transform(x) for x in inlist]
print(convert(my_list))

Comments

0

Define a custom mapper

import string
def mapper(item):
    if not isinstance(item,list):
        return d[item]
    else:
        return [mapper(i) for i in item]

d=dict(zip(range(1, len(string.ascii_lowercase) + 1), string.ascii_lowercase))

Mapping dict will look like this

{1: 'a', 2: 'b', 3: 'c', 4: 'd', 5: 'e', 6: 'f', 7: 'g', 8: 'h', 9: 'i', 10: 'j', 11: 'k', 12: 'l', 13: 'm', 14: 'n', 15: 'o', 16: 'p', 17: 'q', 18: 'r', 19: 's', 20: 't', 21: 'u', 22: 'v', 23: 'w', 24: 'x', 25: 'y', 26: 'z'}

my_list = ([1, [2, 3], 4, [5, [6 , [], [8, 9]], 10]], 8)
tuple([mapper(i) for i in my_list])

You can also use map function with custom mapper

tuple(map(mapper,my_list))

Output

(['a', ['b', 'c'], 'd', ['e', ['f', [], ['h', 'i']], 'j']], 'h')

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.