jonboy
jonboy

Reputation: 368

Apply function to list of lists - Python

I'm aiming to pass a list of lists to a function iteratively but I'm returning an error. The code below creates a random list of lists. Once generated, it gets passed to a separate function.

So the list of lists is named mult_samples. I'm hoping to pass this to a function.

But I can't pass the whole list of lists. How can I iterate over all sublists and return the output as an array or df?

import pandas as pd
import random
import numpy as np

df = pd.DataFrame({                    
        'X' : [1.0,3.0,2.0,2.0,4.0,3.0,3.0,4.0,4.0],
    })

def randomSamples():

    A = df['X']

    n = len(df['X'])

    maxv = int(A.max())
    minv = int(A.min())

    one_sample = []

    for x in range(0, n):

        one_sample.append(random.randint(minv, maxv))

    samples = [one_sample for _ in range(10)]

    return samples

mult_samples = randomSamples()

Subset out list1 and list2

Out:

mult_samples[0] = [3, 2, 3, 3, 4, 2, 3, 3, 3]

mult_samples[1] = [1, 4, 1, 3, 3, 4, 3, 4, 2]

def func(U, m = 2, r = 0.2):

    def _maxdist(x_i, x_j):
        return max([abs(ua - va) for ua, va in zip(x_i, x_j)])

    def _phi(m):
        x = [[U[j] for j in range(i, i + m - 1 + 1)] for i in range(N - m + 1)]
        C = [len([1 for x_j in x if _maxdist(x_i, x_j) <= r]) / (N - m + 1.0) for x_i in x]
        return (N - m + 1.0)**(-1) * sum(np.log(C))

    N = len(U)

    return abs(_phi(m + 1) - _phi(m))

output_mult_samples = func(mult_samples)

#output_mult_samples = list(map(funclist, mult_samples))

print(output_mult_samples)

Out

mult_samples[0] = 0.253692959177
mult_samples[1] = 0.0397554025155

Upvotes: 0

Views: 808

Answers (2)

Jarvis
Jarvis

Reputation: 8564

From what I infer from your question, assuming you want to apply func to every list inside your list, you can proceed like this :

# apply the function 'func' to every LIST inside the main list
output_mult_samples = list(map(func, mult_samples))

EDIT: After getting func definition in question, you need only map that to the list of lists.

Also, you need to fix the function randomSamples. All you are doing as of now is putting the same one_sample list 10 times in your samples. You should do something like this instead:

samples = []

for _ in range(10):
    one_sample = []
    for x in range(0, n):
        one_sample.append(random.randint(minv, maxv))
    samples.append(one_sample)

Upvotes: 1

GeneralCode
GeneralCode

Reputation: 964

You have a lot of options about how you want to handle the data. An example of how to loop through nested lists is below.

for i in range(len(mult_samples)):
    print('Here we are in a list: ' + str(mult_samples[i]))
    for j in range(len(mult_samples[i])):
        print('Access location ' + str(j) + 'in list ' + str(i) + ': ' + str(mult_samples[i][j]))

To make a list into an array np.array(some list).

To make a python dictionary into a data frame pd.DataFrame(some dict).

Upvotes: 1

Related Questions