This question already has answers here:
How are strings compared?
(7 answers)
Closed 4 years ago.
How does python compare strings via the inequality operators?
a = "cat"
b = "dog"
a < b
True
What properties of the strings results in a < b == True?
I've tried few other examples but still have no idea how Python compares them. Does Python compares strings like integers?
Because python compares sequences (i.e. strings you have) sequentially by element values. "c" comes before "d" in Unicode, hence the result.
To compare length, use
len(a) == len(b)
The motivation behind this – having sequential comparison of elements, you can sort sequences in sane way. I.e. sorting list of names alphabetically is just sorted(names_list). And this works exactly because strings will be compared alphabetically and sequentially.
Related
This question already has answers here:
Check if something is (not) in a list in Python
(4 answers)
Closed 2 months ago.
What I mean by that is, that I want to take a list, let's say"
["i","am","you","pure","fact"],
then check whether this list contains 2 different strings, like "i" and "pure", and then if true, it runs something.
This is what I am looking for, but in PYTHON.
I don't know how to do this sort of check.
Try this out
elements: list = ["i","am","you","pure","fact"]
str1: str = "i"
str2: str = "pure"
if str1 in elements and str2 in elements:
# Execute code
...
This question already has answers here:
Writing a function that alternates plus and minus signs between list indices
(7 answers)
Closed 2 years ago.
Given a list of integers, I want to subtract all the integers with an odd index and add all the integers with an even index. Is there any compressed way to do this without the regular "while loop with iterator and add/subtract"?
Use slicing with a step:
n = sum(L[0::2]) - sum(L[1::2])
The sequence slicing syntax L[i:j:k] is documented here, specifically refer to the note 5.
This question already has answers here:
How does tuple comparison work in Python?
(4 answers)
Closed 5 years ago.
What is this python code doing?
min((2,3),(6,'f',1))
Output: (2, 3)
I am not able to follow the documentation.
Can someone explain why the output in (2,3) and not an error?
Because (2,3) < (6,'f',1)
Meaning tuples are compared itemwise, therefore 2 < 6 yields that the first tuple is less than the second one
While this code works on Python 2 and Python 3, it should fail on Python 3 if both items in 1st place were the same. Because it would compare 3 to the string 'f' and such comparison is now invalid.
The min function will call the comparator methods of the objects you pass. In this case, all tuples. It is returning the minimum tuple with respect to lexicographic order.
This question already has answers here:
List slicing with dynamic index on [:index]
(4 answers)
Closed 7 years ago.
I have a list of items in Python and I need to get "all but the last N" items. It needs to work when N is zero (in which case I want the whole list) and when N is greater than or equal to the length of the list (in which case I want an empty list).
This works in most cases:
mylist=[0,1,2,3,4,5,6,7,8,9]
print( mylist[:-n] )
But it fails in the case where N is zero. mylist[:0] returns an empty list: []. Is there a Python slicing notation that will do what I want, or a simple function?
You can pass None to the slice
print(mylist[:-n or None])
This question already has answers here:
How to sort python list of strings of numbers
(4 answers)
Closed 8 years ago.
I am using the following function in order to sort a list in an increasing order. However, while my function works for lists such as: [1,5,6,9,3] or [56,43,16,97,45], it does not work for lists of the form: [20,10,1,3,50].
In such cases, the computer seems to consider that 3>20 and 3>10 and 3 ends up right before 50 (second to last) in the "sorted" list I get. More precisely the result I get is: [1,10,20,3,50].
Here is my code:
def function_sort(L):
for j in range(len(L)):
min=j
for i in range(j+1,len(L)):
if L[i]<L[min]:
min = i
if(min != j):
L[j],L[min] = L[min],L[j]
print L
return L
Could anyone please explain me what is going on?
It sounds like your list consists of strings rather than integers, and you end up getting the elements sorted lexicographically.
By way of illustration, consider the following:
>>> 10 < 2
False
>>> '10' < '2'
True
To fix the issue, convert the elements to integers before sorting:
L = map(int, L)
P.S. I recommend against using min as a variable name since it shadows the built-in function min().