Appending to and removing from numpy arrays

2020-06-10 Thread Urs Thuermann
Hi, is it possible to append data to or remove data from numpy arrays like Python lists? I have some code where I currently use lists and often do things like a.append(elem) a += b del a[:-n] I am thinking of changing to numpy arrays but it seems I cannot modify numpy arrays like th

Re: Checking support for efficient appending to mapped data

2019-04-30 Thread Markus Elfring
> The file name for the client script is passed by a parameter to a command > which is repeated by this server in a loop. Additional explanations can be helpful for the shown software situation. > It is evaluated then how often a known record set count was sent. It was actually determined for a

Re: Appending to a list, which is value of a dictionary

2016-10-15 Thread Wolfgang Maier
On 15.10.2016 18:16, Steve D'Aprano wrote: # Python 3 only: use a dict comprehension py> d = {x:[] for x in (1, 2, 3)} py> d {1: [], 2: [], 3: []} dict (and set) comprehensions got backported so this works just as well in Python 2.7 Wolfgang -- https://mail.python.org/mailman/listinfo/pytho

Re: Appending to a list, which is value of a dictionary

2016-10-15 Thread Steve D'Aprano
On Sat, 15 Oct 2016 11:35 pm, Uday J wrote: > Hi, > > Here is the code, which I would like to understand. > l=['a','b','c'] bm=dict.fromkeys(l,['-1','-1']) fromkeys() doesn't make a copy of the list each time it is used. It uses the exact same list each time. Watch: py> L = [] py> d

Re: Appending to a list, which is value of a dictionary

2016-10-15 Thread Chris Angelico
On Sun, Oct 16, 2016 at 3:12 AM, Jussi Piitulainen wrote: > Chris Angelico writes: > >> On Sat, Oct 15, 2016 at 11:35 PM, Uday J wrote: >> bm=dict.fromkeys(l,['-1','-1']) >> >> When you call dict.fromkeys, it uses the same object as the key every >> time. If you don't want that, try a dict com

Re: Appending to a list, which is value of a dictionary

2016-10-15 Thread Jussi Piitulainen
Chris Angelico writes: > On Sat, Oct 15, 2016 at 11:35 PM, Uday J wrote: > bm=dict.fromkeys(l,['-1','-1']) > > When you call dict.fromkeys, it uses the same object as the key every > time. If you don't want that, try a dict comprehension instead: s/key/value/ -- https://mail.python.org/mailm

Re: Appending to a list, which is value of a dictionary

2016-10-15 Thread Jussi Piitulainen
Uday J writes: > Hi, > > Here is the code, which I would like to understand. > l=['a','b','c'] bm=dict.fromkeys(l,['-1','-1']) u={'a':['Q','P']} bm.update(u) bm > {'a': ['Q', 'P'], 'c': ['-1', '-1'], 'b': ['-1', '-1']} for k in bm.keys(): > bm[k].append('DDD') >

Re: Appending to a list, which is value of a dictionary

2016-10-15 Thread Chris Angelico
On Sat, Oct 15, 2016 at 11:35 PM, Uday J wrote: bm=dict.fromkeys(l,['-1','-1']) When you call dict.fromkeys, it uses the same object as the key every time. If you don't want that, try a dict comprehension instead: bm = {x: ['-1', '-1'] for x in l} This will construct a new list for every k

Appending to a list, which is value of a dictionary

