Re: [Tutor] advice on idiom replacing if test requested
Danny Yoo said unto the world upon 2005-12-11 22:13: > > On Sun, 11 Dec 2005, Brian van den Broek wrote: > > >>I have a case like this toy code: >> >>import random >>list1 = [1,2,3] >>list2 = ['a', 'b', 'c'] >>item = random.choice(list1 +list2) >>if item in list1: >> others = list2 >>else: >> others = list1 > > > Hi Brian, > > This code works, and as long as you give it a good function name, I think > it's fine the way it is. > > If we're concerned with efficiency, Hi Danny, thanks for the reply. My concern wasn't efficiency, but screen space. I'm refactoring some code and had a method that was too long. I was trying to fix it while avoiding real work :-) > we might want to change the > random.choice() call to a random.randrange(), to avoid building the > concatenation of list1 and list2. This looks like: > > > def sampleFromTwoLists(list1, list2): > """Given two lists, returns a random element out of one of the lists > as well as the other list.""" > index = random.randrange(len(list1) + len(list2)) > if index < len(list1): > return list1[index], list2 > else: > return list2[index - len(list1)], list1 > > > Just out of curiosity, are you planning to do some kind of stratified > sampling with this? Thanks for the example. Nothing so interesting as that, I'm afraid :-) I'm just goofing around with a text-based game, so efficiency isn't an issue. The two list represent teams of characters and the point of the code is to select a random character and oppose them to the other team. >>Another way occurred to me, but I wonder if I'm being too cute: >> >>item = random.choice(list1 +list2) >>others = [list1, list2][item in list1] > > > Too cute. *grin* Although it's concise, I'm having a hard time reading > it. Thanks for the feedback. I still don't trust my intuitions on issues like this. Best to all, Brian vdB ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] advice on idiom replacing if test requested
On Sun, 11 Dec 2005, Brian van den Broek wrote: > I have a case like this toy code: > > import random > list1 = [1,2,3] > list2 = ['a', 'b', 'c'] > item = random.choice(list1 +list2) > if item in list1: > others = list2 > else: > others = list1 Hi Brian, This code works, and as long as you give it a good function name, I think it's fine the way it is. If we're concerned with efficiency, we might want to change the random.choice() call to a random.randrange(), to avoid building the concatenation of list1 and list2. This looks like: def sampleFromTwoLists(list1, list2): """Given two lists, returns a random element out of one of the lists as well as the other list.""" index = random.randrange(len(list1) + len(list2)) if index < len(list1): return list1[index], list2 else: return list2[index - len(list1)], list1 Just out of curiosity, are you planning to do some kind of stratified sampling with this? > Another way occurred to me, but I wonder if I'm being too cute: > > item = random.choice(list1 +list2) > others = [list1, list2][item in list1] Too cute. *grin* Although it's concise, I'm having a hard time reading it. Talk to you later! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] advice on idiom replacing if test requested
At 04:15 PM 12/11/2005, Brian van den Broek wrote: >Hi all, > >I have a case like this toy code: > >import random >list1 = [1,2,3] >list2 = ['a', 'b', 'c'] >item = random.choice(list1 +list2) >if item in list1: > others = list2 >else: > others = list1 > > >Another way occurred to me, but I wonder if I'm being too cute: > >item = random.choice(list1 +list2) >others = [list1, list2][item in list1] > >I believe we can rely on True and False being 1 and 0 until Python >3.0. But, even assuming that's right, I wonder if it is obscure to others. It is not obscure to me. I do tings like that all the time. But I think your algorithm is unnecessarily complex and costly. Consider import random list1 = [1,2,3] list2 = ['a', 'b', 'c'] len1 = len(list1) len2 = len(list2) item = random.randint(1, len1 + len2) if item <= len1: others = list2 else: others = list1 But then we also could: import random ... same as above lists = [list1, list2] others = lists[random.randint(1, len1 + len2) <= len1] ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] advice on idiom replacing if test requested
Hi all, I have a case like this toy code: import random list1 = [1,2,3] list2 = ['a', 'b', 'c'] item = random.choice(list1 +list2) if item in list1: others = list2 else: others = list1 Another way occurred to me, but I wonder if I'm being too cute: item = random.choice(list1 +list2) others = [list1, list2][item in list1] I believe we can rely on True and False being 1 and 0 until Python 3.0. But, even assuming that's right, I wonder if it is obscure to others. Thanks and best, Brian vdB ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] information needed to make a connection between computers
[Taking catalog-sig and python-list out of CC.] John, please don't crosspost. catalog-sig in particular is off-topic of your question. When we crosspost, we add noise to those lists and frustrate members of the community. It's generally a bad thing to do. See: http://catb.org/~esr/faqs/smart-questions.html and: http://www.gweep.ca/~edmonds/usenet/ml-etiquette.html for more details about this. Please make sure your replies are only going to a single mailing list unless you really have overriding reasons for crossposting. In fact, you've been called on this behavior back in August: http://mail.python.org/pipermail/catalog-sig/2005-August/692.html Ian Bicking there was fairly civil, but you need to pick up on the clue: he didn't give you much help besides saying, in effect: you're posting on the wrong mailing list, and he and others on catalog-sig won't even bother responding to you if you ignore his request for topicality. It look like you didn't really hear what he said, so let me say it explicitely: crossposting is considered inconsiderate behavior. If you continue to do so, people will respond in kind by ignoring your questions, and that will be bad. So avoid getting people annoyed: don't crosspost. Thanks. Anyway, to your question. > I don't know whether or not this is the same for Python, but could > someone please tell me what information of the computer you want to > connect with the you actually need for a connection? Computers on the internet all have an IP address in the form of dotted numbers. For example, 82.94.237.218 is an example of an IP address. Many computers on the Internet can register to get a nice, mnemonic name, like: python.org > In other words (or plain english), what information do I need to get a > connection with another computer (IP address, name, IP name)? Also, > could you tell me how to find it on a computer? Either IP address or name should be sufficient. For example, here's a little snippet of code that shows how we might contact the web server on Python.org. (Note that in real life, we'd probably use the 'urllib' library instead.): ## >>> import socket >>> s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) >>> s.connect(("python.org", 80)) >>> s.send("GET /\n") 6 >>> s.recv(20) 'http://www.amk.ca/python/howto/sockets You may also want to look at high-level modules like Twisted, which provide some extra support for network programming: http://twistedmatrix.com/projects/twisted http://twistedmatrix.com/projects/core/documentation/howto/index.html If you are looking for more introductory information on network programming, please feel free to ask, and I'm sure on of us can find something useful for you. Good luck. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] information needed to make a connection between computers
Hello again! I'm still working on that instant messenger (for science fair), and I have been reading about networking in some Java tutorials. In one part of it, it said to have a connection with another computer, you need to know the IP name of the computer you want to connect with. I don't know whether or not this is the same for Python, but could someone please tell me what information of the computer you want to connect with the you actually need for a connection? In other words (or plain english), what information do I need to get a connection with another computer (IP address, name, IP name)? Also, could you tell me how to find it on a computer? Since I'll be testing the instant messenger on the three computers in my house, I just need to know how to find the information on the computer I'd currently be on (not from any remote location). Thanks! :) -John Yahoo! Shopping Find Great Deals on Holiday Gifts at Yahoo! Shopping ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] bnf
> ::= is bnf notation for "is defined as" > > please spend that extra minute googling before > you bother all the nice people on this list. To be fair, unless you knew that it was BNF I doubt you'd find much by Googling. I tried several variations of '::=' etc and Google came back empty. Once you know than you are kooking at BNF then Googling for BNF broings back loads of stuff but its not obvious if you don't already know... Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor