Python 检查由数组元素组合而成的数字是否是回文数
在Python中,由逗号分隔的数值项组成的数组(列表)被认为是最灵活的数据类型之一。列表的元素可以是不同的数据类型,这一点非常重要。
在本文中,我们将学习一个Python程序,用于检查由数组元素组合而成的数字是否是回文数。
使用的方法
以下是实现此任务的各种方法:
- 使用map()和join()函数
- 使用类型转换和字符串连接
- 使用str()、list()、extend()、join()和reverse()函数
示例
假设我们有一个 输入列表 。我们将使用上述方法检查由列表所有元素组合而成的数字是否是回文数。
输入
inputList = [3, 25, 42, 24, 52, 3]
输出
Yes, it is a Palindrome
在上面的例子中,所有输入列表元素被组合成一个字符串形式的单个数字,即 3254224523 。这个数字是一个回文数。因此,输出是一个回文数。
使用map()和join()函数
str()函数 (返回对象的字符串格式,即转换为字符串)。 join()函数 - join()是Python中的一个字符串函数,用于连接以字符串分隔的序列元素。该函数将序列元素连接起来转换为一个字符串。 list()函数 (将序列/可迭代对象转换为列表)。 map()函数 - 对迭代器/列表映射任何函数。
示例
以下程序使用map()和join()函数检查通过组合数组中的所有元素形成的数字是否是一个回文数。
# creating a function to check whether the string passed to it
# is a palindrome or not
def checkingPalindrome(inputString):
# reversing the input string
reverseStr = inputString[::-1]
# checking whether the input string is equal to its reverse string
if(inputString == reverseStr):
# returning True if the condition is true
return True
else:
# else returning False
return False
# creating a function to convert the input list into a string
# by accepting the input list as an argument to it.
def joiningList(inputList):
# converting all the input list elements into a string
inputList = list(map(str, inputList))
# converting the input list to a string using the join() function
resultNum = ''.join(inputList)
# checking whether the above-converted string number is a palindrome
# by calling the above defined checkingPalindrome() function
if(checkingPalindrome(resultNum)):
# returning True if the function returns True
return True
else:
# else returning False
return False
# main code
# input list
inputList = [3, 25, 42, 24, 52, 3]
# Calling the above defined joiningList() function by passing input list to it
# and checking whether the function returns True/False
if(joiningList(inputList)):
# printing Palindrome if the joiningList() fuction returns True
print("Yes, it is a Palindrome")
else:
# else printing NOT a Palindrome
print("No, it is NOT a Palindrome")
输出
执行上述程序后,将生成以下输出:
Yes, it is a Palindrome
使用类型转换和字符串连接
以下程序使用类型转换和字符串连接来检查通过组合数组的所有元素形成的数字是否是回文数-
示例
# creating a function to check whether the string passed to is
# palindrome or not
def checkingPalindrome(inputString):
# reversing the input string
reverseStr = inputString[::-1]
# checking whether the input string is equal to its reverse string
if(inputString == reverseStr):
# returning True if the condition is true
return True
else:
# else returning False
return False
# creating a function to convert the input list into a string
# by accepting the input list as an argument to it.
def joiningList(inputList):
# Creating an empty string for storing list elements as a number
resultNum = ""
# travsering through the input list
for e in inputList:
# converting each element of list into a string
e = str(e)
# adding the string element to the resultNum
resultNum = resultNum + e
# checking whether the above-converted string number is a palindrome
# by calling the above defined checkingPalindrome() function
if(checkingPalindrome(resultNum)):
# returning True if the function returns True
return True
else:
# else returning False
return False
# input list
inputList = [3, 25, 42, 24, 52, 3]
# Calling the above defined joiningList() function by passing input list to it
# and checking whether the function returns True/False
if(joiningList(inputList)):
# printing Palindrome if the joiningList() fuction returns True
print("Yes, it is a Palindrome")
else:
# else printing NOT a Palindrome
print("No, it is NOT a Palindrome")
输出
执行上述程序后,将生成以下输出:
Yes, it is a Palindrome
使用str(),list(),extend(),join()和reverse()函数
extend()函数 (将可迭代的元素(如list、tuple、string等)全部添加到列表的末尾)
reverse()函数 - 一个内置函数,用于反转列表对象。它只是改变原始列表,而不会占用额外的空间。
以下程序使用类型转换和字符串拼接来检查由数组中所有元素组合形成的数字是否是回文数-
示例
# input list
inputList = [3, 25, 42, 24, 52, 3]
# an empty string for storing list elements as a number
resultantStr = ""
# travsering through the input list
for e in inputList:
# converting each element of the list as a string and adding it to the result string
resultantStr += str(e)
# converting the result string into a list
newList = list(resultantStr)
# empty list
k = []
# extending the new list to the above empty list
k.extend(newList)
# reversing the new list
newList.reverse()
# checking whether the new list is equal to the extended list
if(newList == k):
# printing Palindrome if the condition is True
print("Yes, it is a Palindrome")
else:
# Else printing NOT a Palindrome
print("No, it is NOT a Palindrome")
输出
在执行时,上述程序将会生成以下输出:
Yes, it is a Palindrome
结论
在本文中,我们学习了使用3种不同方法来检查通过组合数组的所有元素形成的数字是否是回文数。我们还学习了如何使用map()方法将函数应用于列表中的每个元素。最后,我们学习了如何使用extend()函数来合并两个列表。