0

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
4
  • 1
    Can you please be little more clear Commented Feb 7, 2020 at 5:03
  • @Jeril, how about now? Commented Feb 7, 2020 at 5:20
  • Can you give more information about func? What is the nature of parameters? What did you expect to happen and what happened instead? Commented Feb 7, 2020 at 5:28
  • what does func do? can you share its code Commented Feb 7, 2020 at 5:56

2 Answers 2

1

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)
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks @Jarvis. I've included the function. I think I need to change the datatype.
I've updated the question @Jarvis. I'm actually drawing an error with the updated answer.
Thanks Jarvis. This is now returning values but it seems to be using a single list still. I am export 10 random lists to the secondary function, which should return 10 different values. But they are all identical? Does that make sense?
That's because your mult_samples is not really random. Try printing that and you will find that all the lists inside this list are the SAME. @jonboy
Did you check if the lists in mult_samples are same or not? I don't think they are and I provided the fix in my updated answer. @jonboy
|
1

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).

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.