简体   繁体   中英

Searching if a list exists in my python dictionary and append to the associated key

I am pretty new to python and any help will be greatly appreciated. I need to search through a dictionary to confirm if a list exists, if it does, I need to append it as an additional value to the associated key. Here is what I have written:

    _, p = scipy.stats.ks_2samp(k[i],buffer[j])
    if p > alpha:
        for key, value in cluster.iteritems():
            if value == buffer[j]:
                cluster[key].append(i)

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

Can someone help me please. The cluster in the code is a dictionary whose values are lists.

The expression value == buffer[j] returns an array of Booleans. You should not use in an if statement like that. As the error-message suggests you can use one of

if (value == buffer[j]).all() ## true if all elements are equal
if (value == buffer[j]).any() ## true if any of the elements are equal

to avoid this error.

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM