【问题标题】:How do I check the list elements satisfy a given condition?如何检查列表元素是否满足给定条件?
【发布时间】:2021-05-31 19:18:26
【问题描述】:

我有一个数字列表,其中任何两个相邻数字的总和是一个完美的平方。 列表为 x=[1,8,28,21,4,32,17,19,30,6,3,13,12,24]

for i in range(len(x)-1):
    y= x[i]+x[i+1]
    z=y**0.5
    #till here found the square root of the sum of the adjacent numbers in list
    if(z.is_integer==True):
        //code

我想检查列表中的剩余数字。如果列表的所有元素都满足条件。然后我想打印列表

预期的输出应该是

[1,8,28,21,4,32,17,19,30,6,3,13,12,24] satisfies the condition

【问题讨论】:

  • 你能澄清更多吗?如果你已经找到完美的正方形,那么将 X[i], X[i+1] 追加到新列表中,当 for 循环结束时,打印新列表。
  • is_integer是一个函数,应该是is_integer()

标签: python list


【解决方案1】:

也许是这样的?创建将要为列表调用的函数,如果列表满足条件则返回 True,否则返回 False。

def some_function(nums):
   for i in range(len(nums) - 1):
      y = nums[i] + nums[i + 1]
      z = y ** 0.5
      #till here found the square root of the sum of the adjacent numbers in list
      if z.is_integer() not True:
         # if there is some two numbers that don't meet condition, function will return False
         return False
   return True

你这样称呼它:meet_condition = some_function(x) 之后只需检查它是否为 True 以及是否为打印列表和适当的文本。

【讨论】:

    【解决方案2】:

    您可以使用这种方法。我相信有更好的方法,代码行数更少。玩得开心!

    numbers = [1,8,28,21,4,32,17,19,30,6,3,13,12,24]
    
    for i in range(len(numbers)):
        if i < len(numbers) - 1:
            if ((numbers[i] + numbers[i+1]) ** 0.5) % 1 == 0:
                continue
            else:
                print("Does not satisfy condition")
                break
        print(numbers)
    

    输出:

    [1, 8, 28, 21, 4, 32, 17, 19, 30, 6, 3, 13, 12, 24]
    

    【讨论】:

      【解决方案3】:
      import math
      
      x=[1,8,28,21,4,32,17,19,30,6,3,13,12,24]
      
      b = [x[i] for i in range(0,len(x)-1) if math.sqrt(x[i]+x[i+1]).is_integer()]
      
      if (b[-1] == x[-2]):
          b.append(x[-1])
      
      print(b)
      

      输出:[1、8、28、21、4、32、17、19、30、6、3、13、12、24]

      【讨论】:

        【解决方案4】:

        我建议您查看检查整数是否为正方形的方式:

        y = 9
        z = y ** 0.5
        z #=> 3.0
        z.is_integer==True #=> False
        

        所以,9 似乎不是正方形。

        is_integer 应该是 is_integer() 没有检查。

        请参考这个话题例如:Check if a number is a perfect square


        为了解决问题,我建议拆分问题:

        1. 定义一个从列表中返回连续对的方法;
        2. 定义一个检查数字是否为正方形的方法;
        3. 使用all(iterable) 函数将其放在一起。

        首先是获取连续元素的方法,在本例中是生成器:
        def each_cons(iterable, n = 2):
            if n < 2: n = 1
            i, size = 0, len(iterable)
            while i < size-n+1:
                yield iterable[i:i+n]
                i += 1
        

        鉴于您的列表x=[1,8,28,21,4,32,17,19,30,6,3,13,12,24],您可以这样称呼它:

        list(each_cons(x));
        # => [[1, 8],[8, 28],[28, 21],[21, 4],[4, 32],[32, 17],[17, 19],[19, 30],[30, 6],[6, 3],[3, 13],[13, 12],[12, 24]]
        

        然后是正方形的方法,我已经stolen here

        def is_square(apositiveint):
          x = apositiveint // 2
          seen = set([x])
          while x * x != apositiveint:
            x = (x + (apositiveint // x)) // 2
            if x in seen: return False
            seen.add(x)
          return True
        

        最后把所有东西放在一起:
        all(is_square(a + b) for a, b in each_cons(x))
        #=> True
        

        【讨论】:

          【解决方案5】:

          pythonic 应该是这样的。

          import numpy as np
          import functools
          nums = np.array([1,8,28,21,4,32,17,19,30,6,3,13,12,24])
          result = functools.reduce(lambda a,b: a and b, map(lambda x : (x ** 0.5).is_integer(), nums[1:] + nums[:-1]))
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2013-03-06
            • 2018-05-09
            • 1970-01-01
            • 2017-06-20
            • 2021-12-07
            • 2012-10-01
            • 1970-01-01
            • 2022-06-13
            相关资源
            最近更新 更多