Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #83173 > unrolled thread
| Started by | patrick vrijlandt <pvrijlandt@gmail.com> |
|---|---|
| First post | 2015-01-03 19:50 +0100 |
| Last post | 2015-01-03 22:38 +0330 |
| Articles | 6 — 5 participants |
Back to article view | Back to comp.lang.python
surprise - byte in set patrick vrijlandt <pvrijlandt@gmail.com> - 2015-01-03 19:50 +0100
Re: surprise - byte in set Jason Friedman <jsf80238@gmail.com> - 2015-01-03 12:12 -0700
Re: surprise - byte in set Dan Stromberg <drsalists@gmail.com> - 2015-01-03 11:15 -0800
Re: surprise - byte in set Gary Herron <gherron@digipen.edu> - 2015-01-03 12:05 -0800
Re: surprise - byte in set patrick vrijlandt <pvrijlandt@gmail.com> - 2015-01-03 23:08 +0100
Re: surprise - byte in set Amir Arsalan <am1r.ar3alan@gmail.com> - 2015-01-03 22:38 +0330
| From | patrick vrijlandt <pvrijlandt@gmail.com> |
|---|---|
| Date | 2015-01-03 19:50 +0100 |
| Subject | surprise - byte in set |
| Message-ID | <54a83a19$0$6953$e4fe514c@dreader36.news.xs4all.nl> |
Hello list,
Let me first wish you all the best in 2015!
Today I was trying to test for occurrence of a byte in a set ...
>>> sys.version
'3.4.2 (v3.4.2:ab2c023a9432, Oct 6 2014, 22:15:05) [MSC v.1600 32 bit
(Intel)]'
>>> 'b' in 'abc'
True
>>> b'b' in b'abc'
True
>>> 'b' in set('abc')
True
>>> b'b' in set(b'abc')
False
I was surprised by the last result. What happened?
(Examples simplified; I was planning to manipulate the set)
Patrick
---
Dit e-mailbericht is gecontroleerd op virussen met Avast antivirussoftware.
http://www.avast.com
[toc] | [next] | [standalone]
| From | Jason Friedman <jsf80238@gmail.com> |
|---|---|
| Date | 2015-01-03 12:12 -0700 |
| Message-ID | <mailman.17355.1420312373.18130.python-list@python.org> |
| In reply to | #83173 |
>>>> sys.version
> '3.4.2 (v3.4.2:ab2c023a9432, Oct 6 2014, 22:15:05) [MSC v.1600 32 bit
> (Intel)]'
>>>> 'b' in 'abc'
> True
>>>> b'b' in b'abc'
> True
>>>> 'b' in set('abc')
> True
>>>> b'b' in set(b'abc')
> False
>
> I was surprised by the last result. What happened?
> (Examples simplified; I was planning to manipulate the set)
I'm no expert, but I see:
>>> for i in set(b'abc'):
... print(type(i))
...
<class 'int'>
<class 'int'>
<class 'int'>
[toc] | [prev] | [next] | [standalone]
| From | Dan Stromberg <drsalists@gmail.com> |
|---|---|
| Date | 2015-01-03 11:15 -0800 |
| Message-ID | <mailman.17356.1420312524.18130.python-list@python.org> |
| In reply to | #83173 |
On Sat, Jan 3, 2015 at 10:50 AM, patrick vrijlandt <pvrijlandt@gmail.com> wrote: > Hello list, > > Let me first wish you all the best in 2015! > > Today I was trying to test for occurrence of a byte in a set ... In the last case, the set has integers in it. Try: b'b'[0] in set(b'abc')
[toc] | [prev] | [next] | [standalone]
| From | Gary Herron <gherron@digipen.edu> |
|---|---|
| Date | 2015-01-03 12:05 -0800 |
| Message-ID | <mailman.17357.1420316107.18130.python-list@python.org> |
| In reply to | #83173 |
On 01/03/2015 10:50 AM, patrick vrijlandt wrote:
> Hello list,
>
> Let me first wish you all the best in 2015!
>
> Today I was trying to test for occurrence of a byte in a set ...
>
> >>> sys.version
> '3.4.2 (v3.4.2:ab2c023a9432, Oct 6 2014, 22:15:05) [MSC v.1600 32 bit
> (Intel)]'
> >>> 'b' in 'abc'
> True
> >>> b'b' in b'abc'
> True
> >>> 'b' in set('abc')
> True
> >>> b'b' in set(b'abc')
> False
>
> I was surprised by the last result. What happened?
> (Examples simplified; I was planning to manipulate the set)
The surprise is really that the 3rd test is True not that the fourth is
False.
First, as should be expected, a byte string is a sequence of (small)
ints. So b'b' is a (short) byte string and the set set(b'abc') is
composed of three ints. You should not expect your inclusion test to
return True when testing for a bytes-type object in a set of int-type
objects. And that explains your False result in the 4th test.
>>> type(b'abc')
<class 'bytes'>
>>> type(b'abc'[0])
<class 'int'>
But things are different for strings. You might think a string is a
sequence of characters, but Python does not have a character type. In
fact the elements of a string are just 1 char long strings:
>>> type('abc')
<class 'str'>
>>> type('abc'[0])
<class 'str'>
You would not logically expect to find a string 'b' in a set of
characters in, say C++, where the two types are different. But that's
not the Python way. In Python a set of characters set('abc') is really
a set of (short) strings, and the character 'b' is really a (short)
string, so the inclusion test works.
Python's way of returning a 1-byte string when indexing a string
(instead of returning an element of type character) allows this
surprising result.
>>> 'abc'[0]
'a'
>>> 'abc'[0][0]
'a'
>>> 'abc'[0][0][0]
'a'
>>> 'abc'[0][0][0][0]
'a'
...
I've never considered this a problem, but a infinitely indexable object
*is* a bit of an oddity.
>
> Patrick
>
> ---
> Dit e-mailbericht is gecontroleerd op virussen met Avast
> antivirussoftware.
> http://www.avast.com
>
--
Dr. Gary Herron
Department of Computer Science
DigiPen Institute of Technology
(425) 895-4418
[toc] | [prev] | [next] | [standalone]
| From | patrick vrijlandt <pvrijlandt@gmail.com> |
|---|---|
| Date | 2015-01-03 23:08 +0100 |
| Message-ID | <54a8687d$0$15822$e4fe514c@dreader35.news.xs4all.nl> |
| In reply to | #83173 |
Dear all, Many thanks for your responses. I never realised this difference between 'bytes' and 'string'. Thanks, Patrick --- Dit e-mailbericht is gecontroleerd op virussen met Avast antivirussoftware. http://www.avast.com
[toc] | [prev] | [next] | [standalone]
| From | Amir Arsalan <am1r.ar3alan@gmail.com> |
|---|---|
| Date | 2015-01-03 22:38 +0330 |
| Message-ID | <mailman.17386.1420459573.18130.python-list@python.org> |
| In reply to | #83173 |
[Multipart message — attachments visible in raw view] — view raw
Hi my friends.
On last result you have a , b , c binary.
>> set('abc')
{'a','b','c')
>> set(b'abc')
{97,98,99}
On Jan 3, 2015 10:25 PM, "patrick vrijlandt" <pvrijlandt@gmail.com> wrote:
> Hello list,
>
> Let me first wish you all the best in 2015!
>
> Today I was trying to test for occurrence of a byte in a set ...
>
> >>> sys.version
> '3.4.2 (v3.4.2:ab2c023a9432, Oct 6 2014, 22:15:05) [MSC v.1600 32 bit
> (Intel)]'
> >>> 'b' in 'abc'
> True
> >>> b'b' in b'abc'
> True
> >>> 'b' in set('abc')
> True
> >>> b'b' in set(b'abc')
> False
>
> I was surprised by the last result. What happened?
> (Examples simplified; I was planning to manipulate the set)
>
> Patrick
>
> ---
> Dit e-mailbericht is gecontroleerd op virussen met Avast antivirussoftware.
> http://www.avast.com
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web