【问题标题】:define a function to check if a list of elements is palindrome and returns a list定义一个函数来检查元素列表是否为回文并返回一个列表
【发布时间】:2021-06-07 15:52:43
【问题描述】:

我必须做到以下几点:

定义一个名为 isSymmetricalVec 的函数,该函数接受一个元素列表,检查列表中的每个元素是否是回文,然后在列表中返回它们的结果。例如,给定["1441", "Apple", "radar", "232", "plane"],函数返回[TRUE, FALSE, TRUE, TRUE, FALSE]

我编写了以下代码,但我无法在列表中返回结果。

def isSymmetricalVec(myList): 
    
    for myString in myList:
        
        myList = []
   
        mid = (len(myString)-1)//2
        start = 0
        last = len(myString)-1
        flag = 0
  
        while(start<mid): 
   
            if (myString[start]== myString[last]): 
              
                start += 1
                last -= 1
              
            else: 
                flag = 1
                break; 
              
        if flag == 0: 
            print(bool(1)) 
        else: 
            print(bool(0)) 

# Enter a list of strings to check whether it is symmetrical or not 
myList = ["12321", "12345", "madam", "modem"]
isSymmetricalVec(myList)

我的函数返回以下内容,但结果不是列表格式:

True
False
True
False

如何修改我的代码以返回列表格式的结果?

【问题讨论】:

  • .appendmyList 而不仅仅是 printing
  • 你知道如何从函数中返回值吗?
  • 另外,bool(1)flag = 1 非常“C-like”。直接使用TrueFalse就行了

标签: python list function boolean palindrome


【解决方案1】:

你的函数应该return 值而不是打印它。 为它创建了一个空列表new_listappended 结果。

def isSymmetricalVec(myList):
    new_lst = []
    for myString in myList:

        mid = (len(myString) - 1) // 2
        start = 0
        last = len(myString) - 1
        flag = 0

        while (start < mid):

            if (myString[start] == myString[last]):

                start += 1
                last -= 1

            else:
                flag = 1
                break;

        if flag == 0:
            new_lst.append(True)
        else:
            new_lst.append(False)

    return new_lst
        # Enter a list of strings to check whether it is symmetrical or not

【讨论】:

    【解决方案2】:

    你的函数实际上并没有返回任何东西。您看到的只是打印,而不是退回

    您可能希望像这样将每个答案保存在 list 中。

    def isSymmetricalVec(myList):
        list_of_answers = []
        
        for myString in myList:
            ...
      
            while(start<mid): 
                ...
                  
            if flag == 0: 
                print(bool(1))
                list_of_answers.append(True)
            else: 
                print(bool(0)) 
                list_of_answers.append(False)
    
        return list_of_answers
    

    这样你的答案就会被打印出来并返回。

    最后,您需要一个变量来保存返回的 list

    # Enter a list of strings to check whether it is symmetrical or not 
    myList = ["12321", "12345", "madam", "modem"]
    list_of_answers = isSymmetricalVec(myList)
    

    【讨论】:

      【解决方案3】:
      def isSymmetricalVec(myString): 
      
          mid = (len(myString)-1)//2
          start = 0
          last = len(myString)-1
          flag = 0
      
          while(start<mid): 
      
              if (myString[start]== myString[last]): 
                  
                  start += 1
                  last -= 1
                  
              else: 
                  flag = 1
                  break; 
                  
          if flag == 0: 
              return True
          else: 
              return False
      
      
      test = ["1221", "madam", "hello world"]
          
      final = [isSymmetricalVec(i) for i in test]
      print(final)
      

      我稍微重写了代码,这将是我的解决方案。它保留了原有的功能,既时尚又高效。它还使原始功能更加灵活,易于迁移。

      【讨论】:

        【解决方案4】:

        您的函数不返回任何内容,只是打印值。 有一种使用 python slices 缩短算法的好方法。此函数还返回布尔对象列表。

        from math import ceil, floor
        
        def isSymmetricalVec(myList):
            list_of_answers = []
            for string in myList:
                strLength = len(string)
                #There is a string palindrome check
                if string[:floor(strLength//2)] == string[ceil(strLength/2):][::-1]:
                    toAppend = True
                else:
                    toAppend = False
                list_of_answers.append(toAppend)
            
            return list_of_answers
        

        值得补充的是,最好使用TrueFalse 而不是bool(1)bool(0)

        一个例子:

        >>> isSymmetricalVec(['11211', 'AbbA', '12f23'])
        >>> [True, True, False]
        

        【讨论】:

          猜你喜欢
          • 2015-05-07
          • 2022-11-13
          • 2022-01-15
          • 1970-01-01
          • 2016-06-21
          • 2013-01-04
          • 1970-01-01
          • 1970-01-01
          • 2012-11-09
          相关资源
          最近更新 更多