Save all values of a variable (in a loop) in another variable in Python

Question:

I have a code that I inform a folder, where it has n images that the code should return me the relative frequency histogram.

From there I have a function call:

for image in total_images:
    histogram(image)

Where image is the current image that the code is working on and total_images is the total of images (n) it has in the previously informed folder.

And from there I call the histogram() function, sending as a parameter the current image that the code is working.

My histogram() function has the purpose of returning the histogram of the relative frequency of each image (rel_freq).

Although the returned values ​​are correct, rel_freq should be a array 1×256 positions ranging from 0 to 255.

How can I transform the rel_freq variable into a 1×256 array? And each value stored in its corresponding position?

When I do len *rel_freq) it returns me 256, that’s when I realized that it is not in the format I need…

Again, although the returned data is correct…

After that, I need to create an array store_all = len(total_images)x256 to save all rel_freq

I need to save all rel_freq in an array to later save it and to an external file, such as .txt.

I’m thinking of creating another function to do this…

Something like that, but I do not know how to do it correctly, but I believe you will understand the logic…

def store_all_histograms(total_images):
    n = len(total_images)

    store_all = [n][256]

    for i in range(0,n):
        store_all[i] = rel_freq

I know the function store_all_histograms() is wrong, I just wrote it here to show more or less the way I’m thinking of doing… but again, I do not know how to do it properly… At this point, the error I get is:

    store_all = [n][256]
IndexError: list index out of range

After all, I need the store_all variable to save all relative frequency histograms for example like this:

position:     0   ...  256 
store_all = [
            [..., ..., ...],
            [..., ..., ...],
            .
            .
            .
            n
            ]

Now follow this block of code:

def histogram(path):
    global rel_freq

    #Part of the code that is not relevant to the question...

    rel_freq = [(float(item) / total_size) * 100 if item else 0 for item in abs_freq]

def store_all_histograms(total_images):
    n = len(total_images)

    store_all = [n][256]

    for i in range(0,n):
        store_all[i] = rel_freq

#Part of the code that is not relevant to the question...

# Call the functions
for fn in total_images:
    histogram(fn)

    store_all_histograms(total_images)

I hope I have managed to be clear with the question.

Thanks in advance, if you need any additional information, you can ask me…

Asked By: whoisraibolt

||

Answers:

Return the result, don’t use a global variable:

def histogram(path):
    return [(float(item) / total_size) * 100 if item else 0 for item in abs_freq]

Create an empty list:

store_all = []

and append your results:

for fn in total_images:
    store_all.append(histogram(fn))

Alternatively, use a list comprehension:

store_all = [histogram(fn) for fn in total_images]
Answered By: Mike Müller
for i in range(0,n):
    store_all[i+1] = rel_freq

Try this perhaps? I’m a bit confused on the question though if I’m honest. Are you trying to shift the way you call the array with all the items by 1 so that instead of calling position 1 by list[0] you call it via list[1]?

So you want it to act like this?

>>list = [0,1,2,3,4]
>>list[1]
0
Answered By: J0hn

I have a question that how can we store an age inside its id and how can we check it.
when we call id it automatically check the age of that person in C++ language.

Answered By: Saad Zahoor
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.