2016-10-15 Thread Uday J
Hi, Here is the code, which I would like to understand. >>> l=['a','b','c'] >>> bm=dict.fromkeys(l,['-1','-1']) >>> u={'a':['Q','P']} >>> bm.update(u) >>> bm {'a': ['Q', 'P'], 'c': ['-1', '-1'], 'b': ['-1', '-1']} >>> for k in bm.keys(): bm[k].append('DDD') >>> bm {'a': ['Q', 'P', 'DDD'], 'c': [

RE: Appending to []

2012-04-25 Thread Prasad, Ramit
> >> Then nested calls like > >> > >> a = [].append('x').append('y').append('z') > > Sequential appends are nearly always done within a loop. If not in a loop and you have multiple things already in an iterable (or create the iterable inline) you can use extend a= [] a.extend( [ 'x, 'y, 'z' ] )

Re: Appending to []

2012-04-22 Thread Kiuhnm
On 4/22/2012 10:29, Bernd Nawothnig wrote: [...] In general I always prefer the pure functional approach. But you are right, if it is too costly, one has to weigh the pros and contras. Here's some stupid trick I came up with after reading this thread. It's of very limited use, of course and I

Re: Appending to []

2012-04-22 Thread Ian Kelly
On Sun, Apr 22, 2012 at 2:29 AM, Bernd Nawothnig wrote: >> But what about 2), the mixed (impure) functional design? Unfortunately, >> it too has a failure mode: by returning a list, it encourages the error >> of assuming the list is a copy rather than the original: >> >> mylist = [1, 2, 3, 4] >> a

Re: Appending to []

2012-04-22 Thread Bernd Nawothnig
On 2012-04-22, Steven D'Aprano wrote: > On Sat, 21 Apr 2012 14:48:44 +0200, Bernd Nawothnig wrote: > >> On 2012-04-20, Rotwang wrote: >>> since a method doesn't assign the value it returns to the instance on >>> which it is called; what it does to the instance and what it returns >>> are two comple

Re: Appending to []

2012-04-21 Thread Steven D'Aprano
On Sat, 21 Apr 2012 14:48:44 +0200, Bernd Nawothnig wrote: > On 2012-04-20, Rotwang wrote: >> since a method doesn't assign the value it returns to the instance on >> which it is called; what it does to the instance and what it returns >> are two completely different things. > > Returning a None-

Re: Appending to []

2012-04-21 Thread Bernd Nawothnig
On 2012-04-21, Kiuhnm wrote: > Sorry if I wasn't clear. I meant that one should either relies on > side-effects and write something like >a.append('x').append('t').append('z') > or use a more functional style and write >a = a + [x] + [z] > Mixing the two doesn't seem very elegant to me. >

Re: Appending to []

2012-04-21 Thread Kiuhnm
On 4/21/2012 18:14, Kiuhnm wrote: On 4/21/2012 17:41, Bernd Nawothnig wrote: On 2012-04-21, Kiuhnm wrote: Returning a None-value is pretty useless. Why not returning self, which would be the resulting list in this case? Returning self would make the language a little bit more functional, withou

Re: Appending to []

2012-04-21 Thread Kiuhnm
On 4/21/2012 17:41, Bernd Nawothnig wrote: On 2012-04-21, Kiuhnm wrote: Returning a None-value is pretty useless. Why not returning self, which would be the resulting list in this case? Returning self would make the language a little bit more functional, without any drawback. ^

Re: Appending to []

2012-04-21 Thread Terry Reedy
On 4/21/2012 9:08 AM, Dave Angel wrote: On 04/21/2012 08:48 AM, Bernd Nawothnig wrote: On 2012-04-20, Rotwang wrote: since a method doesn't assign the value it returns to the instance on which it is called; what it does to the instance and what it returns are two completely different things. R

Re: Appending to []

2012-04-21 Thread Bernd Nawothnig
On 2012-04-21, Kiuhnm wrote: >> Returning a None-value is pretty useless. Why not returning self, which >> would be >> the resulting list in this case? Returning self would make the >> language a little bit more functional, without any drawback. >> >> Then

Re: Appending to []

2012-04-21 Thread Kiuhnm
On 4/21/2012 14:48, Bernd Nawothnig wrote: On 2012-04-20, Rotwang wrote: since a method doesn't assign the value it returns to the instance on which it is called; what it does to the instance and what it returns are two completely different things. Returning a None-value is pretty useless. Why

Re: Appending to []

2012-04-21 Thread Dave Angel
On 04/21/2012 08:48 AM, Bernd Nawothnig wrote: > On 2012-04-20, Rotwang wrote: >> since a method doesn't assign the value it returns to the instance on >> which it is called; what it does to the instance and what it returns are >> two completely different things. > Returning a None-value is prett

Re: Appending to []

2012-04-21 Thread Bernd Nawothnig
On 2012-04-20, Rotwang wrote: > since a method doesn't assign the value it returns to the instance on > which it is called; what it does to the instance and what it returns are > two completely different things. Returning a None-value is pretty useless. Why not returning self, which would be the

Re: Appending to []

2012-04-20 Thread Chris Angelico
On Sat, Apr 21, 2012 at 6:17 AM, Rotwang wrote: > In general there's no reason why > a.method(arguments) print a > > will print the same thing as > print a.method(arguments) > > since a method doesn't assign the value it returns to the instance on which > it is called; what it does

Re: Appending to []

2012-04-20 Thread Kiuhnm
On 4/20/2012 22:03, Jan Sipke wrote: Can you explain why there is a difference between the following two statements? a = [] a.append(1) print a [1] print [].append(1) None Try this one: a = [] print a.append(1) Does that answer your question? Kiuhnm -- http://mail.python.org/mai

Re: Appending to []

2012-04-20 Thread Rotwang
On 20/04/2012 21:03, Jan Sipke wrote: Can you explain why there is a difference between the following two statements? a = [] a.append(1) print a [1] print [].append(1) None append is a method of the list object []. Methods, in general, both do something to the objects of which they are a

Re: Appending to []

2012-04-20 Thread Chris Angelico
On Sat, Apr 21, 2012 at 6:03 AM, Jan Sipke wrote: > Can you explain why there is a difference between the following two > statements? > a = [] a.append(1) print a > [1] This looks at the list after appending. print [].append(1) > None This looks at the return value of the ap

Appending to []

2012-04-20 Thread Jan Sipke
Can you explain why there is a difference between the following two statements? >>> a = [] >>> a.append(1) >>> print a [1] >>> print [].append(1) None Best regards, Jan Sipke -- http://mail.python.org/mailman/listinfo/python-list

Appending to sys.path during module install with distutils

2011-10-30 Thread Darren Hart
I'm trying to use distutils to install a collection of modules in /usr/local/lib/python2.7/site-packages. My distribution (Fedora 15) doesn't include any /usr/local paths in sys.path, so the import fails when running the program. The distutils documentation suggests adding a $NAME.pth file to an ex

Re: Appending to dictionary of lists

2011-05-03 Thread Alex van der Spek
Thank you! Would never have found that by myself. "Paul Rubin" wrote in message news:7x7ha75zib@ruckus.brouhaha.com... "Alex van der Spek" writes: refd=dict.fromkeys(csvr.fieldnames,[]) ... I do not understand why this appends v to every key k each time. You have initialized every el

Re: Appending to dictionary of lists

2011-05-03 Thread Dan Stromberg
On Tue, May 3, 2011 at 12:56 PM, Paul Rubin wrote: > "Alex van der Spek" writes: > > refd=dict.fromkeys(csvr.fieldnames,[]) ... > > I do not understand why this appends v to every key k each time. > > You have initialized every element of refd to the same list. Try > >refd = dict((k,[]) fo

Re: Appending to dictionary of lists

2011-05-03 Thread Paul Rubin
"Alex van der Spek" writes: > refd=dict.fromkeys(csvr.fieldnames,[]) ... > I do not understand why this appends v to every key k each time. You have initialized every element of refd to the same list. Try refd = dict((k,[]) for k in csvr.fieldnames) instead. -- http://mail.python.org/mai

Appending to dictionary of lists

2011-05-03 Thread Alex van der Spek
I open a csv file and create a DictReader object. Subsequently, reading lines from this file I try to update a dictionary of lists: csvf=open(os.path.join(root,fcsv),'rb') csvr=csv.DictReader(csvf) refd=dict.fromkeys(csvr.fieldnames,[]) for row in csvr: for (k,v) in row.items(): re

Re: Appending to a file using Python API

2010-07-08 Thread Steven D'Aprano
On Wed, 07 Jul 2010 23:55:46 -0700, lavanya wrote: > Hello all, > > How do you append to a file using Python os::file APIs. So that it > appends to the content of the file. Not adding the content to the new > line. But just appends next to the exiting content of the file. > > Example : current c

Appending to a file using Python API

2010-07-08 Thread lavanya
Hello all, How do you append to a file using Python os::file APIs. So that it appends to the content of the file. Not adding the content to the new line. But just appends next to the exiting content of the file. Example : current content of file A B C if we append D to it, it should be A B C D N

Re: looping through values in a dictionary and appending to a list

2009-08-12 Thread Andre Engels
On Tue, Aug 11, 2009 at 8:17 PM, Krishna Pacifici wrote: > Nevermind, > got it. > > Sorry. > Krishna Pacifici 08/11/09 2:12 PM >>> > Hi, > I want to append the values of a dictionary to a list.  I have a dictionary > sec_dict_clean and I want to append the values to a list, but am having a > h

Re: looping through values in a dictionary and appending to a list

2009-08-11 Thread Krishna Pacifici
Nevermind, got it. Sorry. >>> Krishna Pacifici 08/11/09 2:12 PM >>> Hi, I want to append the values of a dictionary to a list. I have a dictionary sec_dict_clean and I want to append the values to a list, but am having a hard time looping through the values in the dictionary. I have tried som

Re: Appending to sys.path

2009-03-24 Thread Peter Otten
mhearne808[insert-at-sign-here]gmail[insert-dot-here]com wrote: > I have an application where I would like to append to the python path > dynamically. Below is a test script I wrote. Here's what I thought > would happen: > > 1) I run this script in a folder that is NOT already in PYTHONPATH > 2

Appending to sys.path

2009-03-24 Thread mhearne808[insert-at-sign-here]gmail[insert-dot-here]com
I have an application where I would like to append to the python path dynamically. Below is a test script I wrote. Here's what I thought would happen: 1) I run this script in a folder that is NOT already in PYTHONPATH 2) The script creates a subfolder called foo. 3) The script creates a file cal

Re: appending * to glob returns files with '*' !!

2008-09-23 Thread John [H2O]
IM, Y!M erikmaxfrancis > Many would be cowards if they had courage enough. > -- Thomas Fuller > -- > http://mail.python.org/mailman/listinfo/python-list > > -- View this message in context: http://www.nabble.com/appending-*-to-glob-returns-files-with-%27*%27-%21%21-tp19579121p19638699.html Sent from the Python - python-list mailing list archive at Nabble.com. -- http://mail.python.org/mailman/listinfo/python-list

Re: appending * to glob returns files with '*' !!

2008-09-21 Thread Erik Max Francis
John [H2O] wrote: I have a glob.glob search: searchstring = os.path.join('path'+'EN*') files = glob.glob(searchstring) for f in files: print f ___ This returns some files: EN082333 EN092334 EN* My routine cannot handle the '*' and it should'nt be returned anyway? :-/ A bug? No, it me

Re: appending * to glob returns files with '*' !!

2008-09-21 Thread alex23
On Sep 20, 6:37 am, "John [H2O]" <[EMAIL PROTECTED]> wrote: > My routine cannot handle the '*' and it should'nt be returned anyway? :-/ > > A bug? Not at all. That's the same behaviour you'll get if you do 'ls EN*'. In your case, you're asking to match on anything that begins with EN, a subset of

Re: appending * to glob returns files with '*' !!

2008-09-21 Thread Sean DiZazzo
On Sep 19, 1:37 pm, "John [H2O]" <[EMAIL PROTECTED]> wrote: > I have a glob.glob search: > > searchstring = os.path.join('path'+'EN*') shouldn't that be os.path.join(path, 'EN*') ? > ___ > This returns some files: > EN082333 > EN092334 > EN* Mine doesn't return that last string. > > My routin

appending * to glob returns files with '*' !!

2008-09-21 Thread John [H2O]
A bug? -- View this message in context: http://www.nabble.com/appending-*-to-glob-returns-files-with-%27*%27-%21%21-tp19579121p19579121.html Sent from the Python - python-list mailing list archive at Nabble.com. -- http://mail.python.org/mailman/listinfo/python-list

Re: creating and appending to a dictionary of a list of lists

2007-08-15 Thread pyscottishguy
On Aug 15, 8:08 am, Ant <[EMAIL PROTECTED]> wrote: > On Aug 15, 3:30 am, [EMAIL PROTECTED] wrote: > > > Hey, > > > I started with this: > > > factByClass = {} > > ... > > def update(key, *args): > > x = factByClass.setdefault(key, [[], [], [], [] ]) > > for i, v in enumerate(args): > >

Re: creating and appending to a dictionary of a list of lists

2007-08-15 Thread Ant
On Aug 15, 3:30 am, [EMAIL PROTECTED] wrote: > Hey, > > I started with this: > > factByClass = {} > ... > def update(key, *args): > x = factByClass.setdefault(key, [[], [], [], [] ]) > for i, v in enumerate(args): > x[i].append(v) > > Is there a better way? Well, the following is p

creating and appending to a dictionary of a list of lists

2007-08-14 Thread pyscottishguy
Hey, I started with this: factByClass = {} def update(key, x0, x1, x2, x3): x = factByClass.setdefault(key, [ [], [], [], [] ]) x[0].append(x0) x[1].append(x1) x[2].append(x2) x[3].append(x3) update('one', 1, 2, 3, 4) update('one', 5, 6, 7, 8) update('two', 9, 10, 11, 12) p

Re: problem with appending to a list, possibly mysqldb related

2006-08-31 Thread John Salerno
John Purser wrote: > On Thu, 31 Aug 2006 18:39:45 GMT > John Salerno <[EMAIL PROTECTED]> wrote: > >> John Salerno wrote: >>> John Purser wrote: >>> I'd say you had a record with a null value for the namefirst field. The join method don't like that. >>> Wow, you're right! I tried this:

Re: problem with appending to a list, possibly mysqldb related

2006-08-31 Thread John Purser
On Thu, 31 Aug 2006 18:39:45 GMT John Salerno <[EMAIL PROTECTED]> wrote: > John Salerno wrote: > > John Purser wrote: > > > >> I'd say you had a record with a null value for the namefirst field. > >> The join method don't like that. > > > > Wow, you're right! I tried this: > > > > if x[0] and

Re: problem with appending to a list, possibly mysqldb related

2006-08-31 Thread John Salerno
John Salerno wrote: > John Purser wrote: > >> I'd say you had a record with a null value for the namefirst field. >> The join method don't like that. > > Wow, you're right! I tried this: > > if x[0] and not x[0] == 'NULL': > > and sure enough it works after that. (Not sure if that's the best

Re: problem with appending to a list, possibly mysqldb related

2006-08-31 Thread John Salerno
John Purser wrote: > I'd say you had a record with a null value for the namefirst field. > The join method don't like that. Wow, you're right! I tried this: if x[0] and not x[0] == 'NULL': and sure enough it works after that. (Not sure if that's the best way to test, though. Just testing for

Re: problem with appending to a list, possibly mysqldb related

2006-08-31 Thread John Purser
On Thu, 31 Aug 2006 18:11:15 GMT John Salerno <[EMAIL PROTECTED]> wrote: > Here's the code: > > class MyFrame(wx.Frame): > > def __init__(self): > wx.Frame.__init__(self, None, wx.ID_ANY) > panel = wx.Panel(self) > dbconn = self.connect_db() > dbconn.exec

problem with appending to a list, possibly mysqldb related

2006-08-31 Thread John Salerno
p(*args, **kwargs) File "C:\Python24\myscripts\bbdata_access\bbdata.py", line 64, in OnInit frame = MyFrame() File "C:\Python24\myscripts\bbdata_access\bbdata.py", line 15, in __init__ name = ' '.join(x) TypeError: sequence item 0: expected string, Non

Re: Confused: appending to a list

2006-03-23 Thread DataSmash
Thanks for explaining and all the additional ideas! R.D. -- http://mail.python.org/mailman/listinfo/python-list

Re: Confused: appending to a list

2006-03-23 Thread Fredrik Lundh
"DataSmash" wrote: > I'm confused. Why is it that when I say "while len(list) < 5:", I get > 5 items in my list. > If I say "while len(list) < 6:", I get 6 items in the list and so on. > I would think if I said "less than 5", I would get 4 items. except that you're saying "as long as there are l

Re: Confused: appending to a list

2006-03-23 Thread adam . bachman
You're wanting it to stop when the len(list) == 4, right? The easiest way to change the logic would be to say while len(list) != 4: but that could get you into trouble later on. The problem with the len(list) < 5 expression is that the loop will run "one more time" as long as len(list)

Re: Confused: appending to a list

2006-03-23 Thread skip
R.D.> I'm confused. Why is it that when I say "while len(list) < 5:", I R.D.> get 5 items in my list. If I say "while len(list) < 6:", I get 6 R.D.> items in the list and so on. I would think if I said "less than R.D.> 5", I would get 4 items. Can anyone explain this? Thanks.

Re: Confused: appending to a list

2006-03-23 Thread Rene Pijlman
DataSmash: >I'm confused. Why is it that when I say "while len(list) < 5:", I get >5 items in my list. Because the last time when len(list) was < 5, the block of code following the while executed and did something to the list to give it a length >= 5 (otherwise the block of code would be executed

Re: Confused: appending to a list

2006-03-23 Thread Diez B. Roggisch
DataSmash wrote: > I'm confused. Why is it that when I say "while len(list) < 5:", I get > 5 items in my list. > If I say "while len(list) < 6:", I get 6 items in the list and so on. > I would think if I said "less than 5", I would get 4 items. > Can anyone explain this? Yes - you loop until the

Re: Confused: appending to a list

2006-03-23 Thread Felipe Almeida Lessa
Em Qui, 2006-03-23 às 08:31 -0800, DataSmash escreveu: > I'm confused. Why is it that when I say "while len(list) < 5:", I get > 5 items in my list. "while len(list) < 5:" implies that the loop will stop only when len(list) >= 5. HTH, -- Felipe. -- http://mail.python.org/mailman/listinfo/pyt

Confused: appending to a list

2006-03-23 Thread DataSmash
I'm confused. Why is it that when I say "while len(list) < 5:", I get 5 items in my list. If I say "while len(list) < 6:", I get 6 items in the list and so on. I would think if I said "less than 5", I would get 4 items. Can anyone explain this? Thanks. R.D. # Start an empty list list = [] while

Re: appending to a list via properties

2006-02-12 Thread Peter Otten
[Alex Martelli] > If you want to hoist for performance, you can hoist more: > > appenders = foo.append, qux.append > while some_condition: > for appender, anitem in zip(appenders, calculate_something()): > appender(anitem) You are of course claiming a performance improvement over Car

Re: appending to a list via properties

2006-02-11 Thread Alex Martelli
Xavier Morel <[EMAIL PROTECTED]> wrote: > Alex Martelli wrote: > > Carl Banks <[EMAIL PROTECTED]> wrote: > >... > >>> class better_list (list): > >>> tail = property(None, list.append) > >> This is an impressive, spiffy little class. > > > > Yes, nice use of property. > > > > Ale

Re: appending to a list via properties

2006-02-11 Thread Xavier Morel
Alex Martelli wrote: > Carl Banks <[EMAIL PROTECTED]> wrote: >... >>> class better_list (list): >>> tail = property(None, list.append) >> This is an impressive, spiffy little class. > > Yes, nice use of property. > > Alex I don't know, I usually see people considering that proper

Re: appending to a list via properties

2006-02-11 Thread Carl Banks
Alex Martelli wrote: > Carl Banks <[EMAIL PROTECTED]> wrote: >... > > > class better_list (list): > > > tail = property(None, list.append) > > > > This is an impressive, spiffy little class. > > Yes, nice use of property. > > > growing_lists = foo,qux > > while some_condition: > >

Re: appending to a list via properties

2006-02-11 Thread Alex Martelli
Carl Banks <[EMAIL PROTECTED]> wrote: ... > > class better_list (list): > > tail = property(None, list.append) > > This is an impressive, spiffy little class. Yes, nice use of property. > growing_lists = foo,qux > while some_condition: > for (s,x) in zip(growing_list,calculate

Re: appending to a list via properties

2006-02-11 Thread Carl Banks
Lonnie Princehouse wrote: > Here's a curious hack I want to put up for discussion. I'm thinking of > writing a PEP for it. A minor library change wouldn' t need a PEP. > Observation > - > I found myself using this construct for assembling multiple lists: > > foo = [] > qu

Re: appending to a list via properties

2006-02-10 Thread Larry Bates
Lonnie Princehouse wrote: > Here's a curious hack I want to put up for discussion. I'm thinking of > writing a PEP for it. > > Observation > - > I found myself using this construct for assembling multiple lists: > > foo = [] > qux = [] > > while some_condition: >

appending to a list via properties

2006-02-10 Thread Lonnie Princehouse
Here's a curious hack I want to put up for discussion. I'm thinking of writing a PEP for it. Observation - I found myself using this construct for assembling multiple lists: foo = [] qux = [] while some_condition: a, b = calculate_something() foo.append