This question already has answers here:
How are strings compared?
(7 answers)
Closed 6 years ago.
I have written this code in python 3.5
x="absx"
o="abcdef"
and if i am doing this operation,
x<o
False #it return's False and i think it should return True
So what is '<' doing in case of strings and why is it not returning true.
how is it comparing x and o?
The < or > would result in lexicographic comparison of the two strings:
>>> x="absx"
>>> o="abcdef"
>>> x > o
True
Lexicographic ordering is same as dictionary ordering, and basically, the operators are checking for which string would come earlier (or later) in a dictionary order. The behavior is same for both Python 2 and 3.
The final result does not depend on the size of the string, Example:
>>> "a" < "aaaaa"
True
In example above "a" would come before "aaaaa" when written in dictionary order. To compare by length of strings, use the len() function on the strings.
Lexicographic comparison. In your case o would come after x.
Related
This question already has an answer here:
python is operator behaviour with string [duplicate]
(1 answer)
Closed 4 months ago.
I am filtering a list of strings according to the following condition.
list_of_strings = ["foo-bar", "foo", "bar"]
print(list(filter(lambda x: x is not "foo-bar", list_of_strings)))
>>> ["foo-bar", "foo", "bar"] # This is output. But I expected "foo-bar" to be removed
But this is working fine with !=
How is this happening?
The problem is in the filtering condition. Should be:
lambda x: x != "foo-bar"
To compare two strings use != and == (equality test).
is is a test for an identical object.
>>> "A" is "A"
True
This is an efect of an optimisation called interning. If a constant is used several times, the Python interpreter tries to save only one copy of it. You cannot rely on that.
>>> "a".upper() is "A"
False
>>> "a".upper() == "A"
True
This question already has answers here:
How to test if one string is a subsequence of another?
(4 answers)
Python list filtering: remove subsets from list of lists
(10 answers)
Closed 4 years ago.
I know many similar questions has been asked before with answers but my concern is related to builtin functionality rather than my own logic of loops and indexes etc.
In my python app, I need to check if listA is subset of listB. I've done this with the help of sets like this
set(listA) < set(listB)
It works fine overall but I also need to check order of objects in list. e.g.
x = ['a','b','c','d']
xx = ['a', 'c']
xxx = ['c' , 'a']
so according to sets definition;
set(xx) < set(x) returns true and set(xxx) < set(x) also returns true...
But I need something that returns false for 2nd case.
Is there any builtin functionality in python or I need to implement my own logic to get required result?
I think a better word for this operation is a subsequence.
You need to write this yourself, but you can do it fairly easily by walking both lists together:
def is_subsequence(a, b):
b_it = iter(b)
try:
for a_val in a:
while next(b_it) != a_val:
pass
except StopIteration:
return False
else:
return True
used as:
>>> is_subsequence(xx, x)
True
>>> is_subsequence(xxx, x)
False
Edit: Searching for "subsequence" finds this great answer.
This question already has answers here:
How are strings compared?
(7 answers)
Closed 7 years ago.
I have numerical values that are loaded from a JSON object and are therefore all strings.
I am having issues with making numerical comparisons with these strings. The following makes no sense to me and I was hoping one of you champions could explain..
In[2]: print '100' < '45'
True
In[3]: print '99' < '45'
False
Using Python 2.7
When comparing strings they're compared by the ascii value of the characters. '1' has a value 49, and '4' is 52. So '1' is < '4'. '9' however is 57, so '9' is > '4'.
If you want to compare them numerically you could just int() the strings first like:
print int('100') < int('45')
It basically checks for lexicographic ordering.
Check documentation here -
>>> 'b' <'a'
False
>>> 'a' < 'b'
True
In above example, a comes before b, hence 'a' <'b' is true. But, not vica versa. Similarly '1'<'2'.
Hence '199999999999' < '5' is true because 1 comes before 5.
This question already has answers here:
"is" operator behaves unexpectedly with integers
(11 answers)
Closed 7 years ago.
I'm wondering why this evaluates to True.
x = 1
if x is 1:
print "Does x and 1 point to the same object?"
print "Does this mean python doesn't store redundant values?"
It doesn't work for this case as I expect.
x = range(10)
y = range(10)
if not x is y:
print "I expect this"
My understanding is that is checks to see if two names are pointing to the same object. Does this imply that python has a mechanism to avoid creating redundant values?
This is an implementation detail of the CPython interpreter, but integers with small values are "interned" -- whenever the result of an expression falls within a certain range, an existing int object with the same value is returned.
You could check the range that gets interned by using the following code:
interned_range = [i for i in range(-1000, 1000) if i+0 is i]
print min(interned_range), max(interned_range)
My CPython inters integers between -5 and 256 inclusive.
Being an implementation detail, this behaviour should generally not be relied upon.
This question already has answers here:
python operator precedence of in and comparison [duplicate]
(3 answers)
Closed 9 years ago.
Consider this list:
list = [1,2,3,4,5]
I want to check if the number 9 is not present in this list. There are 2 ways to do this.
Method 1: This method works!
if not 9 in list: print "9 is not present in list"
Method 2: This method does not work.
if 9 in list == False: print "9 is not present in list"
Can someone please explain why method 2 does not work?
This is due to comparison operator chaining. From the documentation:
Comparisons can be chained arbitrarily, e.g., x < y <= z is equivalent to x < y and y <= z, except that y is evaluated only once (but in both cases z is not evaluated at all when x < y is found to be false).
You are assuming that the 9 in list == False expression is executed as (9 in list) == False but that is not the case.
Instead, python evaluates that as (9 in list) and (list == False) instead, and the latter part is never True.
You really want to use the not in operator, and avoid naming your variables list:
if 9 not in lst:
It should be:
if (9 in list) == False: print "9 is not present in list"