Re: Division help in python
Chris Angelico wrote: > On Fri, Sep 7, 2012 at 10:53 PM, Ramyasri Dodla wrote: >> I am brand new to python. checking over basic stuff. I came across the >> problem while doing so. If any body aware of the problem, kindly respond me. >> > 5/10 >> 0 > - 5/10 >> -1 >> >> The second case also should yield a 'zero' but it is giving a -1 > > ... > The reason for this is that / (or in Python 3, //) rounds toward > negative infinity, not toward zero. This allows the modulo operator I think he means the non-obvious unary minus precedence. -- --- | Radovan Garabík http://kassiopeia.juls.savba.sk/~garabik/ | | __..--^^^--..__garabik @ kassiopeia.juls.savba.sk | --- Antivirus alert: file .signature infected by signature virus. Hi! I'm a signature virus! Copy me into your signature file to help me spread! -- http://mail.python.org/mailman/listinfo/python-list
Re: Division help in python
On 8/09/12 09:03:12, garabik-news-2005...@kassiopeia.juls.savba.sk wrote: > Chris Angelico wrote: >> On Fri, Sep 7, 2012 at 10:53 PM, Ramyasri Dodla wrote: >>> I am brand new to python. checking over basic stuff. I came across the >>> problem while doing so. If any body aware of the problem, kindly respond me. >>> >> 5/10 >>> 0 >> - 5/10 >>> -1 >>> >>> The second case also should yield a 'zero' but it is giving a -1 >> > ... > >> The reason for this is that / (or in Python 3, //) rounds toward >> negative infinity, not toward zero. This allows the modulo operator > > I think he means the non-obvious unary minus precedence. That seems unlikely. Unary minus has lower precedence in Python than in most other programming languages, but its precedence is higher than division, so this example doesn't show the difference. For example, in C unary opeators have the highest precedence. Yet -5/10 returns 0, not because of precedence, but because C rounds towards zero. Hope this helps, -- HansM -- http://mail.python.org/mailman/listinfo/python-list
Re: How to print something only if it exists?
Dave Angel wrote: > Would you like to define "exists" ? A list is not sparse, so all items > exist if their subscript is less than the length of the list. So all > you need to do is compare 2 to len(fld). > Yes, a I said a simple len(fld) will tell me if fld[2] 'exists' but it gets messy if I have to do it in the middle of the print sequence. > But perhaps there's another approach. Just what DO you want to print if > fld(1) exists, but fld(2) does not? Do you still want to print out day, > fld(1), and balance? Or do you want to skip balance as well? > Here's a sample of the file whose lines are being split() :- 01 JB 0.00Start of 2012, Initial balance 02 BB 0.00 13 ZB 0.00 I want to print out everything, it's just that in some cases there's no descriptive text (the bit that ends up in fld[2]). > if you literally want nothing printed for list elements beyond the end, > then I'd add some extra empty-strings to the end of the list. > > fld.extend("" * 5) > > Now, subscripts 0 through 4 inclusive will work, as specified. > That's probably the simplest approach, thank you. -- Chris Green -- http://mail.python.org/mailman/listinfo/python-list
Re: How to print something only if it exists?
On 09/08/2012 06:02 AM, tinn...@isbd.co.uk wrote: > Dave Angel wrote: >> Would you like to define "exists" ? A list is not sparse, so all items >> exist if their subscript is less than the length of the list. So all >> you need to do is compare 2 to len(fld). >> > Yes, a I said a simple len(fld) will tell me if fld[2] 'exists' but it > gets messy if I have to do it in the middle of the print sequence. > > >> But perhaps there's another approach. Just what DO you want to print if >> fld(1) exists, but fld(2) does not? Do you still want to print out day, >> fld(1), and balance? Or do you want to skip balance as well? >> > Here's a sample of the file whose lines are being split() :- > > 01 JB 0.00Start of 2012, Initial balance > 02 BB 0.00 > 13 ZB 0.00 > > I want to print out everything, it's just that in some cases there's > no descriptive text (the bit that ends up in fld[2]). > > >> if you literally want nothing printed for list elements beyond the end, >> then I'd add some extra empty-strings to the end of the list. >> >> fld.extend("" * 5) >> >> Now, subscripts 0 through 4 inclusive will work, as specified. >> > That's probably the simplest approach, thank you. > If there literally is only one missing field, and at the end, then you could use fld.append("") instead. Or better, you could do something like if len(fld) == 2 : fld.append(""") This may be longer, but at least it's in one place -- the place where the split is occurring. And it pretty much states that the fld2 is optional. You ought to consider what error to report if you encounter a line with missing fields that ARE required. -- DaveA -- http://mail.python.org/mailman/listinfo/python-list
Re: Accessing dll
On Sat, Sep 8, 2012 at 3:27 AM, Helpful person wrote: > On Sep 7, 5:16 am, Chris Angelico wrote: >> On Fri, Sep 7, 2012 at 1:44 AM, Helpful person wrote: >> > FYI >> >> > My Python version is 2.5.4 >> >> You may wish to upgrade, that's quite an old version. Unless >> something's binding you to version 2.x, I would strongly recommend >> migrating to 3.2 or 3.3. >> >> ChrisA > > Upgrading is not possible due to the large number of programs using > the early version. Sure. At least you've considered it. :) Do look into moving up to 2.7, at least, though. And of course, you can have multiple Pythons installed simultaneously, allowing you to migrate only when you're ready. ChrisA -- http://mail.python.org/mailman/listinfo/python-list
Re: Comparing strings from the back?
On 2012-09-08, Steven D'Aprano wrote: > On Fri, 07 Sep 2012 19:10:16 +, Oscar Benjamin wrote: > >> On 2012-09-07, Steven D'Aprano >> wrote: >> >> >> Would you say, then, that dict insertion is O(N)? > > Pedantically, yes. > > But since we're allowed to state (or even imply *wink*) whatever > assumptions we like, we're allowed to assume "in the absence of > significant numbers of hash collisions" and come up with amortized O(1) > for dict insertions and lookups. > > (Provided, of course, that your computer has an infinite amount of > unfragmented memory and the OS never starts paging your dict to disk. > Another unstated assumption that gets glossed over when we talk about > complexity analysis -- on real world computers, for big enough N, > *everything* is O(2**N) or worse.) > > Big Oh analysis, despite the formal mathematics used, is not an exact > science. Essentially, it is a way of bringing some vague order to hand- > wavy estimates of complexity, and the apparent mathematical rigour is > built on some awfully shaky foundations. But despite that, it actually is > useful. > > Coming back to strings... given that in any real-world application, you > are likely to have some string comparisons on equal strings and some on > unequal strings, and more importantly you don't know which are which > ahead of time, which attitude is less likely to give you a nasty surprise > when you run your code? > > "I have many millions of 100K strings to compare against other 100K > strings, and string comparisons are O(1) so that will be fast." > > "I have many millions of 100K strings to compare against other 100K > strings, and string comparisons are O(N) so that will be slow, better > find another algorithm." True. I can't think of a situation where I've used string comparisons directly in any text heavy code. Rather, I would use a dict or a set (or a regex) and hash(str) is always O(N). > > > Remember too that "for small enough N, everything is O(1)". Getting hung > up on Big Oh is just as much a mistake as ignoring it completely. > > I can't think of a situation in my own work where O(N) vs O(1) string comparisons would cause a significant problem (except perhaps in libraries that I use but didn't write). However, I can find a number of cases where I compare numpy.ndarrays for equality. For example, I found if np.all(a == b): in some code that I recently wrote. Although np.all() short-circuits, a==b does not so that line forces O(N) behaviour onto a situation where the average case can be better. Unfortunately numpy doesn't seem to provide a short-circuit equals() function. array_equal() is what I want but it does the same as the above. In future, I'll consider using something like def cmparray(a, b): return a.shape == b.shape and a.dtype == b.dtype and buffer(a) == buffer(b) to take advantage of (what I assume are) short-circuit buffer comparisons. >> Since string comparison is only useful if the strings can be equal or >> unequal, the average case depends on how often they are equal/unequal as >> well as the average complexity of both. For random strings the frequency >> of equal strings decreases very fast as N increases so that the >> comparison of random strings is O(1). > > But that is not an upper bound, and Big Oh analysis is strictly defined > in terms of upper bounds. It is an upper bound, but it is an upper bound on the *expectation value* assuming a particular distribution of inputs, rather than an upper bound on all possible inputs. >>> (I'm talking about the average here -- the actual number of comparisons >>> can range all the way up to N, but the average is <= 2.) The average is actually bounded by 1 / (1 - p) where p is the probability that two characters match. This bound can be arbitrarily large as p approaches 1 as would be the case if, say, one character was much more likely than others. The particular assumption that you have made p = 1/M where M is the number of characters is actually the *smallest* possible value of p. For non-uniform real data (English words for example) p is significantly greater than 1/M but in a strict bounds sense we should say that 1/M <= p <= 1. Oscar -- http://mail.python.org/mailman/listinfo/python-list
Re: PExpect Cross-Platform Alternative
Hello Steve, I was wondering if you had working examples of your automated tasks in python ... I am asking because I am starting out in Python, and I believe I can save my company time and headaches by automating a lot of the tasks we have. I would like to study your code and see if I can use it in our environment. Basically, our tasks involve downloading the firmwares and then installing it, restarting the processes and making sure all the processes are running. Its on Linux servers and HP switches. I cannot seem to find any example code to learn from ... I have IT guys who know Perl, but I do not want to go down that route. Many thanks, Zac. On Thursday, February 11, 2010 8:57:39 PM UTC+1, Steve Holden wrote: > Nathan Farrar wrote: > > Hello Community, > > > > Recently I've been automating lots of network operations tasks via > > simple python scripts. Originally, I utilized paramiko but found that > > the module had issues working with cisco equipment. I switched to > > pexpect and things have worked wonderfully since (I've been running this > > scripts in a Linux environment). However, I was just tasked to get > > these scripts running in a windows environment and to my dismay very > > quickly realized that pexpect is not cross platform compatible. > > > > Am I stuck, or are there solutions out there? > > > It works pretty well under Cygwin (which also improves your Windows > scripting environment no end). Any chance you would be allowed to > install that? > > regards > Steve > -- > Steve Holden +1 571 484 6266 +1 800 494 3119 > PyCon is coming! Atlanta, Feb 2010 http://us.pycon.org/ > Holden Web LLC http://www.holdenweb.com/ > UPCOMING EVENTS:http://holdenweb.eventbrite.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Language workbench written in python3
On Friday, 7 September 2012 01:18:45 UTC+5:30, Nestor Arocha wrote: > On Thursday, September 6, 2012 2:53:15 PM UTC+1, Ramchandra Apte wrote: > > > On Thursday, 6 September 2012 19:16:38 UTC+5:30, Dave Angel wrote: > > > > > > > On 09/06/2012 09:34 AM, Ramchandra Apte wrote: > > > > > > > > > > > > > > > Translator means what precisely? > > > > > > > > > > > > > > > > > > > > > > > > > > > > Examples of translators include compilers, assemblers, and > > > > > > > > > > > > > > interpreters. They also include implementations like cfront, which > > > > > > > > > > > > > > translates from one high-level language to another lower-level > > > > > > > > > > > > > > language. (high and low being relative) > > > > > > > > > > > > > > > > > > > > > > > > > > > > -- > > > > > > > > > > > > > > > > > > > > > > > > > > > > DaveA > > > > > > > > > > > > Is conversion from Python to C++ possible from this project? > > > > No, it is not currently possible for several reasons: > >* current parser implementation is a recursive descent parser. I haven't > implemented an LR parser yet, although PLY lexers and parsers are supported. > >* Syntax Directed Translator is not fully implemented either. > >* Parser Trees are supported, but there is no clear method defined for > converting them into ASTs (like antlr grammars) > >* Even with AST and SDT support, a Python to C++ translator will require > more complex tools and a lot of coding. > > > > This tool is oriented to small DSLs parsing and translation; grammars like > Python or C++ are too complex for the current implementation I was thinking I could use it for my Python to C/C++ converter - py2c @ code.google.com/p/py2c (py2c would love a developer so join and contribute) -- http://mail.python.org/mailman/listinfo/python-list
Re: Looking for an IPC solution
On Friday, 7 September 2012 02:25:15 UTC+5:30, Dave Angel wrote: > On 09/06/2012 04:33 PM, Dennis Lee Bieber wrote: > > > > > > > > Note that this difference mainly applies to how the processes are > > > themselves are created... How the library wraps shared data is > > > possibly different (I've never understood how a "fork" process can > > > avoid memory conflicts if it has write access to common virtual memory > > > blocks). > > Here's an approximate description of fork, at least for the memory > > aspects. During a fork, the virtual memory table is copied (that's > > descriptors for all mapped and allocated memory) but the memory itself > > is NOT. All the new descriptors are labeled "COW" (copy-on-write). As > > that process executes, the first time it writes in a particular memory > > block, the OS gets a memory fault, which it fixes by allocating a block > > of the same size, copying the memory block to the new one, and labeling > > it read/write. Subsequent accesses to the same block are normal, with no > > trace of the fork remaining. > > > > Now, there are lots of details that this blurs over, but it turns out > > that many times the new process doesn't change very much. For example, > > all the mappings to the executable and to shared libraries are > > theoretically readonly. In fact, they might have also been labeled COW > > even for the initial execution of the program. Another place that's > > blurry is just what the resolution of this table actually is. There are > > at least two levels of tables. The smallest increment on the Pentium > > family is 4k. > > > > > > -- > > > > DaveA >From my OS development experience, there are two sizes of pages - 4K and 1 byte -- http://mail.python.org/mailman/listinfo/python-list
Re: set and dict iteration
On Monday, September 3, 2012 8:59:16 PM UTC-5, Steven D'Aprano wrote: > On Mon, 03 Sep 2012 21:50:57 -0400, Dave Angel wrote: > > > > > On 09/03/2012 09:26 PM, Steven D'Aprano wrote: > > > > >> An unsigned C int can count up to 4,294,967,295. I propose that you say > > >> that is enough iterators for anyone, and use a single, simple, version > > >> counter in the dict and the iterator. If somebody exceeds that many > > >> iterators to a single dict or set, > > > > > > I think you have the count confused. it has to be a count of how many > > > changes have been made to the dict or set, not how many iterators exist. > > > > Oops, yes you are absolutely right. It's a version number, not a count of > > iterators. > > > > > > -- > > Steven Hello. We have a number of proposed solutions so far. 1) Collection of iterators a) Linked list i) Uncounted references ii) Counted references iii) Weak references b) Weak set 2) Serial index / timestamp a) No overflow - Python longs b) Overflow - C ints / shorts / chars c) Reset index if no iterators left 3) Iterator count - Raise exception on set modifications, not iteration Note, "2b" still leaves the possibility of missing a case and letting an error pass silently, as the current behavior does. The rest catch the error 100% of the time. Anyway, I plan to develop the above patch for the 'dict' class. Would anyone like to take over or help me do it? -- http://mail.python.org/mailman/listinfo/python-list
Re: Comparing strings from the back?
On 09/06/2012 10:33 AM, Steven D'Aprano wrote: On Wed, 05 Sep 2012 22:47:14 +, Oscar Benjamin wrote: I may have been overly-conservative earlier when I said that on average string equality has to compare half the characters. I thought I had remembered that from a computer science textbook, but I can't find that reference now, so possibly I was thinking of something else. (String searching perhaps?). Yeah I think you mixed it up with searching an entry in an unsorted list of length N That's one of the most common N/2 cases, that one hits daily with many straight forward implementations -- http://mail.python.org/mailman/listinfo/python-list
Re: Comparing strings from the back?
On 09/07/2012 06:06 AM, Steven D'Aprano wrote: On Thu, 06 Sep 2012 06:07:38 -0400, Dave Angel wrote: Also of some interest is the best case: O(1) for unequal strings (they differ at the first character) and O(N) for equal strings. The worst case is O(N) or N characters the average case is O(1) or two characters. For denial of service attacks or systems, that are NEVER allowed to fail the worst case is important. For most other cases the average complexity counts. However I still wonder for how many applications the complexity of string comparisons would be the limiting factor. -- http://mail.python.org/mailman/listinfo/python-list
AttributeError: 'list' object has no attribute 'lower'
On page 77 of the book natural language processing with Python, we have such an exercise: The polysemy of a word is the number of senses it has. Using WordNet, we can determine that the noun doghas seven senses with len(wn.synsets('dog', 'n')). Compute the average polysemy of nouns, verbs, adjectives, and adverbs according to WordNet.http://nltk.googlecode.com/svn/trunk/doc/book/ch02.html I wrote the following function to solve it. However, it pops up "AttributeError: 'list' object has no attribute 'lower'". Quite confused, I supposed [synset.lemma_names for synset in synset_list] has made all the lemma into a list, hasn't it? >>> def average_polysemy(pos): synset_list = list(wn.all_synsets(pos)) lemma_list = [synset.lemma_names for synset in synset_list] sense_number = 0 for lemma in lemma_list: sense_number_new = len(wn.synsets(lemma, pos)) sense_number = sense_number + sense_number_new return sense_number/len(synset_list) >>> average_polysemy('n') Traceback (most recent call last): File "", line 1, in average_polysemy('n') File "", line 6, in average_polysemy sense_number_new = len(wn.synsets(lemma, pos)) File "C:\Python27\lib\site-packages\nltk\corpus\reader\wordnet.py", line 1191, in synsets lemma = lemma.lower() AttributeError: 'list' object has no attribute 'lower' Thanks for your tips -- http://mail.python.org/mailman/listinfo/python-list
wordnet NLTK Re: AttributeError: 'list' object has no attribute 'lower'
I don't know why lemma_list = [synset.lemma_names for synset in synset_list] will lead to such an error. I have to use extend to solve the problem for lemma_list. The following codes are successful, take all the nouns as an example: >>> def average_polysemy(pos): synset_list = list(wn.all_synsets(pos)) sense_number = 0 lemma_list = [] for synset in synset_list: lemma_list.extend(synset.lemma_names) for lemma in lemma_list: sense_number_new = len(wn.synsets(lemma, pos)) sense_number = sense_number + sense_number_new return sense_number/len(synset_list) >>> average_polysemy('n') 3 > > I wrote the following function to solve it. However, it pops up > "AttributeError: 'list' object has no attribute 'lower'". Quite confused, I > supposed [synset.lemma_names for synset in synset_list] has made all the > lemma into a list, hasn't it? > > > > >>> def average_polysemy(pos): > > synset_list = list(wn.all_synsets(pos)) > > lemma_list = [synset.lemma_names for synset in synset_list] > > sense_number = 0 > > for lemma in lemma_list: > > sense_number_new = len(wn.synsets(lemma, pos)) > > sense_number = sense_number + sense_number_new > > return sense_number/len(synset_list) > > > > >>> average_polysemy('n') > > > > Traceback (most recent call last): > > File "", line 1, in > > average_polysemy('n') > > File "", line 6, in average_polysemy > > sense_number_new = len(wn.synsets(lemma, pos)) > > File "C:\Python27\lib\site-packages\nltk\corpus\reader\wordnet.py", line > 1191, in synsets > > lemma = lemma.lower() > > AttributeError: 'list' object has no attribute 'lower' > > > > Thanks for your tips -- http://mail.python.org/mailman/listinfo/python-list
Re: AttributeError: 'list' object has no attribute 'lower'
In article , Token Type wrote: > I wrote the following function to solve it. However, it pops up > "AttributeError: 'list' object has no attribute 'lower'". Quite confused, I > supposed [synset.lemma_names for synset in synset_list] has made all the > lemma into a list, hasn't it? I'm not familiar with that library, but here's a few general ideas to help you figure out what's going on. First, I don't understand this code: > synset_list = list(wn.all_synsets(pos)) > lemma_list = [synset.lemma_names for synset in synset_list] It looks like you're taking an iterable, converting it to a list, just so you can iterate over it again. Why not the simpler: > lemma_list = [synset.lemma_names for synset in wn.all_synsets(pos)] ? But, I'm also confused about what lemma_list is supposed to end up being. The name "lemma_names" is plural, making me think it returns a list of something. And then you build those up into a list of lists? In fact, I'm guessing that's your problem. I think you're ending up with a list of lists of strings, when you think you're getting a list of strings. My suggestion is to print out all the intermediate data structures (synset_list, lemma_list, etc) and see what they look like. If the structures are simple, just plain print will work, but for more complicated structures, pprint.pprint() is a life saver. Another possibility is to assert that things are what you expect them to be. Something like: assert isinstance(synset_list, list) assert isinstance(lemma_list, list) assert isinstance(lemma_list[0], str) and so on. > for lemma in lemma_list: > sense_number_new = len(wn.synsets(lemma, pos)) > sense_number = sense_number + sense_number_new > return sense_number/len(synset_list) > > >>> average_polysemy('n') > > Traceback (most recent call last): > File "", line 1, in > average_polysemy('n') > File "", line 6, in average_polysemy > sense_number_new = len(wn.synsets(lemma, pos)) > File "C:\Python27\lib\site-packages\nltk\corpus\reader\wordnet.py", line > 1191, in synsets > lemma = lemma.lower() > AttributeError: 'list' object has no attribute 'lower' > > Thanks for your tips -- http://mail.python.org/mailman/listinfo/python-list
Re: Parsing ISO date/time strings - where did the parser go?
* Roy Smith wrote: > The third is that I never use methods I can't figure out how to > pronounce. here: strip'time nd -- Flhacs wird im Usenet grundsätzlich alsfhc geschrieben. Schreibt man lafhsc nicht slfach, so ist das schlichtweg hclafs. Hingegen darf man rihctig ruhig rhitcgi schreiben, weil eine shcalfe Schreibweise bei irhictg nicht als shflac angesehen wird. -- Hajo Pflüger in dnq -- http://mail.python.org/mailman/listinfo/python-list
Looking for download link for ArcPY
Hi, All, Can anyone send me the link for downloading ArcPY? I came across it before, but can not find it anymore. Regards. David-- http://mail.python.org/mailman/listinfo/python-list
Re: simple client data base
On Mon, 03 Sep 2012 16:50:14 +0200, Peter Otten <__pete...@web.de> wrote: >Chris Angelico wrote: > >> You may also be needlessly reinventing the wheel. Aren't there already >> several million basic contact databases around? Why roll your own? > >To learn a thing or two, and to stick it to the defeatists ;) Yes, that's the reason. I need to learn something. Without getting into the complexities of relational database's for now. -- http://mail.python.org/mailman/listinfo/python-list
Re: simple client data base
On Wed, 5 Sep 2012 05:57:24 -0700 (PDT), Ramchandra Apte wrote: >On Monday, 3 September 2012 19:42:21 UTC+5:30, Manatee wrote: >> Hello all, I am learning to program in python. I have a need to make a >> >> program that can store, retrieve, add, and delete client data such as >> >> name, address, social, telephone number and similar information. This >> >> would be a small client database for my wife who has a home accounting >> >> business. >> >> >> >> I have been reading about lists, tuples, and dictionary data >> >> structures in python and I am confused as to which would be more >> >> appropriate for a simple database. >> >> >> >> I know that python has real database capabilities but I'm not there >> >> yet and would like to proceed with as simple a structure as possible. >> >> >> >> Can anyone give me some idea's or tell me which structure would be >> >> best to use? >> >> >> >> Maybe its a combination of structures? I need some help. >> >> >> >> Thanks for your help. > >If there are not more than 1000 records you can just use `pickle` Yes, not more than a 1000. My wifes clients only number 300. So not much of a problem for now I guess. I can't see it growing to double that, because 300 clients takes alot of work as it is. -- http://mail.python.org/mailman/listinfo/python-list
Re: simple client data base
On Tue, 04 Sep 2012 04:25:14 +0200, Thomas 'PointedEars' Lahn wrote: >Mark R Rivet wrote: > >> Hello all, I am learning to program in python. I have a need to make a >> program that can store, retrieve, add, and delete client data such as >> name, address, social, telephone number and similar information. This >> would be a small client database for my wife who has a home accounting >> business. >> >> I have been reading about lists, tuples, and dictionary data >> structures in python and I am confused as to which would be more >> appropriate for a simple database. >> >> I know that python has real database capabilities but I'm not there >> yet and would like to proceed with as simple a structure as possible. >> >> Can anyone give me some idea's or tell me which structure would be >> best to use? >> >> Maybe its a combination of structures? I need some help. > >The data types that would choose are defined by your requirements and how >well a data type meets your requirements. > >I can imagine: In a database you would want quick access to data. You would >want to access fields primarily by name. You would also want to filter data >by various criteria. > >Therefore, it appears to me that it would be best if your records were >dictionaries or dictionary-like objects, and your recordsets were lists of >records, like so > >#!/usr/bin/env python3 > >from datetime import date > >data = [ > { >'lastname': 'Doe', >'firstname': 'John', >'sn':'123-451-671-890', >'birthdata': date(2000, 1, 2) > }, > { >'lastname': 'Doe', >'firstname': 'Jane', >'sn':'409-212-582-452', >'birthdata': date(2001, 2, 3) > }, >] > >- You could quickly access the second record with data[1]. >- You could access the 'lastname' field of the second record with > data[1]['lastname'] >- You could get a list of records where the person is born before 2001 CE > with filter(lambda record: record['birthdate'] < date(2001, 1, 1), data) > >The advantage of dictionaries over dictionary-like objects is that they are >easily extensible and that the memory footprint is probably lower (CMIIW); >the disadvantage is slightly more complicated syntax and that you have to >keep track of the keys. Therefore, you might want to consider instantiating >a Record class instead; in its simplest form: > >class Record(object): > def __init__(self, lastname, firstname, sn=None, birthdate=None): >self.lastname = lastname >self.firstname = firstname >self.sn = str(sn) >self.birthdate = birthdate > >data = [ > Record(lastname='Doe', firstname='John', sn='123-451-671-890', > birthdate=date(2000, 1, 2)), > Record(lastname='Doe', firstname='Jane', sn='409-212-582-452', > birthdate=date(2001, 2, 3)) >] > >- You could access the 'lastname' property of the second record with > data[1].lastname >- You get a list of records where the person is born before 2001 CE with > list(filter(lambda record: record.birthdate < date(2001, 1, 1), data)) > (in Python 2.x without list()) > >However, if you want your program to manipulate the data *persistently*. as >it will probably be needed for business, you will need to also store it >somewhere else than in the volatile memory in which these data structures >are usually stored. The most simple way would be to store and parse the >string representation of the objects. > >Production-quality implementations of those and other concepts already >exist, of course, but using something finished and polished does not provide >as much learning experience. > >HTH Now this is the kind of answer I was hoping for. This gives me food for thought. Now I have some Ideas of how to appproach this thing. I know that there are solutions to this problem already but I need to do my own before I can really use anything better. Thanks for the help. I fully intend to go with a real realational database, but not now. My version 1 will be a console interface and dictionaries, lists and pickling. Version 2 will be with a GUI written in tkinter, and better and better. I just want to learn how to lay the bricks before I start using prefab walls. -- http://mail.python.org/mailman/listinfo/python-list
Re: simple client data base
On Thu, 6 Sep 2012 01:57:04 -0700 (PDT), Bryan wrote: >Mark R Rivet wrote: >> Hello all, I am learning to program in python. I have a need to make a >> program that can store, retrieve, add, and delete client data such as >> name, address, social, telephone number and similar information. This >> would be a small client database for my wife who has a home accounting >> business. > >Among programming languages Python is exceptionally easy to learn, and >rocks for the kind of app you describe, but your goal is not >realistic. Simple is better than complex, but what you can build at >this point is far from what a professional accountant with her own >business needs from a client database manager. > >> I have been reading about lists, tuples, and dictionary data >> structures in python and I am confused as to which would be more >> appropriate for a simple database. > >Those are good classes to read about, and I dare say that most >Pythoneers at some time faced confusion as to which were most >appropriate for the problem at hand. You'd need of all them and more, >a whole freak'in bunch more, to build a professional quality contact >manager app. > >> I know that python has real database capabilities but I'm not there >> yet and would like to proceed with as simple a structure as possible. >> >> Can anyone give me some idea's or tell me which structure would be >> best to use? >> >> Maybe its a combination of structures? I need some help. > >comp.lang.python tries to be friendly and helpful, and to that end >responders have read and answered your question as directly as >possible. There's good stuff available for Python. > >Mark, there is absolutely no chance, no how, no way, that your stated >plan is a good idea. Fine CRM apps are available for free; excellent >ones for a few dollars. You're reading about lists, tuples, and >dictionary data? Great, but other home accounting businesses have >their client databases automatically synced with their smart-phones >and their time-charging and their invoicing. > >-Bryan Well I have to say that this is most discouraging. I should give up learning to program. I don't have a chance at all. Thanks. -- http://mail.python.org/mailman/listinfo/python-list
Re: Looking for download link for ArcPY
On 09/08/2012 03:19 PM, David Shi wrote: > Hi, All, > > Can anyone send me the link for downloading ArcPY? > > I came across it before, but can not find it anymore. > > Regards. > > David > Try this link; it looks promising. http://www.arcgis.com/home/item.html?id=3a790cd717514f4689ae197c79205805 Or ask mgwhit...@gmail.com, who started a thread yesterday asking for help with what turned out to be arcpy. He didn't give a link in his question (subject: "Defining features in a list"), but if you emailed him, he'd probably help. -- DaveA -- http://mail.python.org/mailman/listinfo/python-list
Re: simple client data base
>>Mark R Rivet wrote: >>> Hello all, I am learning to program in python. I have a need to make a >>> program that can store, retrieve, add, and delete client data such as >>> name, address, social, telephone number and similar information. This >>> would be a small client database for my wife who has a home accounting >>> business. >> >>Among programming languages Python is exceptionally easy to learn, and >>rocks for the kind of app you describe, but your goal is not >>realistic. Simple is better than complex, but what you can build at >>this point is far from what a professional accountant with her own >>business needs from a client database manager. I am married, and I wonder what his wife will think if she discovers that she could have had her solution yesterday. My wife would not tolerate the roll-your-own approach, but every marriage is different. -- http://mail.python.org/mailman/listinfo/python-list
Re: Looking for download link for ArcPY
Didn't see the download link, but a quick google search yielded this: http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//002z000800 It seems it's part of a larger program ArcGIS 10. These are the docs for it, and how to use it, so it should be in there somewhere. -- Best Regards, David Hutto *CEO:* *http://www.hitwebdevelopment.com* -- http://mail.python.org/mailman/listinfo/python-list
Re: set and dict iteration
Am 19.08.2012 00:14 schrieb MRAB: Can someone who is more familiar with the cycle detector and cycle breaker, help prove or disprove the above? In simple terms, when you create an immutable object it can contain only references to pre-existing objects, but in order to create a cycle you need to make an object refer to another which is created later, so it's not possible to create a cycle out of immutable objects. Yes, but if I add a list in-between, I can create a refcycle: a = [] b = (a,) a.append(b) So b is a tuple consisting of one list which in turn contains b. It is not a direct cycle, but an indirect one. Or would that be detected via the list? Thomas -- http://mail.python.org/mailman/listinfo/python-list
Re: simple client data base
Mark R Rivet writes: >>ones for a few dollars. You're reading about lists, tuples, and >>dictionary data? Great, but other home accounting businesses have >>their client databases automatically synced with their smart-phones >>and their time-charging and their invoicing. > Well I have to say that this is most discouraging. I should give up > learning to program. I don't have a chance at all. Thanks. I think the idea is just to start with something simpler. If you are interested in mechanical engineering, then building an automobile from scratch, machining all the parts yourself etc., would be an ill-advised choice as a first project. It's the same way with programming. -- http://mail.python.org/mailman/listinfo/python-list
Re: simple client data base
On Sat, Sep 8, 2012 at 3:11 PM, Paul Rubin wrote: > I think the idea is just to start with something simpler. If you are > interested in mechanical engineering, then building an automobile from > scratch, machining all the parts yourself etc., would be an ill-advised > choice as a first project. It's the same way with programming. I'm wondering what his backup plan is for the information, in case his database somehow messes things up. It's smarter to start with something that's not vital to the functioning of your wife's business. Ian -- http://mail.python.org/mailman/listinfo/python-list
Initial pointers for web based LAMP installation
Hi all This is what I want to do: On a LAMP server, people are able to request for an instance of a particular LAMP application by submitting a few options. That involves creating a couple of directories, getting the code, writing a config file, setting file permissions and creating a mysql user. I can do it except for the interface with a shell script. Because of the web interface I was told that python is the right language but I have no knowledge in python. Could you pl. give me the initial pointers for this scheme? Matteo -- http://mail.python.org/mailman/listinfo/python-list
Re: simple client data base
On Sat, 08 Sep 2012 13:11:27 -0700, Paul Rubin wrote: > Mark R Rivet writes: >>>ones for a few dollars. You're reading about lists, tuples, and >>>dictionary data? Great, but other home accounting businesses have their >>>client databases automatically synced with their smart-phones and their >>>time-charging and their invoicing. >> Well I have to say that this is most discouraging. I should give up >> learning to program. I don't have a chance at all. Thanks. > > I think the idea is just to start with something simpler. If you are > interested in mechanical engineering, then building an automobile from > scratch, machining all the parts yourself etc., would be an ill-advised > choice as a first project. It's the same way with programming. And he has it backwards anyway, IMHO. If he wants a client database and insists on building it himself, he should start with the relational database and work up; not from the GUI and try to work down with silly solutions like pickling. -- http://mail.python.org/mailman/listinfo/python-list
Re: simple client data base
On Sat, 2012-09-08, Mark R Rivet wrote: > On Thu, 6 Sep 2012 01:57:04 -0700 (PDT), Bryan > wrote: ... >>comp.lang.python tries to be friendly and helpful, and to that end >>responders have read and answered your question as directly as >>possible. There's good stuff available for Python. >> >>Mark, there is absolutely no chance, no how, no way, that your stated >>plan is a good idea. Fine CRM apps are available for free; excellent >>ones for a few dollars. You're reading about lists, tuples, and >>dictionary data? Great, but other home accounting businesses have >>their client databases automatically synced with their smart-phones >>and their time-charging and their invoicing. >> >>-Bryan > Well I have to say that this is most discouraging. I should give up > learning to program. I don't have a chance at all. Thanks. He's saying you don't have a chance, but he's *not* telling you to give up programming. Personal reflection: it's risky to make friends and relatives depend on the success of your hobby projects. I got away with it once or twice (under special circumstances) but it might equally well have ended with resentment. "Why did he sell this crap idea to me? Now I'm stuck with it." /Jorgen -- // Jorgen GrahnO o . -- http://mail.python.org/mailman/listinfo/python-list
Re: set and dict iteration
On 8/09/12 22:06:08, Thomas Rachel wrote: > Am 19.08.2012 00:14 schrieb MRAB: > >>> Can someone who is more familiar with the cycle detector and cycle >>> breaker, help prove or disprove the above? >>> >> In simple terms, when you create an immutable object it can contain >> only references to pre-existing objects, but in order to create a cycle >> you need to make an object refer to another which is created later, so >> it's not possible to create a cycle out of immutable objects. > > Yes, but if I add a list in-between, I can create a refcycle: > > a = [] > b = (a,) > a.append(b) > > So b is a tuple consisting of one list which in turn contains b. > > It is not a direct cycle, but an indirect one. It's a cycle and it contains an immutable object, but it's not a cycle consisting of immutable objects only. As MRAB was arguing: it is not possbible to create a cycle consisting of immutable objects only (unless you do unspeakable things at the C level). -- HansM -- http://mail.python.org/mailman/listinfo/python-list
Re: set and dict iteration
On 08/09/2012 21:06, Thomas Rachel wrote: Am 19.08.2012 00:14 schrieb MRAB: Can someone who is more familiar with the cycle detector and cycle breaker, help prove or disprove the above? In simple terms, when you create an immutable object it can contain only references to pre-existing objects, but in order to create a cycle you need to make an object refer to another which is created later, so it's not possible to create a cycle out of immutable objects. Yes, but if I add a list in-between, I can create a refcycle: a = [] b = (a,) a.append(b) So b is a tuple consisting of one list which in turn contains b. It is not a direct cycle, but an indirect one. Or would that be detected via the list? The quote was: ''' ...The tuple type does not implement a tp_clear function, because it’s possible to prove that no reference cycle can be composed entirely of tuples. ''' Note: "composed entirely of tuples". Or, in general, composed entirely of immutables. Lists are not immutable, therefore the proof does not apply. -- http://mail.python.org/mailman/listinfo/python-list
Re: set and dict iteration
On Thursday, August 23, 2012 1:11:14 PM UTC-5, Steven D'Aprano wrote: > On Thu, 23 Aug 2012 09:49:41 -0700, Aaron Brady wrote: > > > > [...] > > > The patch for the above is only 40-60 lines. However it introduces two > > > new concepts. > > > > > > The first is a "linked list", a classic dynamic data structure, first > > > developed in 1955, cf. http://en.wikipedia.org/wiki/Linked_list . > > > Linked lists are absent in Python > > > > They certainly are not. There's merely no named "linked list" class. > > > > Linked lists are used by collections.ChainMap, tracebacks, xml.dom, > > Abstract Syntax Trees, and probably many other places. (Well, technically > > some of these are trees rather than lists.) You can trivially create a > > linked list: > > > > x = [a, [b, [c, [d, [e, None] > > > > is equivalent to a singly-linked list with five nodes. Only less > > efficient. > [snip.] > > -- > > Steven That's not totally true. Your formulation is equivalent to a single-linked list, but a double-linked list can't be represented as an expression because it contains reference cycles. Single and double-linked lists have the same requirements for inserting a new node. For instance: [a, [b, [c, [d, [e, None] [a, [a2, [b, [c, [d, [e, None]] However, to remove a node, the single-linked list requires iterating over the entire list to find the predecessor of the target, whereas the double-linked list already contains that information. [a, [b, [c, [d, [e, None] [a, [b, [c, [e, None The difference might be moot in some applications, such as if we only remove nodes when we're already iterating. Memory constraints were more severe 50 years ago when the structure was developed. The difference between one pointer and two in a structure could have a big impact in repetition. The single-linked list admits more easily of recursive algorithms as well. -- http://mail.python.org/mailman/listinfo/python-list
Re: AttributeError: 'list' object has no attribute 'lower'
On 08Sep2012 13:45, Roy Smith wrote: | First, I don't understand this code: | | In article , | Token Type wrote: | > synset_list = list(wn.all_synsets(pos)) | > lemma_list = [synset.lemma_names for synset in synset_list] | | It looks like you're taking an iterable, converting it to a list, just | so you can iterate over it again. Why not the simpler: | | > lemma_list = [synset.lemma_names for synset in wn.all_synsets(pos)] Speaking for myself, when I write something like that it is because I need to iterate over it twice or more. Often I'll make a tuple instead of a list in that case, too, to avoid certain types of accidents. | ? But, I'm also confused about what lemma_list is supposed to end up | being. The name "lemma_names" is plural, making me think it returns a | list of something. And then you build those up into a list of lists? | | In fact, I'm guessing that's your problem. I think you're ending up | with a list of lists of strings, when you think you're getting a list of | strings. In my case, I have most often had this error (.lower or its equivalent) when I've accidentally converted a string into a list of characters; easy to do because strings are themselves iterables, yielding a sequence of single character strings:-) It is usually an accident from getting my nesting wrong somewhere. | My suggestion is to print out all the intermediate data structures | (synset_list, lemma_list, etc) and see what they look like. If the | structures are simple, just plain print will work, but for more | complicated structures, pprint.pprint() is a life saver. | | Another possibility is to assert that things are what you expect them to | be. Something like: | | assert isinstance(synset_list, list) | assert isinstance(lemma_list, list) | assert isinstance(lemma_list[0], str) | | and so on. +1 to all of this, too. Cheers, -- Cameron Simpson Too much of a good thing is never enough. - Luba -- http://mail.python.org/mailman/listinfo/python-list
Re: Initial pointers for web based LAMP installation
On Sat, Sep 8, 2012 at 4:31 PM, Matteo Grämlin wrote: > Hi all > > This is what I want to do: On a LAMP server, people are able > to request for an instance of a particular LAMP application > by submitting a few options. That involves creating a couple > of directories, getting the code, writing a config file, > setting file permissions and creating a mysql user. > > I can do it except for the interface with a shell script. Because > of the web interface I was told that python is the right language > but I have no knowledge in python. > > Could you pl. give me the initial pointers for this scheme? > I haven't played with this part of that much of Linux yet. you give them group access, certain privileges, and shell access, then they sudo apt-get install, or you, and use it in usr/shr I think, but not from the admin, it becomes sandboxed. -- Best Regards, David Hutto *CEO:* *http://www.hitwebdevelopment.com* -- http://mail.python.org/mailman/listinfo/python-list
Re: Initial pointers for web based LAMP installation
On Sat, Sep 8, 2012 at 7:42 PM, Dwight Hutto wrote: > > > On Sat, Sep 8, 2012 at 4:31 PM, Matteo Grämlin wrote: > >> Hi all >> >> This is what I want to do: On a LAMP server, people are able >> to request for an instance of a particular LAMP application >> by submitting a few options. That involves creating a couple >> of directories, getting the code, writing a config file, >> setting file permissions and creating a mysql user. >> >> I can do it except for the interface with a shell script. Because >> of the web interface I was told that python is the right language >> but I have no knowledge in python. >> >> Could you pl. give me the initial pointers for this scheme? >> > I haven't played with this part of that much of Linux yet. > > you give them group access, certain privileges, and shell access, then > they sudo apt-get install, or yum(correction), etc., and use it in usr/shr > I think, but not from the admin, it becomes sandboxed. > > > -- > Best Regards, > David Hutto > *CEO:* *http://www.hitwebdevelopment.com* > > -- Best Regards, David Hutto *CEO:* *http://www.hitwebdevelopment.com* -- http://mail.python.org/mailman/listinfo/python-list
Re: Initial pointers for web based LAMP installation
On Sat, Sep 8, 2012 at 7:42 PM, Dwight Hutto wrote: > > > On Sat, Sep 8, 2012 at 4:31 PM, Matteo Grämlin wrote: > >> Hi all >> >> This is what I want to do: On a LAMP server, people are able >> to request for an instance of a particular LAMP application >> by submitting a few options. That involves creating a couple >> of directories, getting the code, writing a config file, >> setting file permissions and creating a mysql user. >> >> I can do it except for the interface with a shell script. Because >> of the web interface I was told that python is the right language >> but I have no knowledge in python. >> >> Could you pl. give me the initial pointers for this scheme? >> > I haven't played with this part of that much of Linux yet. > > you give them group access, certain privileges, and shell access, then > they sudo apt-get install, or yum,etc., and use it in usr/shr I think, but > not from the admin, it becomes sandboxed. > > > -- > Best Regards, > David Hutto > *CEO:* *http://www.hitwebdevelopment.com* > > -- Best Regards, David Hutto *CEO:* *http://www.hitwebdevelopment.com* -- http://mail.python.org/mailman/listinfo/python-list
Re: Parsing ISO date/time strings - where did the parser go?
On 2012-09-06, at 2:34 PM, John Nagle wrote: Yes, it should. There's no shortage of implementations. PyPi has four. Each has some defect. PyPi offers: iso8601 0.1.4 Simple module to parse ISO 8601 dates iso8601.py 0.1dev Parse utilities for iso8601 encoding. iso8601plus 0.1.6 Simple module to parse ISO 8601 dates zc.iso8601 0.2.0ISO 8601 utility functions Here are three more on PyPI you can try: iso-8601 0.2.3 Flexible ISO 8601 parser... PySO8601 0.1.7 PySO8601 aims to parse any ISO 8601 date... isodate 0.4.8An ISO 8601 date/time/duration parser and formater All three have been updated this year. -- http://mail.python.org/mailman/listinfo/python-list
Re: how to run python2.6 module with absolute imports stand alone
On 09/08/2012 02:13 AM, Mark Lawrence wrote: On 07/09/2012 23:04, Gelonida N wrote: Hi, many of my modules contain following section at the end def main(): do_something() if __name__ == '__main__': main() This allows me to run some basic example code or some small test in a stand alone mode. My new modules contain following line at the beginning: from __future__ import absolute_import I like this: - It can reduce import name conflicts - and second it allows 'relative' imports like from .othermodule import funcname from ..mod_one_level_higher import fdfsd However If I try to run such a script from the command line it will now complain with ValueError: Attempted relative import in non-package Any tricks to work around this ??? The only idea, that I have is to have a script, that would take my modulename or path name as parameter, and try to import it and then call the main function of the imported module. Not very elegant, but probably functional. Thanks in advance for any other suggestions / ideas. I hope this helps http://stackoverflow.com/questions/3616952/how-to-properly-use-relative-or-absolute-imports-in-python-modules It seems the safest bet seems to be to not use relative imports. What I did in the end however is write a wrapper script, that takes another script as parameter, converts it's path name to a module name, imports it, adapts sys.path and calls main of the imported module. Perhaps a little overkill, but most convenient, as the script can even adapt sys.path prior to importing other files if required. -- http://mail.python.org/mailman/listinfo/python-list
Re: Parsing ISO date/time strings - where did the parser go?
On 9/8/2012 5:20 PM, John Gleeson wrote: > > On 2012-09-06, at 2:34 PM, John Nagle wrote: >> Yes, it should. There's no shortage of implementations. >> PyPi has four. Each has some defect. >> >> PyPi offers: >> >> iso8601 0.1.4 Simple module to parse ISO 8601 dates >> iso8601.py 0.1dev Parse utilities for iso8601 encoding. >> iso8601plus 0.1.6 Simple module to parse ISO 8601 dates >> zc.iso8601 0.2.0 ISO 8601 utility functions > > > Here are three more on PyPI you can try: > > iso-8601 0.2.3 Flexible ISO 8601 parser... > PySO8601 0.1.7 PySO8601 aims to parse any ISO 8601 date... > isodate 0.4.8An ISO 8601 date/time/duration parser and formater > > All three have been updated this year. There's another one inside feedparser, and there used to be one in the xml module. Filed issue 15873: "datetime" cannot parse ISO 8601 dates and times http://bugs.python.org/issue15873 This really should be handled in the standard library, instead of everybody rolling their own, badly. Especially since in Python 3.x, there's finally a useful "tzinfo" subclass for fixed time zone offsets. That provides a way to directly represent ISO 8601 date/time strings with offsets as "time zone aware" date time objects. John Nagle -- http://mail.python.org/mailman/listinfo/python-list
Is there a unique method in python to unique a list?
Is there a unique method in python to unique a list? thanks -- http://mail.python.org/mailman/listinfo/python-list
Re: Is there a unique method in python to unique a list?
If you don't need to retain order you can just use a set, set([1, 1, 2, 3, 4]) = set([1, 2, 3, 4]) But set's don't retain order. On Sunday, September 9, 2012 at 1:43 AM, Token Type wrote: > Is there a unique method in python to unique a list? thanks > -- > http://mail.python.org/mailman/listinfo/python-list > > -- http://mail.python.org/mailman/listinfo/python-list
Re: Is there a unique method in python to unique a list?
On Sun, Sep 9, 2012 at 3:43 PM, Token Type wrote: > Is there a unique method in python to unique a list? thanks I don't believe there's a method for that, but if you don't care about order, try turning your list into a set and then back into a list. ChrisA -- http://mail.python.org/mailman/listinfo/python-list
Re: Is there a unique method in python to unique a list?
Many thanks. I put all the set result into a list first . Then it will work and work without result displayed. >>> import nltk >>> from nltk.corpus import wordnet as wn >>> def average_polysemy(pos): synset_list = list(wn.all_synsets(pos)) sense_number = 0 lemma_list =[] for synset in synset_list: lemma_list.extend(synset.lemma_names) unqiue_lemma_list = list(set(lemma_list)) for lemma in unique_lemma_list: sense_number_new = len(wn.synsets(lemma, pos)) sense_number = sense_number + sense_number_new return sense_number/len(unique_lemma_list) >>> average_polysemy('n') However, if I don't put list(set(lemma_list)) to a variable name, it works much faster. Say: >>> def average_polysemy(pos): synset_list = list(wn.all_synsets(pos)) sense_number = 0 lemma_list = [] for synset in synset_list: lemma_list.extend(synset.lemma_names) for lemma in list(set(lemma_list)): sense_number_new = len(wn.synsets(lemma, pos)) sense_number = sense_number + sense_number_new return sense_number/len(set(lemma_list)) >>> average_polysemy('n') 1 Do you know why there is such a big difference? Does that mean set() will work slower if its value is given to a variable name? On Sun, Sep 9, 2012 at 1:59 PM, Donald Stufft wrote: > seen = set() > uniqued = [] > for x in original: > if not x in seen: > seen.add(x) > uniqued.append(x) > > or > > uniqued = [] > for x in oriignal: > if not x in uniqued: > uniqued.append(x) > > The difference between is option #1 is more efficient speed wise, but uses > more memory (extraneous set hanging around), whereas the second is slower > (``in`` is slower in lists than in sets) but uses less memory. > > On Sunday, September 9, 2012 at 1:56 AM, John H. Li wrote: > > Many thanks. If I want keep the order, how can I deal with it? > or we can list(set([1, 1, 2, 3, 4])) = [1,2,3,4] > > > On Sun, Sep 9, 2012 at 1:47 PM, Donald Stufft wrote: > > If you don't need to retain order you can just use a set, > > set([1, 1, 2, 3, 4]) = set([1, 2, 3, 4]) > > But set's don't retain order. > > On Sunday, September 9, 2012 at 1:43 AM, Token Type wrote: > > Is there a unique method in python to unique a list? thanks > -- > http://mail.python.org/mailman/listinfo/python-list > > > > > -- http://mail.python.org/mailman/listinfo/python-list
Re: Is there a unique method in python to unique a list?
Thanks first, I could understand the second approach easily. The first approach is a bit puzzling. Why are seen=set() and seen.add(x) still necessary there if we can use unique.append(x) alone? Thanks for your enlightenment. On Sun, Sep 9, 2012 at 1:59 PM, Donald Stufft wrote: > seen = set() > uniqued = [] > for x in original: > if not x in seen: > seen.add(x) > uniqued.append(x) > > or > > uniqued = [] > for x in oriignal: > if not x in uniqued: > uniqued.append(x) > > The difference between is option #1 is more efficient speed wise, but uses > more memory (extraneous set hanging around), whereas the second is slower > (``in`` is slower in lists than in sets) but uses less memory. > > On Sunday, September 9, 2012 at 1:56 AM, John H. Li wrote: > > Many thanks. If I want keep the order, how can I deal with it? > or we can list(set([1, 1, 2, 3, 4])) = [1,2,3,4] > > > On Sun, Sep 9, 2012 at 1:47 PM, Donald Stufft wrote: > > If you don't need to retain order you can just use a set, > > set([1, 1, 2, 3, 4]) = set([1, 2, 3, 4]) > > But set's don't retain order. > > On Sunday, September 9, 2012 at 1:43 AM, Token Type wrote: > > Is there a unique method in python to unique a list? thanks > -- > http://mail.python.org/mailman/listinfo/python-list > > > > > -- http://mail.python.org/mailman/listinfo/python-list
Re: Is there a unique method in python to unique a list?
On Sun, Sep 9, 2012 at 4:29 PM, John H. Li wrote: > However, if I don't put list(set(lemma_list)) to a variable name, it works > much faster. Try backdenting that statement. You're currently doing it at every iteration of the loop - that's why it's so much slower. But you'll probably find it better to work with the set directly, instead of uniquifying a list as a separate operation. ChrisA -- http://mail.python.org/mailman/listinfo/python-list
Re: Is there a unique method in python to unique a list?
For a short list the difference is going to be negligible. For a long list the difference is that checking if an item in a list requires iterating over the list internally to find it but checking if an item is inside of a set uses a faster method that doesn't require iterating over the list. This doesn't matter if you have 20 or 30 items, but imagine if instead you have 50 million items. Your going to be iterating over the list a lot and that can introduce significant slow dow. On the other hand using a set is faster in that case, but because you are storing an additional copy of the data you are using more memory to store extra copies of everything. On Sunday, September 9, 2012 at 2:31 AM, John H. Li wrote: > Thanks first, I could understand the second approach easily. The first > approach is a bit puzzling. Why are seen=set() and seen.add(x) still > necessary there if we can use unique.append(x) alone? Thanks for your > enlightenment. > > On Sun, Sep 9, 2012 at 1:59 PM, Donald Stufft (mailto:donald.stu...@gmail.com)> wrote: > > seen = set() > > uniqued = [] > > for x in original: > > if not x in seen: > > seen.add(x) > > uniqued.append(x) > > > > or > > > > uniqued = [] > > for x in oriignal: > > if not x in uniqued: > > uniqued.append(x) > > > > The difference between is option #1 is more efficient speed wise, but uses > > more memory (extraneous set hanging around), whereas the second is slower > > (``in`` is slower in lists than in sets) but uses less memory. > > > > On Sunday, September 9, 2012 at 1:56 AM, John H. Li wrote: > > > > > Many thanks. If I want keep the order, how can I deal with it? > > > or we can list(set([1, 1, 2, 3, 4])) = [1,2,3,4] > > > > > > > > > On Sun, Sep 9, 2012 at 1:47 PM, Donald Stufft > > (mailto:donald.stu...@gmail.com)> wrote: > > > > If you don't need to retain order you can just use a set, > > > > > > > > set([1, 1, 2, 3, 4]) = set([1, 2, 3, 4]) > > > > > > > > But set's don't retain order. > > > > > > > > On Sunday, September 9, 2012 at 1:43 AM, Token Type wrote: > > > > > > > > > Is there a unique method in python to unique a list? thanks > > > > > -- > > > > > http://mail.python.org/mailman/listinfo/python-list > > > > > > > > > > > > > > > > > > > > > > > > > > > > > -- http://mail.python.org/mailman/listinfo/python-list
Re: Is there a unique method in python to unique a list?
> Try backdenting that statement. You're currently doing it at every > > iteration of the loop - that's why it's so much slower. Thanks. I works now. >>> def average_polysemy(pos): synset_list = list(wn.all_synsets(pos)) sense_number = 0 lemma_list = [] for synset in synset_list: lemma_list.extend(synset.lemma_names) for lemma in list(set(lemma_list)): sense_number_new = len(wn.synsets(lemma, pos)) sense_number = sense_number + sense_number_new return sense_number/len(set(lemma_list)) >>> average_polysemy('n') 1 > But you'll probably find it better to work with the set directly, > > instead of uniquifying a list as a separate operation. Yes, the following second methods still runs faster if I don't give a separate variable name to list(set(lemma_list)). Why will this happen? >>> def average_polysemy(pos): synset_list = list(wn.all_synsets(pos)) sense_number = 0 lemma_list = [] for synset in synset_list: lemma_list.extend(synset.lemma_names) for lemma in list(set(lemma_list)): sense_number_new = len(wn.synsets(lemma, pos)) sense_number = sense_number + sense_number_new return sense_number/len(set(lemma_list)) >>> average_polysemy('n') 1 -- http://mail.python.org/mailman/listinfo/python-list