Re: Dictionary order?

2022-08-01 Thread Weatherby,Gerard
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

Re: Dictionary order?

2022-08-01 Thread Dan Stromberg
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

Re: Dictionary order?

2022-08-01 Thread Dan Stromberg
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: > > > > > >

Re: Dictionary order?

2022-08-01 Thread 2QdxY4RzWzUUiLuE
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

Re: Dictionary order?

2022-08-01 Thread Chris Angelico
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

Re: Dictionary order?

2022-08-01 Thread 2QdxY4RzWzUUiLuE
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

Re: Dictionary order?

2022-08-01 Thread Dan Stromberg
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

Re: Dictionary order?

2022-08-01 Thread Chris Angelico
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 > >

Re: Dictionary order?

2022-08-01 Thread Skip Montanaro
> > 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

Re: "dictionary changed size during iteration" error in Python 3 but not in Python 2

2020-08-23 Thread Peter Otten
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

Re: "dictionary changed size during iteration" error in Python 3 but not in Python 2

2020-08-23 Thread Chris Green
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:

Re: "dictionary changed size during iteration" error in Python 3 but not in Python 2

2020-08-23 Thread Cameron Simpson
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

Re: Dictionary

2019-02-27 Thread Peter Otten
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

Re: Dictionary

2019-02-27 Thread Phu Sam
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] >>>

Re: Dictionary

2019-02-25 Thread ast
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

Re: Dictionary

2019-02-24 Thread MRAB
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

Re: Dictionary

2019-02-24 Thread Peter Otten
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

Re: dictionary comparing int keys and joins their values if two key are within a certain distance

2017-11-08 Thread Peter Otten
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],

Re: Dictionary order (Is it consistent up to py3.3 unless using -R or PYTHONHASHSEED is set)

2017-05-28 Thread Steve D'Aprano
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

Re: Dictionary order (Is it consistent up to py3.3 unless using -R or PYTHONHASHSEED is set)

2017-05-28 Thread Ian Kelly
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

Re: Dictionary order (Is it consistent up to py3.3 unless using -R or PYTHONHASHSEED is set)

2017-05-28 Thread Jon Ribbens
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,

Re: Dictionary order (Is it consistent up to py3.3 unless using -R or PYTHONHASHSEED is set)

2017-05-28 Thread Steve D'Aprano
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

Re: Dictionary order (Is it consistent up to py3.3 unless using -R or PYTHONHASHSEED is set)

2017-05-28 Thread Jon Ribbens
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

Re: Dictionary order (Is it consistent up to py3.3 unless using -R or PYTHONHASHSEED is set)

2017-05-27 Thread Steve D'Aprano
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

Re: dictionary of pickle error when get it in multiprocessing and has name error

2017-02-27 Thread Peter Otten
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

Re: dictionary mutability, hashability, __eq__, __hash__

2016-11-27 Thread Jussi Piitulainen
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

Re: dictionary mutability, hashability, __eq__, __hash__

2016-11-27 Thread Ned Batchelder
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

Re: dictionary mutability, hashability, __eq__, __hash__

2016-11-27 Thread Veek M
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 >>

Re: dictionary mutability, hashability, __eq__, __hash__

2016-11-27 Thread Veek M
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

Re: dictionary mutability, hashability, __eq__, __hash__

2016-11-27 Thread Jussi Piitulainen
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: >

RE: dictionary total sum

2016-09-07 Thread Joaquin Alzola
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

Re: dictionary total sum

2016-09-07 Thread Ned Batchelder
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

Re: Dictionary is really not easy to handle

2016-04-28 Thread jfong
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

Re: Dictionary is really not easy to handle

2016-04-28 Thread Steven D'Aprano
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

Re: Dictionary is really not easy to handle

2016-04-28 Thread Peter Otten
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

Re: Dictionary is really not easy to handle

2016-04-28 Thread Rustom Mody
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: >

Re: Dictionary from sqlite3.Row and PyCharm unresolved reference

2015-02-13 Thread Mario Figueiredo
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

Re: Dictionary from sqlite3.Row and PyCharm unresolved reference

2015-02-13 Thread Peter Otten
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'

Re: Dictionary error

2014-11-25 Thread Dave Angel
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

Re: Dictionary error

2014-11-25 Thread John Gordon
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

Re: Dictionary error

2014-11-25 Thread Steven D'Aprano
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

Re: dictionary issue for formatted print

