简体   繁体   English

比较和乘以列表中的元素

[英]Compare and multiply elements in a list

I'm trying to write in an algorithm a function that:我正在尝试在算法中编写一个函数:

  • Check if all elements in a list are different检查列表中的所有元素是否不同
  • Multiply all elements in the list, except the zeros将列表中的所有元素相乘,除了零

But I can't find a way to compare all elements in one list, do you have any idea ?但是我找不到一种方法来比较一个列表中的所有元素,你有什么想法吗?

Thanks!谢谢!

PS: I use arr = np.random.randint(10, size=a) to create a random list PS:我使用arr = np.random.randint(10, size=a)创建一个随机列表

EDIT:编辑:

More precisely, I'm trying to check if, in a numpy array to be more precise, all the elements are the same or different, if they are different, that it returns me True.更准确地说,我试图在一个 numpy 数组中更精确地检查所有元素是否相同或不同,如果它们不同,它会返回 True。 Also, once that done, multiply all elements in the array except the zeros For example: If I have an array [4,2,6,8,9,0] , the algorithm tells returns me at first True because all elements are different, then it multiplies them 4*2*6*8*9 except the 0此外,一旦完成,将数组中除零以外的所有元素相乘例如:如果我有一个数组[4,2,6,8,9,0] ,算法首先告诉我返回True因为所有元素都不同,然后将它们乘以4*2*6*8*9除了 0

You can use numpy.unique .您可以使用numpy.unique

Following code snippet checks if all elements in the array are unique (different from each other) and if so, it will multiply non-zero values with factor factor :以下代码片段检查数组中的所有元素是否唯一(彼此不同),如果是,则将非零值与因子factor相乘:

import numpy as np

factor = 5

if np.unique(arr).size == arr.size:
    arr[arr != 0] = arr[arr != 0] * factor

To check if all elements in a list are different you can convert the list into a set which removes duplicates and compare the length of the set to the original list.要检查列表中的所有元素是否不同,您可以将列表转换为删除重复项的集合,并将集合的长度与原始列表进行比较。 If the length of the set is different than the length of the list, then there are duplicates.如果集合的长度与列表的长度不同,则存在重复。

x = np.random.randint(10, size=10)
len(set(x)) == len(x)

To multiply all values except 0 you can do list comprehension to remove the 0s and use np.prod to multiply the values in the new list.要将除 0 之外的所有值相乘,您可以执行列表理解以删除 0,并使用 np.prod 将新列表中的值相乘。

np.prod([i for i in x if i != 0])

Example:例子:

x = np.random.randint(10, size=10)
if len(set(x)) == len(x):
    print(np.prod([i for i in x if i != 0]))
else:
    print("array is not unique")

You can use Collections to find the unique numbers.您可以使用集合来查找唯一编号。 I have included a code that solves your problem.我已经包含了解决您的问题的代码。

import numpy as np
from collections import Counter


a = 5
arr = np.random.randint(10, size=a)


result = 1 #Variable that will store the product
flag = 0 #The counter

#Check if all the numbers are unique
for i in Counter(arr):
    if Counter(arr)[i] > 1:
        flag = 1
        break

#Convert the dictionary into a list
l = [i for i in Counter(arr)]

#Return the product of all the numbers in the list except 0
if flag == 0:
    for i in l:
        if i != 0:
            result = result * i
else:
    print("The numbers are not unique")

Just for fun, here's a one-liner:只是为了好玩,这是一个单行:

arr = np.array([1, 2, 3, 4, 0])

np.prod(arr[arr!=0]) if np.unique(arr).size == arr.size else False

>>> 24

If the array is [1, 2, 3, 4, 4] the result is False如果数组是[1, 2, 3, 4, 4]结果为False

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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