Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在不使用排序函数的情况下,如何确定列表中的元素是按升序还是按降序排列?_Python_Python 3.x_List_Sorting - Fatal编程技术网

Python 在不使用排序函数的情况下,如何确定列表中的元素是按升序还是按降序排列?

Python 在不使用排序函数的情况下,如何确定列表中的元素是按升序还是按降序排列?,python,python-3.x,list,sorting,Python,Python 3.x,List,Sorting,python给我的问题是,接受一个包含n个术语的列表,检查元素是否按升序、降序排列,而不使用sort函数和def 请帮我做这个节目 代码: a=列表(输入(“输入元素:”) 对于范围内的i(len(a)): 对于范围(i)中的j: 如果a[j]=a[j+1]: 打印(“按降序排列”) 其他: 打印(“不正常”) 全部(a@splash58很好的逻辑,但它只适用于升序。若要将其扩展为升序和降序,all([(a=b)表示a,b在zip中(lst[:-1],lst[1:]))@SatheeshK No

python给我的问题是,接受一个包含n个术语的列表,检查元素是否按升序、降序排列,而不使用sort函数和def

请帮我做这个节目

代码:

a=列表(输入(“输入元素:”)
对于范围内的i(len(a)):
对于范围(i)中的j:
如果a[j]=a[j+1]:
打印(“按降序排列”)
其他:
打印(“不正常”)

全部(a@splash58很好的逻辑,但它只适用于升序。若要将其扩展为升序和降序,
all([(a=b)表示a,b在zip中(lst[:-1],lst[1:]))
@SatheeshK No OP需要询问更多关于desc的信息。您的表达式永远不会返回false-(a=b)总是true@splash58,是的,你是对的。所以答案应该是
all([a=b代表a,b在zip中(lst3[:-1],lst3[1:]))
@SatheeshK我已经用一个密码写了答案谢谢你的程序亲爱的朋友,但是我们可以不用def来做这个程序吗?我这样写的,这样你就可以检查列表中的任何编号,如果你想单独使用,那么就去掉第一行并缩进其他行,而不是返回使用print这会给出预期的输出,但效率很低:I如果您有一个以
[0,2,1,…]
开头的10亿个值的列表,那么您的代码将在10亿个值上迭代两次,尽管输出(既不递增也不递减)可以在查看前三个值之后决定。@ TyelyLathuuli我根据OP需要编写了这个代码,并且很容易理解他,然后考虑第一个三值是否相同。不能仅基于此决定,如果需要,我可以更优化它,加上一个需要遍历所有值并告诉TI是完全升或是DEN。不定不定
a=list(input("Enter elements:"))
for i in range(len(a)):
    for j in range(i):
        if a[j]<=a[j+1]:
            print("It is in ascending order")
        elif a[j]>=a[j+1]:
            print("It is in descending order")
        else:
            print("It is not in order")
def func(l):
    a_cond = True # considering list is in ascending order
    d_cond = True # # considering list is in descending order

    # checking if for any number my ascending condition false

    for i in range(1,len(l)):
        if not l[i]>l[i-1]:
            a_cond = False
    # if condition not false then return list is ascending
    if a_cond:
        return 'Ascending Order'

    # if condition false then check for descending codition
    for i in range(1, len(l)):
        if not l[i]<l[i-1]:
            d_cond = False
    # check if all number in descending order
    if d_cond:
        return 'Descending order'

    # if above both condition fail, list is in mix order
    return 'Mix Order'



print(func([1,2,3,4,5]))
print(func([5,4,3,2,1]))
print(func([1,2,3,1,2]))
Ascending Order
Descending order
Mix Order
def which_order(list1):
    isAscending = True
    isDescending = True
    for i in range(1,len(list1)):
        if(list1[i] >= list1[i-1]):
            isDescending = False
        elif(list1[i] <= list1[i-1]):
            isAscending = False
        if((not isAscending) and (not isDescending)):
            print("The list is not sorted in either order.")
            break
    if(isAscending):
        print("The list is sorted in ascending order.")
    if(isDescending):
        print("The list is sorted in descending order.")
list1 = [1,2,3,4]
list2 = [9,8,7,6]
list3 = [1,9,8,7,6]
which_order(list1)
which_order(list2)
which_order(list3)

The list is sorted in ascending order.
The list is sorted in descending order.
The list is not sorted in either order.