2014-11-09 Thread Peter Otten
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 = { >>

Re: Dictionary help

2014-02-18 Thread Tim Chase
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

Re: dictionary with tuples

2014-01-14 Thread YBM
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"

Re: dictionary with tuples

2014-01-14 Thread emile
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"

Re: dictionary with tuples

2014-01-14 Thread Tobiah
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

Re: dictionary with tuples

2014-01-14 Thread YBM
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

Re: dictionary with tuples

2014-01-14 Thread MRAB
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','

Re: dictionary with tuples

2014-01-14 Thread Tim Chase
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 = {} > >>>

Re: dictionary with tuples

2014-01-14 Thread Larry Martell
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

Re: Dictionary

2014-01-09 Thread wxjmfauth
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. > > >> > >

Re: Dictionary

2014-01-08 Thread Chris Angelico
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

Re: Dictionary

2014-01-08 Thread Bischoop
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

Re: Dictionary

2014-01-08 Thread Bischoop
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

Re: Dictionary

2013-12-30 Thread Walter Hurry
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

Re: Dictionary of Functions

2012-11-15 Thread Joshua Landau
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.

Re: Dictionary of Functions

2012-11-15 Thread Chris Kaynor
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

Re: Dictionary of Functions

2012-11-15 Thread MRAB
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

Re: dictionary into desired variable....

2012-08-10 Thread woooee
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

Re: dictionary into desired variable....

2012-08-10 Thread Tamer Higazi
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

Re: dictionary into desired variable....

2012-08-10 Thread Dave Angel
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

Re: dictionary into desired variable....

2012-08-10 Thread Zero Piraeus
: > 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

Re: dictionary into desired variable....

2012-08-10 Thread Chris Angelico
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

Re: dictionary comprehensions with Python 2.4/2.5

2012-08-07 Thread Iryna Feuerstein
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

Re: dictionary comprehensions with Python 2.4/2.5

2012-08-07 Thread Paul Rubin
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

Re: Dictionary sorting

2011-11-04 Thread Ben Finney
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

Re: Dictionary sorting

2011-11-04 Thread Hrvoje Niksic
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

Re: Dictionary sorting

2011-11-03 Thread Ben Finney
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

Re: Dictionary sorting

2011-11-03 Thread Chris Angelico
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

Re: Dictionary sorting

2011-11-03 Thread Tim Chase
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

Re: Dictionary sorting

2011-11-03 Thread Terry Reedy
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

Re: Dictionary sorting

2011-11-03 Thread insomnia
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

Re: Dictionary sorting

2011-11-03 Thread Chris Kaynor
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

Re: Dictionary sorting

2011-11-03 Thread Scott Ware
Great! Thanks, John for the quick response! -- http://mail.python.org/mailman/listinfo/python-list

Re: Dictionary sorting

2011-11-03 Thread John Gordon
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

Re: Dictionary Views -- good examples? [was Re: Python 3 dict question]

2011-05-10 Thread Ethan Furman
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

Re: Dictionary from String?

2011-05-09 Thread python
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

Re: Dictionary from String?

2011-05-09 Thread python
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'

Re: dictionary size changed during iteration

2011-05-08 Thread Paul Rubin
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

Re: dictionary size changed during iteration

2011-05-08 Thread Hans Mulder
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

Re: Re: Dictionary from String?

2011-05-08 Thread gslindstrom
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

Re: Dictionary from String?

2011-05-08 Thread Dan Stromberg
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

Re: Dictionary from String?

2011-05-08 Thread Benjamin Kaplan
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

Re: Dictionary from String?

2011-05-08 Thread Chris Angelico
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

Re: Dictionary Views -- good examples? [was Re: Python 3 dict question]

2011-05-08 Thread Thomas Rachel
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

Re: dictionary size changed during iteration

2011-05-07 Thread Roy Smith
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

Re: dictionary size changed during iteration

2011-05-07 Thread Paul Rubin
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

Re: dictionary size changed during iteration

2011-05-07 Thread Laszlo Nagy
... 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

Re: Dictionary Views -- good examples? [was Re: Python 3 dict question]

2011-05-07 Thread 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.items()) -- Greg -- http://mail.pyt

Re: Dictionary Views -- good examples? [was Re: Python 3 dict question]

2011-05-06 Thread Ian Kelly
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

Re: dictionary size changed during iteration

2011-04-22 Thread Roy Smith
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

Re: dictionary size changed during iteration

2011-04-22 Thread Mark Niemczyk
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

Re: dictionary size changed during iteration

2011-04-21 Thread John Nagle
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

Re: dictionary size changed during iteration

2011-04-20 Thread Mel
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

Re: dictionary size changed during iteration

2011-04-20 Thread Mel
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] >

Re: dictionary size changed during iteration

2011-04-20 Thread Peter Otten
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

Re: dictionary size changed during iteration

2011-04-20 Thread Peter Otten
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] >

Re: Dictionary with additional attributes

2011-02-22 Thread goodman
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

Re: Dictionary with additional attributes

2011-02-22 Thread Santoso Wijaya
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

Re: Dictionary with additional attributes

2011-02-22 Thread Robert Kern
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   2   3   4   5   6   >