Python - 检查数组中是否存在数字及其双精度值
pythonserver side programmingprogramming更新于 2025/11/8 11:24:17
当需要检查数组中是否存在数字及其双精度值时,会对其进行迭代,并将其乘以 2 并进行检查。
示例
以下是相同的演示
def check_double_exists(my_list):
for i in range(len(my_list)):
for j in (my_list[:i]+my_list[i+1:]):
if 2*my_list[i] == j:
print("The double exists")
my_list = [67, 34, 89, 67, 90, 17, 23]
print("列表为:")
print(my_list)
check_double_exists(my_list)
输出
列表为: [67, 34, 89, 67, 90, 17, 23] The double exists
解释
定义了一个名为‘check_double_exists’的方法,该方法以列表为参数。
它遍历列表,并将每个元素乘以 2,并检查是否存在与此双倍值匹配的数字。
如果找到这样的值,则显示相关消息。
在方法之外,定义了一个列表,并显示在控制台上。
通过传递列表来调用该方法。
输出显示在控制台上。
相关文章
有用资源
python 参考教程 - 该教程包含有关 python 的更多信息:https://www.cainiaomax.com/python/

