【问题标题】:Exclude elements in an array which do not contains specific decimals排除数组中不包含特定小数的元素
【发布时间】:2019-10-24 18:10:33
【问题描述】:

我有一个数组,其中包含一位小数。小数点只能是 1、2 或 3(由用户设置,因此不必是算法)。我想创建一个函数来排除数组中不包含所有三个小数的元素。

例如,当考虑以下数组并预期输出时:

a = np.array([1.1, 1.3, 1.2, 2.1, 2.2])
b = np.array([1.3, 1.2, 1.1, 7.3, 7.1, 8.1, 8.2, 8.3])

#desired output
a_usefull = [1.1, 1.3, 1.2]
b_usebull = [1.3, 1.2, 1.1, 8.1, 8.2, 8.3]
a 中的

元素 2.12.2 被排除在外,因为两者都缺少小数点 0.3。元素 7.37.1 被排除在外,因为缺少十进制 .2。请注意,原始数组的顺序是导入的,因此例如 [1.3, 1.1, 1.2] 应该以 [1.3, 1.1, 1.2] 出现


另一个条件是,例如[1.1, 1.3, 2.1, 2.2, 1.2, 2.3] 的输出应该与它所看到的完全一样。因此,例如它是 [1,1,2,1,2,2] 它不应该是 [1,1,1,2,2,2]。顺序不应该改变。

我想先计算数组中的所有元素并计算它们。但是,代码应该在函数中给出。有人可以帮助这个循环吗?

def remove(id):

return useful_elements

谢谢!

【问题讨论】:

  • 有点丑但是[k for k in a if all((int(k) + 0.1*i) in a for i in range(1, 4))]
  • 它们总是按照 x.1、x.2、x.3 的顺序排列吗?
  • 是的,输出数组的顺序应该与原始数组相同。不过,小数不需要按相同的顺序排列。 [2.3, 2.1, 2.2] 没问题。
  • 在这种情况下,您可以创建十进制列表并检查该十进制列表是否是您的输入列表@vectorizinglife 的子列表,该列表也将按顺序排列您的小数,以及您的整数部分下单查看我的答案
  • @vectorizinglife 我的意思是在原始列表中,数字总是按这个顺序排列吗?

标签: python arrays for-loop while-loop


【解决方案1】:

你可以先从列表中找出数字的所有整数部分,用它们组成一个列表,然后检查包含所有三个小数的列表是否存在于更大的列表中

def func(li):

    res = []
    int_li = []

    #Create a list for integers in the input list
    for item in li:
        int_item = int(item)
        if int_item not in int_li:
            int_li.append(int_item)

    #Iterate through the list for integers
    for num in int_li:

        #Create list with all 3 decimals
        check_list = [(num + .1*i) for i in range(1, 4)]
        dec_list = []

        #Iterate through the bigger list
        for number in li:

            #Compare the integer part of element with given integer
            if num == int(number):
                dec_list.append(number)

            #If the found decimal list is present in bigger list with 3 decimals
            if sorted(dec_list) == sorted(check_list):
                #Add it to result and move on to next item
                res.extend(dec_list)
                break

    #Return result
    return res

print(func([1.1, 1.2, 1.3, 2.1, 2.2]))
print(func([1.3, 1.2, 1.1, 7.3, 7.1, 8.1, 8.2, 8.3]))

输出将是

[1.1, 1.2, 1.3]
[1.3, 1.2, 1.1, 8.1, 8.2, 8.3]

【讨论】:

    【解决方案2】:

    这是我的看法:

    # your code goes here
    import numpy as np
    import math
    
    a = np.array([1.1, 1.2, 1.3, 2.1, 2.2])
    b = np.array([1.1, 1.2, 1.3, 7.3, 7.1, 8.1, 8.2, 8.3])
    
    def numpy_filter(arr):
        required_decimals = {1, 2, 3}
        lst = arr.tolist()
        numbers = { math.floor(x) for x in lst }
        fmap = { n: { x for x in lst if math.floor(x) == n } for n in numbers }
        toReturn = []
        for n, decs in fmap.items():
            target_set = { n + x * 0.1 for x in required_decimals }
            if decs == target_set:
                toReturn.extend(target_set)
        return np.array(toReturn)
    
    #desired output
    print(numpy_filter(a))
    print(numpy_filter(b))
    a_usefull = [1.1, 1.2, 1.3]
    b_usebull = [1.1, 1.2, 1.3, 8.1, 8.2, 8.3]
    

    首先我提取底层列表以便于管理它

    lst = arr.tolist()
    

    然后我提取集合中的所有整数部分以避免重复

    numbers = { math.floor(x) for x in lst }
    

    然后我将原始列表划分为一个映射,为每个整数映射原始列表中包含的元素

    fmap = { n: { x for x in lst if math.floor(x) == n } for n in numbers }
    

    之后,我解析地图的所有元素,寻找我在列表中拥有的元素与我应该拥有的元素之间的差异。任何映射的人都会添加到返回的新列表中。

    toReturn = []
    for n, decs in fmap.items():
        target_set = { n + x * 0.1 for x in required_decimals }
        if decs == target_set:
            toReturn.extend(target_set)
    

    这里是executable version

    【讨论】:

      【解决方案3】:

      简单而简短

      a = np.array([1.1, 1.2, 1.3, 2.1, 2.2])
      
      def remove(id):
          useful_elements=np.array([])
          for x in np.unique(a.astype(int)):
               if((x+.1 in a) and (x+.2 in a) and (x+.3 in a)):
                   useful_elements=np.append(useful_elements,(x+.1,x+.2,x+.3))
      
          return useful_elements
      

      【讨论】:

      • 您的代码有效,但它更改了元素的索引号。如果给定 [1.3, 1.1, 1.2] 它会将其更改为 [1.1, 1.2, 1.3] - 我不希望这样,但感谢您提供简单而简短的代码!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-20
      • 2016-11-11
      • 1970-01-01
      • 2020-03-04
      • 2016-10-16
      相关资源
      最近更新 更多