Re: [Tutor] Option parser tools
just FYI, A simple, not versatile way, kargs = dict([i.split('=', 1) for i in sys.argv[1:]]) Changsheng Jiang On Thu, Jul 23, 2009 at 14:11, Todd Matsumoto wrote: > Hello Tutors, > > I'm building a script that now needs a command line interface. > > I'm aware of optparse, but I'm wondering if there are any other tools that > may be better for building the interface. > > Cheers, > > T > -- > GRATIS für alle GMX-Mitglieder: Die maxdome Movie-FLAT! > Jetzt freischalten unter http://portal.gmx.net/de/go/maxdome01 > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] File I/O help
On Wed, Jul 22, 2009 at 10:48:49PM -0500, Chris Castillo wrote: > *I'm supposed to be reading in names a grades from a text file like so: > * > Chris > 100 > 89 > 76 > 0 My Spidey Senses are picking up "homework assignment", so I'll try to nudge you in a general direction without doing your work for you. > from myFunctions import * I'm curious about this... is this a way for you to organize your code into multiple modules? I'd suggest looking at dividing things up into classes or modules by topic, not just a "dumping ground for a bunch of functions I use often" (which is what I'm guessing here). > inputFile = open("input.txt", "r" )# Read in a file with student names a > grades > > for line in open("input.txt", "r"):# iterate through txt file with names > and grades You're opening the file twice, which probably isn't what you wanted. > if line.strip().isdigit(): > grade = float(line)# convert score into float type > gradeTotal += grade# adds score to running total > if grade != 0: > grade = grades.append(grade) > else: > name = line.strip() > name = names.append(name)# Append name to names list > > studentTotal = str(len(names))# Get student total Why do you want to turn the number of students into a string? While you're at it, look at the return value from the append method for lists. What are you accomplishing by assigning that back onto the variables grade and name above? > grades.sort() # Sort the grades in ascending order All the grades? Seems like you need a way to keep track of the grades for each individual student? Maybe some kind of collection object which can store things by name? -- Steve Willoughby| Using billion-dollar satellites st...@alchemy.com | to hunt for Tupperware. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] xlwt & xlrd: can I refactor this code better?
> Btw, I see that you're the author of a Python book. I am using Python for my > work as a researcher. Should, in your opinion, the learning strategy for > somebody like me vis-a-vis somebody who is becoming a professional programmer > be > very much different? Not in the early days. The art of programming is basically the same and a good program is a good program. However a professional should study a lot more supplementary material, such as the math theory behind programming (predicate and lambda calculii for example, set theory, graph theory, statistics and geometry are all fundamental programming foundations). Also because professionals typically work on much bigger projects (eg the smallest professional project I've ever done had 6 people producing 50,000 lines of code in over 70 files, the largest was 450 people, 3.5million lines in 10,000 files) they need to better understand the use and value of automated build, test and configuration tools. Finally professionals need to understand how to design programs and systems, they need to learn the indistry standard notations such as UML, ERDs and DFDs etc. They need to know about coupling and cohesion (not just the concepts but how to measure them empirically) as well as things like deadlock, race conditions etc... They need a much deeper understanding of the OS and computer hardware, of networks and comms, and so on. In essence they need to really understand what they are doing rather than just finding something that works - or seems to... This is why professional software engineers have degrees in the subject! So in summary a casual programmer who just needs to build something for their own use (or maybe a few colleagues) doesn't need the breadth or depth of a professional who will be building mission critical (even safety critical - think Space Shuttle!) systems, often used by thousands or even millions of (often untrained) users. Regards, Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Mapping of co-ordinates... pygame
Hi, I have some x and y co-ordinates ranging from (-100, 100) for each x and y. Something like this: 100 | | | | -100--100 | | | | -100 I would like to plot them on a pygame surface. I have found that pygame surface is (0, 0) at top right to (width, height) at the bottom right corner, like this: (0,0) (width, 0) | | | | | | | | | | | | | | (0,height)(width, height) I need a way to map any value from my range to this surface so I could do stuff like this: pygame.draw.circle(screen, color, (x, y), size Also my range could vary from -5, 5 or -500 to 500 so I need something generic... It looks like a simple problem but I can't seem to be able to figure it out. Thanks, Ali ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Mapping of co-ordinates... pygame
On Thu, Jul 23, 2009 at 7:09 AM, Muhammad Ali wrote: > Hi, > > I have some x and y co-ordinates ranging from (-100, 100) for each x and y. > Something like this: > >100 > | > | > | > | > -100--100 > | > | > | > | > -100 > > I would like to plot them on a pygame surface. > I have found that pygame surface is (0, 0) at top right to (width, height) > at the bottom right corner, like this: > > (0,0) (width, 0) > > | | > | | > | | > | | > | | > | | > | | > > (0,height)(width, height) > > > I need a way to map any value from my range to this surface so I could do > stuff like this: > pygame.draw.circle(screen, color, (x, y), size > > Also my range could vary from -5, 5 or -500 to 500 so I need something > generic... > > It looks like a simple problem but I can't seem to be able to figure it > out. Well, first off you'll need to know what your window size is. I presume you're defining the size and you're defining a 1:1 ratio (400x400, or 600x600 or some such)? If so, then all you need to do is scale your x, y down and then offset your imaginary grid to the middle. I don't know any of the pygame methods, but here's what it might look like: def draw_pixel(x, y): w = screen.get_width() h = screen.get_height() screen.draw_pixel(w/2+x, h/2+y) That should give you what you need. It starts right in the middle of the screen (w/2, h/2) then will add your values of x and y. If you have a screen of 400x400, and x, y of -20, 20 you'll have 200 + (-20) = 180 and 200 + 20 = 220. HTH, Wayne ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Question from a newbie (thank you)
WoW! You all have given me lots to look at, think about, and play with. Thank you one and all for your answers! <><><>___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Pygame
Hi, there also is: "Invent Your Own Computer Games with Python", which apparently does not use pygame (like Dawson). http://pythonbook.coffeeghost.net/ I can't comment on the quality. David Mazhar Hussain wrote: > Hello All! My name is Mazhar Hussain, and I am new to python, in > fact, I am new to programming as a whole. I dont know if its the right > list to talk about this. > I am really interested in 2d games, especially 2d games. I am also > interested in making games. I searched for a lot of programming > languages but I didnt like anyone of them, either they were too hard > to learn or not suitable to make games. But then I found Python. I had > heard that it was very easy to learn and great for making games, it > also had a binding for SDL called pygame. But the main games I want to > create are: a pokemon clone, a megaman battle network clone and a > world of goo like game. I just want to know if I can make these type > of games with pygame(before learning python). If it can then I'll > start learning python right away but if it cant then I think I may > better find another language. > > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] dictionaries help
hello, please excuse me, but i would like to understand how dictionaris work. for example: >>> my_lst = [{'code': 'aaa', 'name': 'a name'}, {'code': 'bbb', 'name': 'b >>> name'}] >>> my_code = 'aaa' from the above i would like to compare my_code and return the dictionary which has code == my_code dave ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] dictionaries help
this should work def find_value(value, lst): for obj in lst: if value in obj.values(): return obj >> find_value("aaa", my_lst) Vince On Thu, Jul 23, 2009 at 9:55 AM, wrote: > hello, > please excuse me, but i would like to understand how dictionaris work. > > for example: > >>> my_lst = [{'code': 'aaa', 'name': 'a name'}, {'code': 'bbb', 'name': 'b > name'}] > >>> my_code = 'aaa' > > > from the above i would like to compare my_code and return the dictionary > which has code == my_code > > dave > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Mapping of co-ordinates... pygame
"Wayne" wrote def draw_pixel(x, y): w = screen.get_width() h = screen.get_height() screen.draw_pixel(w/2+x, h/2+y) I think that should be h/2-y for the coordinates given? Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] dictionaries help
wrote i would like to understand how dictionaries work. They work like a lookup table. You provide a key and lookup the corresponding value in the dictionary. So to implement a traditional language dictionary the keys would be words and the values would be a list of definitions. You read them by placing the key in square brackets, or by using the get() method: for example: my_lst = [{'code': 'aaa', 'name': 'a name'}, {'code': 'bbb', 'name': 'b name'}] my_lst[0]['code'] -> 'aaa' my_lst[0].get('code', 0)-> 'aaa' or 0 if 'aaa' does not exist as a key You can write to them using the [] notation too: my_lst[0]['name'] = 'A new name' changes the value for name in the first dictionary. my_code = 'aaa' from the above i would like to compare my_code and return the dictionary which has code == my_code Vince has shown you one way, this is another way: def findDict(value, dictList): for dct in dictList: if dct.get('code', '') == my_code return dct HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] dictionaries help
"Alan Gauld" wrote Oops! Should be: def findDict(value, dictList): for dct in dictList: if dct.get('code', '') == my_code if dct.get('code', '') == value return dct HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] mnemonics to better learn Python
I approach these things more along the lines of objects rather than looking at the raw source code to remember what I'm doing.For example, I see (x, y, z) and I think "ah, it's a little pre-packaged goodie of delicious variables" and when I see [x, y, z] I think "ah, it's a box with some candy at the bottom, but there's plenty of room for me to put more. I'm guessing all the candy is interrelated somehow because people usually keep things that are similar in the same container" and for dictionaries I think "hey, it's just a list that's indexed with anything you want, instead of only integers." I wouldn't call it "rote learning" because that would be more along the lines of x = {"hello": 1} # dictionary with a string as key and an integer as valuex = {1: "hello"} # dictionary with an integer as key and a string as value x = {1: 1} # dictionary with an integer as key and value x = {"hello": "hello"} # dictionary with a string as key and value etc. I.E. I'm not forcing myself to learn every possibility. I learn what "mutability" is and then I learn what dictionaries are, and then I understand how to use them. I don't need mnemonics to remember them. I guess my point is that I see syntax more as a means for defining a specific behavior I want, rather than as an actual representation of the behavior. Like, I think "I need a dictionary here. Oh, that's the {} thing, right?" rather than "Oh, there needs to be {} here, I think. Oh yeah, that's called a dictionary" so it doesn't really help to relate syntax to actual behavior. I think of things on the conceptual level and translate down to Python when I need to tell the computer how to do it. And when I read Python code, I translate it up to conceptual level as well. I don't see "series = form.getvalue('series').strip()" and think "okay, there's an object called "form". it has a method called "getvalue" and it's getting passed "series". Then the return value is being stripped of whitespace on both ends." I see it and I think "okay, 'series' was passed to the program in a user-submitted form and now it's stored in a local variable so I can manipulate it." On Thu, Jul 23, 2009 at 1:05 AM, David wrote: > Dear List, > > in order to memorize which Python sequences are mutable or immutable, I > focused on the SHAPE of the brackets that are associated with each type > of sequence. > > For instance, a *list* is characterised by square brackets, []. > My mnemonic device to memorize that lists are mutable is this: "the > brackets have sharp edges, they could be trimmed, taking their edges off". > > The same thing happens with *dictionaries* (which, okay, are not > sequences). Anyway, their brackets, {}, have sharp edges, hence they are > mutable. > > *Tuples*, in turn, have perfectly 'round' brackets, (), and these > brackets obviously can't be improved upon by taking anything off them. > Hence: tuples are immutable. > > That leaves us with *strings*, which are also not mutable. Here we have > no brackets, and this particular mnemonic device breaks down. > > What I am interested in is finding out whether you use similar > techniques, and if so, which ones? How, for examples, do you make sense > of all those special characters that make regular expressions powerful? > Do you rely on rote learning, or do you employ some other technique? > > I reckon that if we could come up with some tips and techniques as to > how to uncloud the thick information fog that any beginning programmer > has to wade through, the very first steps in learning Python could be > made more easy. > > What insights can you share? > > Curious, > > David > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] reading data
I have a data file in which the first line is made up of words. Here is the original data file: #Column density-scaled with production rate 3.16227766016838e+25 -10. 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 -9.9000 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 I'd like to my program to skip any lines that begin with words and move on to lines that contain numbers. Right now I just cheat and delete the first line and resave the data file, but I was hoping that I could avoid these types of things in the future. Here is my code for reading the file and figuring out how many rows and columns to dimensionalize my array, then I read the data into the array: from numpy import * row=0 columnindex=0 x=open('halfmethanol.col','rt') for line in x.readlines(): data=line.split() columnindex=len(data) row=row+1 temp=ones((row,columnindex)) row=0 x=open('halfmethanol.col','rt') for line in x.readlines(): data=line.split() for column in range(columnindex): temp[row,column]=data[column] row=row+1 _ Windows Live™ SkyDrive™: Store, access, and share your photos. See how. http://windowslive.com/Online/SkyDrive?ocid=TXT_TAGLM_WL_CS_SD_photos_072009___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] mnemonics to better learn Python
> Date: Thu, 23 Jul 2009 14:05:36 +0800 > From: ld...@gmx.net > To: Tutor@python.org > Subject: [Tutor] mnemonics to better learn Python > > Dear List, > > in order to memorize which Python sequences are mutable or immutable, I > focused on the SHAPE of the brackets that are associated with each type > of sequence. > > For instance, a *list* is characterised by square brackets, []. > My mnemonic device to memorize that lists are mutable is this: "the > brackets have sharp edges, they could be trimmed, taking their edges off". > > The same thing happens with *dictionaries* (which, okay, are not > sequences). Anyway, their brackets, {}, have sharp edges, hence they are > mutable. > > *Tuples*, in turn, have perfectly 'round' brackets, (), and these > brackets obviously can't be improved upon by taking anything off them. > Hence: tuples are immutable. > > That leaves us with *strings*, which are also not mutable. Here we have > no brackets, and this particular mnemonic device breaks down. > > What I am interested in is finding out whether you use similar > techniques, and if so, which ones? How, for examples, do you make sense > of all those special characters that make regular expressions powerful? > Do you rely on rote learning, or do you employ some other technique? > > I reckon that if we could come up with some tips and techniques as to > how to uncloud the thick information fog that any beginning programmer > has to wade through, the very first steps in learning Python could be > made more easy. > > What insights can you share? I am all for mnemonics and have used them to good effect in learning and teaching; students' abilities to memorize large lists of biological terms and such are greatly increased this way. But so far it hadn't occurred to me to apply mnemonics to learning Python. This is because I have seen learning Python as less about memorizing content and more about understanding concepts. Others may see it differently, but that is how it has seemed to me. Compared to biology, where words are based on Greek or Latin (e.g. "postzygopophysis"), and there may be many such odd terms per topic, Python is mostly based in simple English words like random, string, open, etc. What was harder, coming from a cold start, was how to use the language, "grokking" things like self, list comprehensions, namespaces, = vs ==, Boolean stuff, exceptions, lambdas, decorators, etc. Also, importantly, I tend to have ready access to the web to look things up if I forget them, like whether it is .split() or .join() that I want to use. But your idea to mnemonicize some of your learning could no doubt be helpful to speed you along, since looking things up does slow one down (and can derail one's thought stream as one is trying to figure out why the program is not working as expected). _ Windows Live™ SkyDrive™: Store, access, and share your photos. See how. http://windowslive.com/Online/SkyDrive?ocid=TXT_TAGLM_WL_CS_SD_photos_072009___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] dictionaries help
Also you can use list comprehension In [1]: my_lst = [{'code': 'aaa', 'name': 'a name'}, {'code': 'bbb', 'name': 'b name'}] In [2]: my_code = 'aaa' In [3]: print [d for d in my_lst if d['code'] == my_code] --> print([d for d in my_lst if d['code'] == my_code]) [{'code': 'aaa', 'name': 'a name'}] In [4]: On Thu, Jul 23, 2009 at 6:04 PM, Alan Gauld wrote: > > "Alan Gauld" wrote > Oops! > Should be: > >> def findDict(value, dictList): >> for dct in dictList: >> if dct.get('code', '') == my_code > > if dct.get('code', '') == value > >> return dct > > >> HTH, >> >> >> -- >> Alan Gauld >> Author of the Learn to Program web site >> http://www.alan-g.me.uk/ > > ___ > Tutor maillist - tu...@python.org > http://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] dictionaries help
thank you for all your answers Original Message From: Norman Khine Apparently from: tutor-bounces+davidwilson=safe-mail@python.org To: tutor@python.org Subject: Re: [Tutor] dictionaries help Date: Thu, 23 Jul 2009 21:59:01 +0100 > Also you can use list comprehension > > In [1]: my_lst = [{'code': 'aaa', 'name': 'a name'}, {'code': 'bbb', > 'name': 'b name'}] > > In [2]: my_code = 'aaa' > > In [3]: print [d for d in my_lst if d['code'] == my_code] > --> print([d for d in my_lst if d['code'] == my_code]) > [{'code': 'aaa', 'name': 'a name'}] > > In [4]: > > > On Thu, Jul 23, 2009 at 6:04 PM, Alan Gauld wrote: > > > > "Alan Gauld" wrote > > Oops! > > Should be: > > > >> def findDict(value, dictList): > >> for dct in dictList: > >> if dct.get('code', '') == my_code > > > > if dct.get('code', '') == value > > > >> return dct > > > > > >> HTH, > >> > >> > >> -- > >> Alan Gauld > >> Author of the Learn to Program web site > >> http://www.alan-g.me.uk/ > > > > ___ > > Tutor maillist - tu...@python.org > > http://mail.python.org/mailman/listinfo/tutor > > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] reading data
"chris Hynes" wrote Its not a good idea to start a new thread by replying to an existing one. Threaded newsreaders or mailing tools will still see it as part of the old thread and bored readers may not notice the new subject... However have a data file in which the first line is made up of words. Here is the original data file: #Column density-scaled with production rate 3.16227766016838e+25 -10. 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 ... I'd like to my program to skip any lines that begin with words and move on to lines that contain numbers. We've had a few requests very similar to this over the last couple of weeks. the isdigit() method of strings may well help here. -- from numpy import * row=0 columnindex=0 x=open('halfmethanol.col','rt') for line in x.readlines(): data=line.split() columnindex=len(data) row=row+1 The row line does nothing and can be deleted. But it looks like a hard wayto find the length of the last line. Is that really what you want? If so you could gather the data wjile you are at it: data = [line.split() for line in x.readlines()] columnindex = len(x[-1]) Then the next loop can be over x rather than rereading the file and splitting the lines again. And if you use enumerate you can miss out the incrementing here too: temp=ones((row,columnindex)) for row, line in enumerate(x): for column in range(columnindex): temp[row,column]=data[column] However I'm still not totally clear what you are achieving with this? -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Mapping of co-ordinates... pygame
Think "linear relation". For each of your new "horizontal" coordinates, there is a linear relation changing the "old" coordinate into the pixel coordinate of the pygame map. You have to find the intercept and the slope of that linear relation. There will be a second, unrelated "linear transformation" for the new vertical coordinate as a function of the original vertical coordinate. So you need to figure out slope and intercept for two functions def new_horizontal(oldx, hslope, hintercept): new=hintercept+hslope*oldx return new (There are better ways to write the code.) ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Pygame
Hi, I'm Al, the author of Invent Your Own Computer Games with Python. The book does indeed cover Pygame. The last four chapters covers the Pygame library, and the final one has the source code for a complete game. I still recommend learning Python first though, it will make it much easier to understand the code. The book can do this for you and is freely available at http://pythonbook.coffeeghost.net -Al ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] dictionaries help
Hello again, Here is my full attempt, can you please tell me if it is OK and if there should be any improvements >>> ws_industry = ('it', 'science') >>> code = ws_industry[0] >>> active = [] >>> industries = [{'code': 'it', 'name': 'Information Technology'}, {'code': >>> 'science', 'name': 'Science'}, {'code': 'biology', 'name': 'Biology'}] >>> for industry in industries: if industry['code'] in ws_industry: active.append({ 'code': industry['code'], 'name': industry['name'], 'isdefault': code == industry['code']}) >>># Not active industry >>> not_active = [x for x in industries if x['code'] not in ws_industry ] >>>not_active.sort(lambda x, y: cmp(x['name'], y['name'])) >>> return [active, non-active] Line [6] i was not too sure if it could be written better or more efficient. Basically I have a big list of industries and I want to verify for each member whether they are active or not active. The industry list of dictionaries will stay fixed at about 25 items, whereas the ws_industry tuple will change depending. I have to go through about 20,000 items. Original Message From: davidwil...@safe-mail.net Apparently from: tutor-bounces+davidwilson=safe-mail@python.org To: tutor@python.org Subject: Re: [Tutor] dictionaries help Date: Thu, 23 Jul 2009 18:14:01 -0400 > thank you for all your answers > > > Original Message > From: Norman Khine > Apparently from: tutor-bounces+davidwilson=safe-mail@python.org > To: tutor@python.org > Subject: Re: [Tutor] dictionaries help > Date: Thu, 23 Jul 2009 21:59:01 +0100 > > > Also you can use list comprehension > > > > In [1]: my_lst = [{'code': 'aaa', 'name': 'a name'}, {'code': 'bbb', > > 'name': 'b name'}] > > > > In [2]: my_code = 'aaa' > > > > In [3]: print [d for d in my_lst if d['code'] == my_code] > > --> print([d for d in my_lst if d['code'] == my_code]) > > [{'code': 'aaa', 'name': 'a name'}] > > > > In [4]: > > > > > > On Thu, Jul 23, 2009 at 6:04 PM, Alan Gauld > > wrote: > > > > > > "Alan Gauld" wrote > > > Oops! > > > Should be: > > > > > >> def findDict(value, dictList): > > >> for dct in dictList: > > >> if dct.get('code', '') == my_code > > > > > > if dct.get('code', '') == value > > > > > >> return dct > > > > > > > > >> HTH, > > >> > > >> > > >> -- > > >> Alan Gauld > > >> Author of the Learn to Program web site > > >> http://www.alan-g.me.uk/ > > > > > > ___ > > > Tutor maillist - tu...@python.org > > > http://mail.python.org/mailman/listinfo/tutor > > > > > ___ > > Tutor maillist - Tutor@python.org > > http://mail.python.org/mailman/listinfo/tutor > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] dictionaries help
On Thu, Jul 23, 2009 at 7:09 PM, wrote: > Hello again, > Here is my full attempt, can you please tell me if it is OK and if there > should be any improvements > ws_industry = ('it', 'science') code = ws_industry[0] active = [] industries = [{'code': 'it', 'name': 'Information Technology'}, {'code': 'science', 'name': 'Science'}, {'code': 'biology', 'name': 'Biology'}] for industry in industries: > if industry['code'] in ws_industry: > active.append({ > 'code': industry['code'], > 'name': industry['name'], > 'isdefault': code == industry['code']}) > # Not active industry not_active = [x for x in industries if x['code'] not in ws_industry ] not_active.sort(lambda x, y: cmp(x['name'], y['name'])) return [active, non-active] > > Line [6] i was not too sure if it could be written better or more efficient. > > Basically I have a big list of industries and I want to verify for each > member whether they are active or not active. > > The industry list of dictionaries will stay fixed at about 25 items, whereas > the ws_industry tuple will change depending. I have to go through about > 20,000 items. If ws_industry contains 25 items, it will probably be faster to search it if it is a set rather than a list. Just use ws_industry = set(('it', 'science')) Which part of the problem has 20,000 items? Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Mapping of co-ordinates... pygame
Thanks Wayne and Alan, Here is what I finally ended up with: def newCoord(x, y, oldWidth, oldHeight, newWidth, newHeight): return( newWidth/2 + newWidth/oldWidth * x), (newHeight/2 - newHeight/oldHeight * y) for my earlier example, the old width and height would be 200, 200 respectively. Thanks! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] how to get blank value
Hi, I have a file having lines:- 48 ALA H = 8.33 N = 120.77 CA = 55.18 HA = 4.12 C = 181.50 104 ALA H = 7.70 N = CA = HA = 4.21 C = 85 ALA H = 8.60 N = CA = HA = 4.65 C = Now i want to make two another file in which i want to put those lines for which C is missing and another one for which N,CA and C all are missing, I tried in this way: import re expr = re.compile("C = None") f = open("helix.dat") for line in f: if expr.search(line): print line but i am not getting the desired output. Amrita Kumari Research Fellow IISER Mohali Chandigarh INDIA ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor