Pythonic way to check if a list is sorted or not
Here is a one liner:
all(l[i] <= l[i+1] for i in range(len(l) - 1))
If using Python 2, use xrange
instead of range
.
For reverse=True
, use >=
instead of <=
.
Check if list is sorted in python
I think the function need to be two lines of code with <=
operators, to detect low to high
and high to low
list and equal sequences.
def in_order(nums):
a = [nums[i-1] <= nums[i] for i in range(1, len(nums))]
return all(x == a[0] for x in a)
[8, 6, 5, 4]
In order
[5, 5, 6, 6, 6, 8, 8, 10, 10, 10, 10]
In order
How to find if the elements in a list are in ascending order or in descending order without using sort function?
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.")
Sample output:
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.
How to check if a list of dictionaries is sorted?
Just use the default check if something is sorted, but index before comparing:
k = "a"
all(l[i][k] <= l[i+1][k] for i in range(len(l) - 1))
Check if a list is in a custom order
from itertools import zip_longest, groupby
okay = list(x == y for y, (x, _) in zip_longest(
(y for y in Y if y in X), groupby(x for x in X if x in Y)))
print(len(okay) >= 2 and all(okay))
First we discard unnecessary elements from both lists. Then we can use groupby
to collapse sequences of the same elements of X
. For example, your first example ['a', 'c', 'c', 'b', 'd', 'd', 'd']
first becomes ['a', 'c', 'c', 'b'] (by discarding the unnecessary
'd'), then
[('a', _), ('c', _), ('b', _)]. If we compare its keys element by element to the Y
without the unnecessary bits, and there are at least 2 of them, we have a match. If the order was violated (e.g. ['b', 'c', 'c', 'a', 'd', 'd', 'd']
, there would have been a False
in okay
, and it would fail. If an extra element appeared somewhere, there would be a comparison with None
(thanks to zip_longest
), and again a False
would have been in okay
.
This can be improved by use of sets to speed up the membership lookup.
Related Topics
Deprecationwarning: Executable_Path Has Been Deprecated Selenium Python
Importerror: No Module Named 'Tkinter'
Generating Permutations with Repetitions
How to Delete Items from a Dictionary While Iterating Over It
How to Check If the String Is Empty
Changing User Agent on Urllib2.Urlopen
I'm Getting Key Error in Python
Syntax Behind Sorted(Key=Lambda: ...)
Stacked Bar Chart with Centered Labels
Does "\D" in Regex Mean a Digit
Reading a Utf8 CSV File with Python
What Does the 'U' Symbol Mean in Front of String Values
How to Convert Each Item in the List to String, for the Purpose of Joining Them
In Python, How to Convert All of the Items in a List to Floats
How to Delete Rows from a Pandas Dataframe Based on a Conditional Expression