python: unicode data is not equal to unicode data [duplicate] - python

This question already has answers here:
Why does comparing strings using either '==' or 'is' sometimes produce a different result?
(15 answers)
Closed 8 years ago.
i has some json works like this.
>>> j = '{"a":5}'
>>> js = json.loads(j)
>>> for key in js:
... key
... type(key)
... key is unicode('a')
...
u'a'
<type 'unicode'>
False
In my opinion last value of output should be true. Plese helpme to find my mistake.

Why are you using is here? You just want to compare:
key == unicode('a')
or even better:
key == u'a'

Related

How do I loop a python list just in 1 line code? [duplicate]

This question already has answers here:
Check if multiple strings exist in another string
(17 answers)
Closed 3 days ago.
I have a list and a string:
exclude_list=['2019-1','2019-2']
first_part='Report - 2019-1'
I need a logic to check ,if any item in the list ,is part of the string ,then just print yes,else print no.
something like:
if any(exclude_list) in first_part:
pritn('yes')
else:
print('no')
I can do it this way:
for i in exclude_list:
if i in first_part:
print('yes')
else:
print('no')
But I want it be simpler, any friend can help ?
>>> exclude_list=['2019-1','2019-2']
>>> first_part='Report - 2019-1'
>>> any(e in first_part for e in exclude_list)
True

New string after concatenation refers to an existing object [duplicate]

This question already has answers here:
Why variables holding same value has the common ID in Python? [duplicate]
(1 answer)
About the changing id of an immutable string
(5 answers)
Closed 5 years ago.
Here's my snippet of code
>>> a = "some_string"
140420665652016
>>> id(a)
>>> id("some" + "_" + "string")
140420665652016
Notice that both the ids are same. But this does not happen with integers (which are also immutable like strings).
>>> a = 999
>>> id(a)
140420666022800
>>> id(999)
140420666021200
>>> id(998 + 1)
140420666023504
I'm not able to find a reason of why it's happening only with strings.

Test if a key is present in a dict and do another test in the same if [duplicate]

This question already has answers here:
Does Python support short-circuiting?
(3 answers)
Closed 6 years ago.
Is the following code using if ... and ...: safe, i.e. are we sure the first condition is tested first and that, if the key is not present, the second condition is ignored?
d = {'a': 1}
if 'b' in d and d['b'] == 2:
print 'hello'
It seems that yes, because this generates no error. But is it true for all Python versions?
Will it never generate a KeyError: 'b'?
PS: this is probably more pythonic, with no ambiguity:
if 'b' in d:
if d['b'] == 2:
...
Yes, it is called a short circuit and.
Both python2 (Link to the doc) and python3.x(Link to the doc) support short circuit and and or

Why id() for same Unicode string literals gives different result? [duplicate]

This question already has answers here:
memory location in unicode strings
(2 answers)
Closed 8 years ago.
Why Unicode string literals show different id's ? I was hoping the same behavior as that of String literals.
>>> p = 'abcd'
>>> q = 'abcd'
>>> id(p) == id(q)
True
>>> p = u'abcd'
>>> q = u'abcd'
>>> id(p) == id(q)
False
Please provide some pointers on this.
For the same reason two dicts with the same contents would have different ids: they are distinct objects. I suspect that the non-Unicode string literals being the same object is something of an optimization.

How to print a string value of a dicionary without the quotes? [duplicate]

This question already has answers here:
In the Python interpreter, how do you return a value without single quotes around it?
(2 answers)
Closed 8 years ago.
In [4]: {'a': "hello"}['a']
Out[4]: 'hello'
I want the output to instead be:
Out[4]: hello
What's the most elegant way to achieve this?
I am also using repr().rjust() later on to print a list of dictionaries into a neatly formatted table, and I notice the repr function also adds quotes which is annoying:
def print_dictionary(d): print repr(d['a']).rjust(10)
print_dictionary({'a':'hello'})
yields:
'hello'
when I want:
hello
Just drop the repr:
def print_dictionary(d): print (d['a']).rjust(10)

Categories