3

If I have two lists, both of which contain multidimensional Numpy arrays, how can I see if an array in one list is in the other?

If I have two lists, both of which contain elements, I can search through one list (element-by-element) to determine if it is in the second list, like so:

all_numbers = [1,2,3,4,5,6,7,8,9,10]
even_numbers = [2,4,6,8,10]

for i in all_numbers:
    if i in even_numbers:
        print(i,'is an even number.')
    else:
        print(i,'is an odd number.')

Output:

1 is an odd number.
2 is an even number.
3 is an odd number.
4 is an even number.
5 is an odd number.
6 is an even number.
7 is an odd number.
8 is an even number.
9 is an odd number.
10 is an even number.

Now if I have two lists, both of which contain only multidimensional arrays, why can I not search if an array in one list is in the other using the same method? The arrays are all multidimensional, and generated from data files (using the urllib and numpy modules). As far as I understand, the arrays in the lists are simply elements, so using the same method should yield the same result; however, when I try to execute my program, I get the following message:

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

Why is this occurring? Clearly, using multidimensional arrays is not equivalent to simple elements in lists. Is there a way to achieve what I want (that is, seeing if an array in one list is in another)?


[The following is an example of my program. I am using several data files, all of which are multidimensional arrays. To keep the program general, I have removed the actual URLs. The data files are organized by year (from 1981-1990), so the files have the same amount of information, except for leap years, which have an extra day's worth of data. My two lists are one which has all of the files, and one which has the files for the leap years. Please let me know if there is any other information that is needed.]

import numpy as np
import urllib.request

data1981url = '1981url'
data1981 = np.genfromtxt(data1981url)
data1982url = '1982url'
data1982 = np.genfromtxt(data1982url)
data1983url = '1983url'
data1983 = np.genfromtxt(data1983url)
data1984url = '1984url'
data1984 = np.genfromtxt(data1984url)
data1985url = '1985url'
data1985 = np.genfromtxt(data1985url)
data1986url = '1986url'
data1986 = np.genfromtxt(data1986url)
data1987url = '1987url'
data1987 = np.genfromtxt(data1987url)
data1988url = '1988url'
data1988 = np.genfromtxt(data1988url)
data1989url = '1989url'
data1989 = np.genfromtxt(data1989url)
data1990url = '1990url'
data1990 = np.genfromtxt(data1990url)

years = [data1981, data1982, data1983, data1984, data1985, data1986, data1987, data1988,
data1989, data1990]
leapyears = [data1984, data1988]

years_by_day = []
flux_data = []
for i in years:
    y = i[:,46]
    for k in y:
        flux_data.append(k)
    if i in leapyears: # Where the problem arises.
        x = i[0,0] + np.arange(len(y))/105480
        for j in x:
            years_by_day.append(j)
    else:
        x = i[0,0] + np.arange(len(y))/105120
        for j in x:
            years_by_day.append(j)

Output:

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
Guest
  • 39
  • 2
  • Print `k in y`; I bet it is a boolean array. That cannot be used with `if`. In general `in` does not work when testing arrays. – hpaulj Dec 04 '16 at 04:17
  • See if this helps - http://stackoverflow.com/questions/38674027/find-the-row-indexes-of-several-values-in-a-numpy-array – Divakar Dec 04 '16 at 07:29

1 Answers1

0

Comparing two numpy arrays like [1 2 3] and [1 2 1] using the eq operator == returns an ndarray of boolean value corresponding to [True True False] in this case.

So, it would be ambiguous for the if .. in .. statement to understand whether you want all the elements to be equal or just any (one) of them.

What I suggest is manually checking:

i_in_leapyears = False
for ly in leapyears:
    if (ly == i).all(): # this requires all the array elements to be equal
        i_in_leapyears = True
        break

if i_in_leapyears: 
        x = i[0,0] + np.arange(len(y))/105480
        for j in x:
            years_by_day.append(j)
    else:
        x = i[0,0] + np.arange(len(y))/105120
        for j in x:
            years_by_day.append(j)