Python 程序检查数组所有元素组合形成的数字是否为回文数
在 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() 函数**(将可迭代对象(如列表、元组、字符串等)的所有元素添加到列表的末尾)
**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
结论
在本文中,我们学习了如何使用三种不同的方法检查由数组所有元素组合形成的数字是否为回文数。我们还学习了如何使用 map() 方法将函数应用于列表中的每个元素。最后,我们使用 extend() 函数学习了如何组合两个列表。
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP