I don’t see what is surprising. The interface for a dictionary never specified
the ordering of the keys, so I would not be surprised to see it vary based on
release, platform, values of keys inserted, number of items in the dictionary,
etc.
—
Gerard Weatherby | Application Architect NMRbox
On Mon, Aug 1, 2022 at 4:42 PM Dan Stromberg wrote:
>
> > Yes, but I'm pretty sure that's been true for a LONG time. The hashes
>> > for small integers have been themselves for as long as I can remember.
>> > But the behaviour of the dictionary, when fed such keys, is what's
>> > changed.
>>
>> I
On Mon, Aug 1, 2022 at 3:25 PM <2qdxy4rzwzuui...@potatochowder.com> wrote:
> On 2022-08-02 at 07:50:52 +1000,
> Chris Angelico wrote:
>
> > On Tue, 2 Aug 2022 at 07:48, <2qdxy4rzwzuui...@potatochowder.com> wrote:
> > >
> > > On 2022-08-01 at 13:41:11 -0700,
> > > Dan Stromberg wrote:
> > >
> > >
On 2022-08-02 at 07:50:52 +1000,
Chris Angelico wrote:
> On Tue, 2 Aug 2022 at 07:48, <2qdxy4rzwzuui...@potatochowder.com> wrote:
> >
> > On 2022-08-01 at 13:41:11 -0700,
> > Dan Stromberg wrote:
> >
> > > keys = [5, 10, 15, 14, 9, 4, 1, 2, 8, 6, 7, 12, 11]
> > >
> > > dict_ = {}
> > > for key i
On Tue, 2 Aug 2022 at 07:48, <2qdxy4rzwzuui...@potatochowder.com> wrote:
>
> On 2022-08-01 at 13:41:11 -0700,
> Dan Stromberg wrote:
>
> > keys = [5, 10, 15, 14, 9, 4, 1, 2, 8, 6, 7, 12, 11]
> >
> > dict_ = {}
> > for key in keys:
> > dict_[key] = 1
>
> $ python
> Python 3.10.5 (main, Jun 6 2
On 2022-08-01 at 13:41:11 -0700,
Dan Stromberg wrote:
> keys = [5, 10, 15, 14, 9, 4, 1, 2, 8, 6, 7, 12, 11]
>
> dict_ = {}
> for key in keys:
> dict_[key] = 1
$ python
Python 3.10.5 (main, Jun 6 2022, 18:49:26) [GCC 12.1.0] on linux
Type "help", "copyright", "credits" or "license" for more
On Mon, Aug 1, 2022 at 1:41 PM Dan Stromberg wrote:
> On 1.4 through 2.1 I got descending key order. I expected the keys to be
> scattered, but they weren't.
>
I just noticed that 1.4 was ascending order too - so it was closer to 2.2
than 1.5.
I guess that's kind of beside the point though - it
On Tue, 2 Aug 2022 at 06:50, Skip Montanaro wrote:
>
> >
> > So I decided to write a little test program to run on a variety of
> > CPythons, to confirm what I was thinking.
> >
> > And instead I got a surprise.
> >
> > On 1.4 through 2.1 I got descending key order. I expected the keys to be
> >
>
> So I decided to write a little test program to run on a variety of
> CPythons, to confirm what I was thinking.
>
> And instead I got a surprise.
>
> On 1.4 through 2.1 I got descending key order. I expected the keys to be
> scattered, but they weren't.
>
> On 2.2 through 3.5 I got ascending ke
Chris Green wrote:
>> >1 - Why doesn't it error in Python 2?
>>
>> The dict internal implementation has changed. I don't know the
>> specifics, but it is now faster and maybe smaller and also now preserves
>> insert order.
>>
> Ah, that probably explains it then.
But if you try to modify a dict
Cameron Simpson wrote:
> On 23Aug2020 10:00, Chris Green wrote:
> >I have a (fairly) simple little program that removes old mail messages
> >from my junk folder. I have just tried to upgrade it from Python 2 to
> >Python 3 and now, when it finds any message[s] to delete it produces
> >the error:
On 23Aug2020 10:00, Chris Green wrote:
>I have a (fairly) simple little program that removes old mail messages
>from my junk folder. I have just tried to upgrade it from Python 2 to
>Python 3 and now, when it finds any message[s] to delete it produces
>the error:-
>
>RuntimeError: dictionary
Phu Sam wrote:
> The condition 'if not fibs.get(n):' will not work because
> n = 0
> fibs.get(0) is 0 so not 0 is 1
>
> Here is the modified code that works:
>
> fibs={0:0,1:1}
> def rfib(n):
> if n == 0 or n == 1:
> return fibs[n]
> else:
> fibs[n]=rfib(n-2
The condition 'if not fibs.get(n):' will not work because
n = 0
fibs.get(0) is 0 so not 0 is 1
Here is the modified code that works:
fibs={0:0,1:1}
def rfib(n):
if n == 0 or n == 1:
return fibs[n]
else:
fibs[n]=rfib(n-2)+rfib(n-1)
return fibs[n]
>>>
Le 24/02/2019 à 05:21, Himanshu Yadav a écrit :
fibs={0:0,1:1}
def rfib(n):
global fibs
if not fibs.get(n):
fibs[n]=rfib(n-2)+rfib(n-1)
return fibs[n]
Why it is gives error??
Nothing to do with the malfunction, but you dont need
to define fibs as globa
On 2019-02-24 04:21, Himanshu Yadav wrote:
fibs={0:0,1:1}
def rfib(n):
global fibs
if not fibs.get(n):
fibs[n]=rfib(n-2)+rfib(n-1)
return fibs[n]
Why it is gives error??
What error?
--
https://mail.python.org/mailman/listinfo/python-list
Himanshu Yadav wrote:
> fibs={0:0,1:1}
> def rfib(n):
> global fibs
>if not fibs.get(n):
> fibs[n]=rfib(n-2)+rfib(n-1)
> return fibs[n]
Please use cut and paste to make sure you are not introducing new errors
like the inconsistent indentation above.
> Why
Daiyue Weng wrote:
> I have a nested dictionary of defaultdict(dict) whose sub dict have int
> keys and lists (list of ints) as values,
>
> 'A' = {2092: [1573], 2093: [1576, 1575], 2094: [1577], 2095:
> [1574]}'B' = {2098: [1], 2099: [2, 3], 2101: [4], 2102: [5]}'C' =
> {2001: [6], 2003: [7, 8],
On Mon, 29 May 2017 12:15 am, Jon Ribbens wrote:
> On 2017-05-28, Steve D'Aprano wrote:
>> What exactly did you think I got wrong?
>
> 3.6 does preserve the dict order. It isn't a guarantee so may change
> in future versions, but it is what 3.6 actually does.
Did I say it didn't?
I said you ca
On May 28, 2017 8:23 AM, "Jon Ribbens" wrote:
> On 2017-05-28, Steve D'Aprano wrote:
>> What exactly did you think I got wrong?
>
> 3.6 does preserve the dict order. It isn't a guarantee so may change
> in future versions, but it is what 3.6 actually does.
No, it's what CPython 3.6 actually does
On 2017-05-28, Steve D'Aprano wrote:
> What exactly did you think I got wrong?
3.6 does preserve the dict order. It isn't a guarantee so may change
in future versions, but it is what 3.6 actually does.
>> If you're asking "given a fixed Python version, and where appropriate
>> PYTHONHASHSEED=0,
On Sun, 28 May 2017 11:12 pm, Jon Ribbens wrote:
> On 2017-05-28, Bill Deegan wrote:
>> As a follow up to a discussion on IRC #python channel today.
>>
>> Assuming the same order of insertions of the same items to a dictionary
>> would the iteration of a dictionary be the same (not as the order o
On 2017-05-28, Bill Deegan wrote:
> As a follow up to a discussion on IRC #python channel today.
>
> Assuming the same order of insertions of the same items to a dictionary
> would the iteration of a dictionary be the same (not as the order of
> insertion, just from run to run) for Python 2.7 up t
On Sun, 28 May 2017 10:51 am, Bill Deegan wrote:
> Greetings,
>
> As a follow up to a discussion on IRC #python channel today.
>
> Assuming the same order of insertions of the same items to a dictionary
> would the iteration of a dictionary be the same (not as the order of
> insertion, just from
Ho Yeung Lee wrote:
> before cloususerlogin
> Unexpected error:
> after map pool
>
>
> ...
> passwordlist = pickle.load( open( str(currentworkingdirectory) +
> "\\decryptedsecret.p", "rb" ) )
According to
https://docs.python.org/dev/library/multiprocessing.html#programming-guidelines
you cann
Veek M writes:
> Jussi Piitulainen wrote:
>
>> Veek M writes:
>>
>> [snip]
>>
>>> Also if one can do x.a = 10 or 20 or whatever, and the class instance
>>> is mutable, then why do books keep stating that keys need to be
>>> immutable? After all, __hash__ is the guy doing all the work and
>>> ma
On Sunday, November 27, 2016 at 4:53:20 AM UTC-5, Veek M wrote:
> I was reading this:
> http://stackoverflow.com/questions/4418741/im-able-to-use-a-mutable-object-as-a-dictionary-key-in-python-is-this-not-disa
>
> In a User Defined Type, one can provide __hash__ that returns a integer
> as a key
Veek M wrote:
> Jussi Piitulainen wrote:
>
>> Veek M writes:
>>
>> [snip]
>>
>>> Also if one can do x.a = 10 or 20 or whatever, and the class
>>> instance is mutable, then why do books keep stating that keys need
>>> to be
>>> immutable? After all, __hash__ is the guy doing all the work and
>>
Jussi Piitulainen wrote:
> Veek M writes:
>
> [snip]
>
>> Also if one can do x.a = 10 or 20 or whatever, and the class instance
>> is mutable, then why do books keep stating that keys need to be
>> immutable? After all, __hash__ is the guy doing all the work and
>> maintaining consistency for u
Veek M writes:
[snip]
> Also if one can do x.a = 10 or 20 or whatever, and the class instance
> is mutable, then why do books keep stating that keys need to be
> immutable? After all, __hash__ is the guy doing all the work and
> maintaining consistency for us. One could do:
>
> class Fruit:
>
On Wednesday, September 7, 2016 at 8:25:42 PM UTC-4, p...@blacktoli.com wrote:
> Hello,
>
> any ideas why this does not work?
>
> >>> def add(key, num):
> ... a[key] += num
> ...
> >>> a={}
> >>> a["007-12"] = 22 if not a.has_key("007-12") else add("007-12",22)
> >>> a
> {'007-12': 22} # OK her
On Wednesday, September 7, 2016 at 8:25:42 PM UTC-4, p...@blacktoli.com wrote:
> Hello,
>
> any ideas why this does not work?
>
> >>> def add(key, num):
> ... a[key] += num
> ...
> >>> a={}
> >>> a["007-12"] = 22 if not a.has_key("007-12") else add("007-12",22)
> >>> a
> {'007-12': 22} # OK h
I was overwhelmed that three gurus inspire me in three different ways in their
own flavour:-) That's really appreciated! Now I understand why it's so, thanks
to all of you.
To Peter:
> With that information, can you predict what
> for k, v in {(1, 2): "three"}: print(k, v)
> will print?
It's
On Thu, 28 Apr 2016 06:27 pm, jf...@ms4.hinet.net wrote:
> I have a dictionary like this:
>
dct ={1: 'D', 5: 'A', 2: 'B', 3: 'B', 4: 'E'}
>
> The following code works:
>
for k in dct: print(k, dct[k])
> ...
> 1 D
> 2 B
> 3 B
> 4 E
> 5 A
When you iterate over the dictionary, you get a
jf...@ms4.hinet.net wrote:
> I have a dictionary like this:
>
dct ={1: 'D', 5: 'A', 2: 'B', 3: 'B', 4: 'E'}
>
> The following code works:
> But...this one?
>
for k,v in dct: print(k,v)
> ...
> Traceback (most recent call last):
> File "", line 1, in
> TypeError: 'int' object is no
On Thursday, April 28, 2016 at 1:57:40 PM UTC+5:30, jf...@ms4.hinet.net wrote:
> I have a dictionary like this:
>
> >>> dct ={1: 'D', 5: 'A', 2: 'B', 3: 'B', 4: 'E'}
>
> The following code works:
>
> >>> for k in dct: print(k, dct[k])
> ...
> 1 D
> 2 B
> 3 B
> 4 E
> 5 A
>
> and this one too:
>
In article ,
__pete...@web.de says...
>
> self.data = dict(row)
I didn't realize from the documentation it could be this simple. Thanks.
>
> And now an unsolicited remark: if you have more than one instance of Unknown
> you might read the data outside the initialiser or at least keep the
> c
Mario Figueiredo wrote:
> Currently i'm using the following code to transform a row fetched from an
> sqlite database into a dictionary property:
class Unknown:
> def __init__(self, id_):
> self.id = id_
> self.data = None
> ...
> conn = sqlite3.connect('data'
On 11/25/2014 04:34 AM, Thuruv V wrote:
Please Clarify the 'TypeError: zip argument #1 must support iteration'
import openpyxl
book = openpyxl.load_workbook('c:/users/c_thv/desktop/tax.xlsx')
sheet = book.get_sheet_by_name('Thilip')
cell = sheet.cell(row=2,column = 4)
i = 2
x = []
y = []while
In Thuruv V
writes:
> Please Clarify the 'TypeError: zip argument #1 must support iteration'
> import openpyxl
> book = openpyxl.load_workbook('c:/users/c_thv/desktop/tax.xlsx')
> sheet = book.get_sheet_by_name('Thilip')
> cell = sheet.cell(row=2,column = 4)
> i = 2
> x = []
> y = []while i
Thuruv V wrote:
> Please Clarify the 'TypeError: zip argument #1 must support iteration'
Try it at the interactive interpreter:
py> zip('abc', [1, 2, 3]) # works fine
[('a', 1), ('b', 2), ('c', 3)]
But:
py> zip(1000, [1, 2, 3]) # fails
Traceback (most recent call last):
File "", line 1, i
Yusuf Can Bayrak wrote:
> when dictionary has one value for each key it's okey. I'm just type '%
> greek_letters' and it's working.
>
> But how can i assign dict's values to formatted print, if it has more
> values than one.
>
>>
>>1. # -*- coding: utf-8 -*-
>>2. greek_letters = {
>>
On 2014-02-18 10:30, kjaku...@gmail.com wrote:
> So let's say I have a file and it looks like this:
> Title 1: item
> Title 2: item
> etc
>
> Is it possible to use a dictionary for something like the input
> above? Because I want to be able to use the input above to delete
> the "Title 1" and "T
Le 14/01/2014 23:00, Tobiah a écrit :
On 01/14/2014 01:21 PM, YBM wrote:
Le 14/01/2014 22:10, Igor Korot a écrit :
Hi, ALL,
C:\Documents and Settings\Igor.FORDANWORK\Desktop\winpdb>python
Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit
(Intel)] on win32
Type "help", "copyright"
On 01/14/2014 02:00 PM, Tobiah wrote:
On 01/14/2014 01:21 PM, YBM wrote:
Le 14/01/2014 22:10, Igor Korot a écrit :
Hi, ALL,
C:\Documents and Settings\Igor.FORDANWORK\Desktop\winpdb>python
Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit
(Intel)] on win32
Type "help", "copyright"
On 01/14/2014 01:21 PM, YBM wrote:
Le 14/01/2014 22:10, Igor Korot a écrit :
Hi, ALL,
C:\Documents and Settings\Igor.FORDANWORK\Desktop\winpdb>python
Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more infor
Le 14/01/2014 22:10, Igor Korot a écrit :
Hi, ALL,
C:\Documents and Settings\Igor.FORDANWORK\Desktop\winpdb>python
Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
dict = {}
dict[(1,2)] = ('a
On 2014-01-14 21:10, Igor Korot wrote:
Hi, ALL,
C:\Documents and Settings\Igor.FORDANWORK\Desktop\winpdb>python
Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
dict = {}
dict[(1,2)] = ('a','
On 2014-01-14 13:10, Igor Korot wrote:
> Hi, ALL,
> C:\Documents and Settings\Igor.FORDANWORK\Desktop\winpdb>python
> Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit
> (Intel)] on win32
> Type "help", "copyright", "credits" or "license" for more
> information.
> >>> dict = {}
> >>>
On Tue, Jan 14, 2014 at 2:10 PM, Igor Korot wrote:
> Hi, ALL,
> C:\Documents and Settings\Igor.FORDANWORK\Desktop\winpdb>python
> Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit
> (Intel)] on win32
> Type "help", "copyright", "credits" or "license" for more information.
dict
Le mercredi 8 janvier 2014 20:00:02 UTC+1, Bischoop a écrit :
> Walter Hurry wrote:
>
>
>
> > On Mon, 30 Dec 2013 18:38:20 +, Bischoop wrote:
>
> >
>
> >> I have a txt file with some words, and need simply program that will
>
> >> print me words containing provided letters.
>
> >>
>
>
On Thu, Jan 9, 2014 at 11:13 AM, Dennis Lee Bieber
wrote:
> #generate search re expression representing
> # .* any character/multiple -- leading
> # [l|e|t|t|e|r] match any of the letters supplied
> # .* any character/mu
Walter Hurry wrote:
> On Mon, 30 Dec 2013 18:38:20 +, Bischoop wrote:
>
>> I have a txt file with some words, and need simply program that will
>> print me words containing provided letters.
>>
>> For example:
>> Type the letters:
>> (I type: g,m,o)
>> open the dictionary.txt
>> check words
Dennis Lee Bieber wrote:
> On Mon, 30 Dec 2013 18:38:20 +, Bischoop
> declaimed the following:
>
>>I have a txt file with some words, and need simply program that will
>>print me words containing provided letters.
>>
>>For example:
>>Type the letters:
>> (I type: g,m,o)
>>open the dictionary
On Mon, 30 Dec 2013 18:38:20 +, Bischoop wrote:
> I have a txt file with some words, and need simply program that will
> print me words containing provided letters.
>
> For example:
> Type the letters:
> (I type: g,m,o)
> open the dictionary.txt
> check words containing:g,m,o in dictio
On 15 November 2012 17:13, Chris Kaynor wrote:
> On Thu, Nov 15, 2012 at 8:04 AM, Kevin Gullikson
> wrote:
> > Hi all,
> >
> > I am trying to make a dictionary of functions, where each entry in the
> > dictionary is the same function with a few of the parameters set to
> specific
> > parameters.
On Thu, Nov 15, 2012 at 8:04 AM, Kevin Gullikson
wrote:
> Hi all,
>
> I am trying to make a dictionary of functions, where each entry in the
> dictionary is the same function with a few of the parameters set to specific
> parameters. My actual use is pretty complicated, but I managed to boil down
On 2012-11-15 16:04, Kevin Gullikson wrote:
Hi all,
I am trying to make a dictionary of functions, where each entry in the
dictionary is the same function with a few of the parameters set to
specific parameters. My actual use is pretty complicated, but I managed
to boil down the issue I am havin
On Friday, August 10, 2012 8:31:48 AM UTC-7, Tamer Higazi wrote:
> let us say a would be x = [2,5,4]
>
> y = a[3]
>
> if I change y to []
>
> I want the result to be x = [2,5,[]] and that's automaticly
There is no such thing as a[3] if a=[2,4,5]. And this is a list not a
dictionary, so I wo
Sorry,
I ment of course list
what I exaclty ment is
that if I assign
value = Number
that I automaticly assign y[1][3][6][1][1] a new number.
more detailled explained:
let us say a would be x = [2,5,4]
y = a[3]
if I change y to []
I want the result to be x = [2,5,[]] and that's automati
On 08/10/2012 10:02 AM, Tamer Higazi wrote:
> Hi!
> suppose you have a dictionary that looks like this:
>
> x = [1,3,6,1,1] which should represent a certain other variable.
>
> in reality it would represent:
>
> y[1][3][6][1][1]
>
> Now, how do I write a python routine, that points in this dictiona
:
> suppose you have a dictionary that looks like this:
>
> x = [1,3,6,1,1] which should represent a certain other variable.
That's a list, not a dict.
> in reality it would represent:
>
> y[1][3][6][1][1]
>
> Now, how do I write a python routine, that points in this dictionary,
> where I should
On Sat, Aug 11, 2012 at 12:02 AM, Tamer Higazi wrote:
> Hi!
> suppose you have a dictionary that looks like this:
>
> x = [1,3,6,1,1] which should represent a certain other variable.
>
> in reality it would represent:
>
> y[1][3][6][1][1]
>
> Now, how do I write a python routine, that points in th
Not back to 2.5, but they're not that important anyway. Just use:
d = dict((k, v) for k,v in ... )
Thank you very much! It is the perfect solution for me.
Regards,
Iryna.
--
http://mail.python.org/mailman/listinfo/python-list
Iryna Feuerstein writes:
> code. The dictionary comprehensions were added to Python in version
> 2.7 at first time. Is it possible to make it compatible with Python
> 2.5 anyway? Perhaps by using the __future__ module?
Not back to 2.5, but they're not that important anyway. Just use:
d = dict
Hrvoje Niksic writes:
> Ben Finney writes:
>
> > Tim Chase writes:
> >> Does this "never trust it" hold even for two consecutive iterations
> >> over an unchanged dict? I didn't see anything in the docs[1] to make
> >> such a claim,
> >
> > Exactly.
>
> This is false. The docs say:
>
> If
Ben Finney writes:
> Tim Chase writes:
>
>> On 11/03/11 16:36, Terry Reedy wrote:
>> > CPython iterates (and prints) dict items in their arbitrary internal
>> > hash table order, which depends on the number and entry order of the
>> > items. It is a bug to depend on that arbitrary order in any w
Tim Chase writes:
> On 11/03/11 16:36, Terry Reedy wrote:
> > CPython iterates (and prints) dict items in their arbitrary internal
> > hash table order, which depends on the number and entry order of the
> > items. It is a bug to depend on that arbitrary order in any way.
>
> Does this "never tru
On Fri, Nov 4, 2011 at 10:01 AM, Tim Chase
wrote:
> list1 = list(d.iterkeys())
> list2 = list(d.iterkeys())
> assert list1 == list2
>
There is such a guarantee in Python 2. From
http://docs.python.org/library/stdtypes.html:
"If items(), keys(), values(), iteritems(), iterkeys(), and
itervalues
On 11/03/11 16:36, Terry Reedy wrote:
> Is there a way to not sort them and leave the order as is?
CPython iterates (and prints) dict items in their arbitrary internal
hash table order, which depends on the number and entry order of the
items. It is a bug to depend on that arbitrary order in
On 11/3/2011 2:46 PM, Scott Ware wrote:
Python newbie here. So, when creating dictionaries, I am noticing
that each time I print it out, that its not in the same order as when
I typed it in. They seem to be getting sorted somehow.
No, the entries are not being sorted at all.
> Is there a way t
Moreover, for on-the-fly ordering you can consider to use sorted() on
yourdict.keys(), like:
for k in sorted(yourdict.keys()):
print k, yourdict[k]
I think it has no side effects, except that the orderering can be slow on huge
data sets, and that you need to call it every time after updatin
Note that there are a number of recipes available for free online, and
if you are using a newer version of Python (2.7 or higher), the
collections module includes an OrderedDict class
(http://docs.python.org/library/collections.html#collections.OrderedDict
- this also include a library for Python 2
Great! Thanks, John for the quick response!
--
http://mail.python.org/mailman/listinfo/python-list
In <16245908.783.1320346014867.JavaMail.geo-discussion-forums@yqhd1> Scott Ware
writes:
> Python newbie here. So, when creating dictionaries, I am noticing that
> each time I print it out, that its not in the same order as when I typed
> it in. They seem to be getting sorted somehow. Is there a
Ian Kelly wrote:
On Fri, May 6, 2011 at 4:49 PM, Ethan Furman wrote:
Anybody care to chime in with their usage of this construct?
You should start with PEP 3106. The main idea is that dict.keys() and
dict.items() can be treated as frozensets, while still being more
lightweight than lists. Th
On May 9, 9:33 pm, python wrote:
> On May 8, 12:43 pm, Benjamin Kaplan wrote:
>
>
>
>
>
>
>
>
>
> > On Sun, May 8, 2011 at 8:20 AM, Greg Lindstrom
> > wrote:
> > > Is it possible to create a dictionary from a string value? Something
> > > along
> > > these lines (but that works):
>
> > m
On May 8, 12:43 pm, Benjamin Kaplan wrote:
> On Sun, May 8, 2011 at 8:20 AM, Greg Lindstrom wrote:
> > Is it possible to create a dictionary from a string value? Something along
> > these lines (but that works):
>
> mystring = "{'name':'greg','hatsize':'7 5/8'}"
> mystring
> > "{'name'
Hans Mulder writes:
> How about:
> changes = filter(is_bad, d)
> Or would that be too compact?
I thought of writing something like that but filter in python 3 creates
an iterator that would have the same issue of walking the dictionary
while the dictionary is mutating.
changes = list(f
On 08/05/2011 00:12, Roy Smith wrote:
In article<7xd3jukyn9@ruckus.brouhaha.com>,
Paul Rubin wrote:
Roy Smith writes:
changes = [ ]
for key in d.iterkeys():
if is_bad(key):
changes.append(key)
changes = list(k for k in d if is_bad(k))
is a little bit more direct.
This
On May 8, 2011 2:00pm, Dan Stromberg wrote:
On Sun, May 8, 2011 at 8:20 AM, Greg Lindstrom gslindst...@gmail.com>
wrote:
Is it possible to create a dictionary from a string value? Something
along these lines (but that works):
>>> mystring = "{'name':'greg','hatsize':'7 5/8'}"
>>> mystri
On Sun, May 8, 2011 at 8:20 AM, Greg Lindstrom wrote:
> Is it possible to create a dictionary from a string value? Something along
> these lines (but that works):
>
> >>> mystring = "{'name':'greg','hatsize':'7 5/8'}"
> >>> mystring
> "{'name':'greg','hatsize':'7 5/8'}"
> >>> dict(mystring)
> Tra
On Sun, May 8, 2011 at 8:20 AM, Greg Lindstrom wrote:
> Is it possible to create a dictionary from a string value? Something along
> these lines (but that works):
>
mystring = "{'name':'greg','hatsize':'7 5/8'}"
mystring
> "{'name':'greg','hatsize':'7 5/8'}"
dict(mystring)
> Traceb
On Mon, May 9, 2011 at 1:20 AM, Greg Lindstrom wrote:
> Is it possible to create a dictionary from a string value? Something along
> these lines (but that works):
>
mystring = "{'name':'greg','hatsize':'7 5/8'}"
mystring
> "{'name':'greg','hatsize':'7 5/8'}"
dict(mystring)
> Traceb
Am 07.05.2011 11:09, schrieb Gregory Ewing:
Ethan Furman wrote:
Ian Kelly wrote:
next(iter(myDict.items()))
Which is becoming less elegant.
If you're doing this sort of thing a lot you can make
a little helper function:
def first(x):
return next(iter(x))
then you get to say
first(myDict
In article <7xd3jukyn9@ruckus.brouhaha.com>,
Paul Rubin wrote:
> Roy Smith writes:
> > changes = [ ]
> > for key in d.iterkeys():
> > if is_bad(key):
> > changes.append(key)
>
> changes = list(k for k in d if is_bad(k))
>
> is a little bit more direct.
This is true. I still fi
Roy Smith writes:
> changes = [ ]
> for key in d.iterkeys():
> if is_bad(key):
> changes.append(key)
changes = list(k for k in d if is_bad(k))
is a little bit more direct.
--
http://mail.python.org/mailman/listinfo/python-list
...
That works, but if d is large, it won't be very efficient because it has
to generate a large list.
It is not large. But I'm using Python 2.6 , not Python 3.
I did not get this error again in the last two days. I'll post a new
reply if I encounter it again. (It happened just a few times o
Ethan Furman wrote:
Ian Kelly wrote:
next(iter(myDict.items()))
Which is becoming less elegant.
If you're doing this sort of thing a lot you can make
a little helper function:
def first(x):
return next(iter(x))
then you get to say
first(myDict.items())
--
Greg
--
http://mail.pyt
On Fri, May 6, 2011 at 4:49 PM, Ethan Furman wrote:
> Ian Kelly wrote:
>>
>> On Fri, May 6, 2011 at 1:57 PM, dmitrey wrote:
>>>
>>> Unfortunately, it doesn't work, it turn out to be dict_items:
>>
>> next({1:2}.items())
>>>
>>> Traceback (most recent call last):
>>> File "", line 1, in
In article ,
Peter Otten <__pete...@web.de> wrote:
> You now have to create the list explicitly to avoid the error:
>
> >>> d = dict(a=1)
> >>> keys = list(d.keys())
> >>> for k in keys:
> ... d["b"] = 42
> ...
That works, but if d is large, it won't be very efficient because it has
to gen
As of Python 3.x (which I suspect you are running):
"The objects returned by dict.keys(), dict.values() and dict.items() are view
objects. They provide a dynamic view on the dictionary’s entries, which means
that when the dictionary changes, the view reflects these changes.", and
"Iterating vi
On 4/20/2011 5:52 AM, Laszlo Nagy wrote:
Given this iterator:
class SomeIterableObject(object):
def __iter__(self):
ukeys = self.updates.keys()
for key in ukeys:
if self.updates.has_key(key):
yield self.updates[key]
for rec in self.inserts:
yield rec
How can I get this exce
Mel wrote:
> Laszlo Nagy wrote:
> `ukeys` isn't a different dictionary from `self.updates.keys` I'ts merely
> another name referring to the same dict object. I think
>
> ukeys = dict (self.updates.keys)
>
> would do what you want.
Sorry. Belay that. Thought I'd had enough coffee.
Me
Laszlo Nagy wrote:
> Given this iterator:
>
> class SomeIterableObject(object):
>
>
>
> def __iter__(self):
> ukeys = self.updates.keys()
> for key in ukeys:
> if self.updates.has_key(key):
> yield self.updates[key]
>
Peter Otten wrote:
> Laszlo Nagy wrote:
>
>> Given this iterator:
>>
>> class SomeIterableObject(object):
>>
>>
>>
>> def __iter__(self):
>> ukeys = self.updates.keys()
>> for key in ukeys:
>> if self.updates.has_key(key):
Hm, I see you a
Laszlo Nagy wrote:
> Given this iterator:
>
> class SomeIterableObject(object):
>
>
>
> def __iter__(self):
> ukeys = self.updates.keys()
> for key in ukeys:
> if self.updates.has_key(key):
> yield self.updates[key]
>
On Feb 22, 1:32 pm, Robert Kern wrote:
> On 2/22/11 1:49 PM, goodman wrote:
>
> > Hi, my question is this: Is it a bad idea to create a wrapper class
> > for a dictionary so I can add attributes?
>
> Nope. I do recommend adding a custom __repr__(), though.
>
Good point. Thanks
--
http://mail.pyt
Instead of inheriting a dict, why not composing a dict into your "model"
class, like:
class Model(object):
def __init__(self, *args, **kwargs):
self._probabilities = defaultdict(lambda: defaultdict(float))
@property
def features(self):
# insert
On 2/22/11 1:49 PM, goodman wrote:
Hi, my question is this: Is it a bad idea to create a wrapper class
for a dictionary so I can add attributes?
Nope. I do recommend adding a custom __repr__(), though.
--
Robert Kern
"I have come to believe that the whole world is an enigma, a harmless enigma
1 - 100 of 524 matches
Mail list logo