assignment with [:]

2008-12-27 Thread Ben Bush
Hi,

I saw this line of code on a recent post:

a1[:] = [x*3 for x in a1]

Could somebody tells me what the [:] means? I can't find it anywhere.
See context below if needed:

On Dec 26, 4:46 pm, Tim Chase  wrote:
> > What does *not* work is
> > 3 * [0,1,2]
> > As you know, this gives
> > [0,1,2,0,1,2,0,1,2]
> > What I am hoping for is
> > [0,3,6]
> > I see that I can use
> > numpy.multiply(3,range(3))
> > but this seems overkill to me.  Can you tell I am coming to Python from
> > Matlab?
>
> The common way to do this is just
>
>   a1 = [0,1,2]
>   a2 = [x * 3 for x in a1]
>
> or, if you need a1 to be done in place:
>
>   a1[:] = [x*3 for x in a1]
>
> -tkc


--
http://mail.python.org/mailman/listinfo/python-list


change color

2005-12-04 Thread Ben Bush
I tested the following code and wanted to make oval 2 become red after
I hit the enter key but though the code did not report error, it did
not change.
from Tkinter import *
root=Tk()
canvas=Canvas(root,width=100,height=100)
canvas.pack()
canvas.create_oval(10,10,20,20,tags='oval1',fill='blue')
canvas.create_oval(50,50,80,80,tags='oval2',fill='yellow')
def turnRed(self, event):
event.widget["activeforeground"] = "red"
self.button.bind("", self.turnRed)
canvas.tag_bind('oval2',func=turnRed)
root.mainloop()
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: oval

2005-12-04 Thread Ben Bush
On 12/4/05, Peter Otten <[EMAIL PROTECTED]> wrote:
> Ben Bush wrote:
>
> > On 12/4/05, Diez B. Roggisch <[EMAIL PROTECTED]> wrote:
> >> Ben Bush wrote:
> >> > I tested the following code and wanted to get the message of "oval2
> >> > got hit" if I click the red one. But I always got "oval1 got hit".
> >> > from Tkinter import *
> >> > root=Tk()
> >> > canvas=Canvas(root,width=100,height=100)
> >> > canvas.pack()
> >> > a=canvas.create_oval(10,10,20,20,tags='oval1',fill='blue')
> >> > b=canvas.create_oval(50,50,80,80,tags='oval2',fill='red')
> >> > def myEvent(event):
> >> > if a:
> >>
> >> Here is your problem. a is a name, bound to some value. So - it is true,
> >> as python semantics are that way. It would not be true if it was e.g.
> >>
> >> False, [], {}, None, ""
> >>
> >>
> >> What you want instead is something like
> >>
> >> if event.source == a:
> >> ...
> >>
> >> Please note that I don't know what event actually looks like in Tkinter,
> >> so check the docs what actually gets passed to you.
> >
> > got AttributeError: Event instance has no attribute 'source'
>
> Note that Diez said /something/ /like/ /event.source/. The source is
> actually called widget -- but that doesn't help as it denotes the canvas as
> a whole, not the individual shape.
>
> The following should work if you want one handler for all shapes:
>
>def handler(event):
>print event.widget.gettags("current")[0], "got hit"
>canvas.tag_bind('oval1', '', handler)
>canvas.tag_bind('oval2', '', handler)
>
> I prefer one handler per shape:
>
>    def make_handler(message):
>def handler(event):
>print message
>return handler
>
>canvas.tag_bind('oval1', '', make_handler("oval 1 got hit"))
>canvas.tag_bind('oval2', '', make_handler("oval 2 got hit"))
>
> Peter
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
Peter,
It works. Thanks!
is there any good material to read if I want to improve my
understanding of working on interactive ways of dealing with shapes
on the TKinter?
--
Ben Bush
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: oval

2005-12-04 Thread Ben Bush
On 12/4/05, Diez B. Roggisch <[EMAIL PROTECTED]> wrote:
> >>
> >>What you want instead is something like
> >>
> >>if event.source == a:
> >>...
> >>
> >>Please note that I don't know what event actually looks like in Tkinter,
> >>so check the docs what actually gets passed to you.
> >
> >
> > got AttributeError: Event instance has no attribute 'source'
>
> As I said: I don'k _know_ how event actually looks like,a s I'm not a
> Tkinter-expert, but I've had plenty of projects involving other
> GUI-Toolkits, which work all the same in this manner. After all the
> whole purposs of the event parameter is to inform you about the cause
> for that event - in this case the pressing of a MB on a specific canvas
> element.
>
> But if you'd take it on you to consult the documentation as I asked you
> to do, I'm pretty sure you find the proper attribute.
>
>
> Regards,
>
> Diez
> --
> http://mail.python.org/mailman/listinfo/python-list
>
thanks anyway. The python manual for event is very brief and it is
hard for me to understand.


--
Ben Bush
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: oval

2005-12-04 Thread Ben Bush
On 12/4/05, Diez B. Roggisch <[EMAIL PROTECTED]> wrote:
> Ben Bush wrote:
> > I tested the following code and wanted to get the message of "oval2
> > got hit" if I click the red one. But I always got "oval1 got hit".
> > from Tkinter import *
> > root=Tk()
> > canvas=Canvas(root,width=100,height=100)
> > canvas.pack()
> > a=canvas.create_oval(10,10,20,20,tags='oval1',fill='blue')
> > b=canvas.create_oval(50,50,80,80,tags='oval2',fill='red')
> > def myEvent(event):
> > if a:
>
> Here is your problem. a is a name, bound to some value. So - it is true,
> as python semantics are that way. It would not be true if it was e.g.
>
> False, [], {}, None, ""
>
>
> What you want instead is something like
>
> if event.source == a:
> ...
>
> Please note that I don't know what event actually looks like in Tkinter,
> so check the docs what actually gets passed to you.

got AttributeError: Event instance has no attribute 'source'
-- 
http://mail.python.org/mailman/listinfo/python-list


oval

2005-12-04 Thread Ben Bush
I tested the following code and wanted to get the message of "oval2
got hit" if I click the red one. But I always got "oval1 got hit".
from Tkinter import *
root=Tk()
canvas=Canvas(root,width=100,height=100)
canvas.pack()
a=canvas.create_oval(10,10,20,20,tags='oval1',fill='blue')
b=canvas.create_oval(50,50,80,80,tags='oval2',fill='red')
def myEvent(event):
if a:
print "oval1 got hit!"
else:
print "oval2 got hit"
canvas.tag_bind('oval1','',func=myEvent)
canvas.tag_bind('oval2','',func=myEvent)
root.mainloop()
-- 
http://mail.python.org/mailman/listinfo/python-list


enter and event

2005-12-04 Thread Ben Bush
When I read python Manuel, I got confused by the following code:
def turnRed(self, event):
event.widget["activeforeground"] = "red"

self.button.bind("", self.turnRed)
I can not understand it.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: the first element in the list of list

2005-11-25 Thread Ben Bush
On 11/22/05, Fredrik Lundh <[EMAIL PROTECTED]> wrote:
> "Ben Bush" wrote:
>
> > This question just came to my mind and not assigned by anyone.
>
> given that you and "Shi Mu" are asking identical questions, and that
> you've both started to send questions via direct mail, can you please
> ask your course instructor to contact me asap.
>
> thanks /F
Hi Fredrik,
Thanks for your help.
Miss Mu is my friend and we are learning Python by ourselves. Because
we have too many naive questions and we discussed first then posted
online either though her or me.
Actually we are looking for some good online python courses about data
structure. Both of us are using gmail for easier to track everything
that has been posted under the topic. That is why brings you
confusion.
Thanks a lot!
Ben
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: drawline

2005-11-25 Thread Ben Bush
On 11/23/05, Magnus Lycka <[EMAIL PROTECTED]> wrote:
> Ben Bush wrote:
> > I had the following code and when I clicked the left mouse button one
> > time. I got green line and the second click got a purple line and the
> > green disappeared.
> > I was confused by two questions:
>
> It's good that these postings are archived, so that teachers can
> check them before grading their students. After all,  the grades
> belong to those who solved the problems, not to those who handed
> them in...
I learned Python by myself and not seeking to hand in these results
for anyone. All the questions are not from any textbook. Please be
polite and do not assume something just in your mind.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: after sorted from the lists

2005-11-22 Thread Ben Bush
On 11/22/05, Ben Bush <[EMAIL PROTECTED]> wrote:
> On 11/22/05, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
> > >>> ll = [[1,2],[2,1],[3,1],[1,4],[3,3],[1,4]]
> > >>> ls = [frozenset(i) for i in ll]
> > >>> ss = set(ls)
> > >>> ss
> > set([frozenset([1, 3]), frozenset([1, 2]), frozenset([1, 4]), 
> > frozenset([3])])
> > >>> [list(i) for i in ss]
> > [[1, 3], [1, 2], [1, 4], [3]]
> why this happens?
> >>> from sets import *
> >>> ll = [[1,2],[2,1],[3,1],[1,4],[3,3],[1,4]]
> >>> ls = [frozenset(i) for i in ll]
> Traceback (most recent call last):
>  File "", line 1, in ?
> NameError: name 'frozenset' is not defined
>
I know that:
Python 2.3 introduced the sets module. C implementations of set data
types have now been added to the Python core as two new built-in
types, set(iterable) and frozenset(iterable).
But why it does not work in my machine?
PythonWin 2.3.5 (#62, Feb  8 2005, 16:23:02) [MSC v.1200 32 bit
(Intel)] on win32.
Portions Copyright 1994-2001 Mark Hammond ([EMAIL PROTECTED])
- see 'Help/About PythonWin' for further copyright information.
>>> from sets import *
>>> dir(frozenset)
Traceback (most recent call last):
File "", line 1, in ?
NameError: name 'frozenset' is not defined
-- 
http://mail.python.org/mailman/listinfo/python-list


drawline

2005-11-22 Thread Ben Bush
I had the following code and when I clicked the left mouse button one
time. I got green line and the second click got a purple line and the
green disappeared.
I was confused by two questions:
First, Clicknum increases when every time I click the button. Is it
possible to reset Clicknum to 0?
Second, how to do something like: I click the left button the first
time, the green line appear then disappear, the purple line
appear(just as the code has done but become a loop; if a second-time
click is implied, the loop stops.
from Tkinter import *

ClickNum = 0

def drawline(event):
   global ClickNum
   if ClickNum == 0:
   canvas.create_line(100, 0, 100, 200, arrow=FIRST,fill="green",tag="a")
   elif ClickNum == 1:
   canvas.delete("a")
   canvas.create_line(100, 50, 60, 300, arrow=FIRST,fill="purple")
   ClickNum += 1

tk = Tk()
canvas = Canvas(tk, bg="white", bd=0, highlightthickness=0)
canvas.pack(fill=BOTH, expand=YES)
canvas.create_line(100, 200, 350, 200, arrow=LAST,fill='red')
canvas.bind("<1>", drawline)
tk.mainloop()
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: after sorted from the lists

2005-11-22 Thread Ben Bush
On 11/22/05, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
> >>> ll = [[1,2],[2,1],[3,1],[1,4],[3,3],[1,4]]
> >>> ls = [frozenset(i) for i in ll]
> >>> ss = set(ls)
> >>> ss
> set([frozenset([1, 3]), frozenset([1, 2]), frozenset([1, 4]), frozenset([3])])
> >>> [list(i) for i in ss]
> [[1, 3], [1, 2], [1, 4], [3]]
why this happens?
>>> from sets import *
>>> ll = [[1,2],[2,1],[3,1],[1,4],[3,3],[1,4]]
>>> ls = [frozenset(i) for i in ll]
Traceback (most recent call last):
  File "", line 1, in ?
NameError: name 'frozenset' is not defined
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: the first element in the list of list

2005-11-22 Thread Ben Bush
On 11/22/05, Fredrik Lundh <[EMAIL PROTECTED]> wrote:
> Ben Bush wrote:
>
> > I have a lis:
> > [[1,3],[3,4],[5,6],[8,9],[14,0],[15,8]]
> > I want a code to test when the difference between the first element in
> > the list of list is equal to or larger than 6, then move the previous
> > lists to the end of the list. that is:
> > [[14,0],[15,8],[1,3],[3,4],[5,6],[8,9]]
>
> have you tried doing this yourself?  how far did you get?  can you
> post the code so we can help you figure out what you need to fix?
> is this a homework assignment?
>
> here's a oneliner:
>
>L = [x for x in L if x[0]-x[1] >= 6] + [x for x in L if x[0]-x[1] < 6]
>
> depending on how you define "difference", you may need to replace
> x[0]-x[1] with abs(x[0]-x[1])

Thanks! it works!
This question just came to my mind and not assigned by anyone.
I did not know '+' can be used to link the tow lists.
By the way, where i can find more information about how to use List
and data structure in Python.
really amazed by its use!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: after sorted from the lists

2005-11-22 Thread Ben Bush
On 11/22/05, Ben Bush <[EMAIL PROTECTED]> wrote:
> I have a list:
> [[1,2],[2,1],[3,1],[1,4],[3,3],[1,4]]
> How to remove all the duplicate or same after sorted from the lists?
> That is, [1,2] and [2,1] are the same after sorting them.
> I want the result to be:
> [[1,2],[3,1],[1,4],[3,3]]

I want to set it first but it does not work:
>>> q=[[1,2],[2,1],[3,1],[1,4],[3,3],[1,4]]
>>> set(q)
Traceback (most recent call last):
  File "", line 1, in ?
  File "C:\Python23\lib\sets.py", line 429, in __init__
self._update(iterable)
  File "C:\Python23\lib\sets.py", line 374, in _update
data[element] = value
TypeError: list objects are unhashable
-- 
http://mail.python.org/mailman/listinfo/python-list


the first element in the list of list

2005-11-22 Thread Ben Bush
I have a lis:
[[1,3],[3,4],[5,6],[8,9],[14,0],[15,8]]
I want a code to test when the difference between the first element in
the list of list is equal to or larger than 6, then move the previous
lists to the end of the list. that is:
[[14,0],[15,8],[1,3],[3,4],[5,6],[8,9]]
-- 
http://mail.python.org/mailman/listinfo/python-list


after sorted from the lists

2005-11-22 Thread Ben Bush
I have a list:
[[1,2],[2,1],[3,1],[1,4],[3,3],[1,4]]
How to remove all the duplicate or same after sorted from the lists?
That is, [1,2] and [2,1] are the same after sorting them.
I want the result to be:
[[1,2],[3,1],[1,4],[3,3]]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Tkinter's coordinates setting

2005-11-22 Thread Ben Bush

On 11/21/05, Steve Juranich <[EMAIL PROTECTED]> wrote:
On 11/17/05, Shi Mu <[EMAIL PROTECTED]> wrote:
> why subtract 1 from max_y - original_y?Because in the computer science world we like starting to count at 0.image_size = 1000original_y = 25 # Really the 26th pixel line.new_y = 1000 - 25 - 1 # 26 pixels from the bottom of the screen.
--Steve JuranichTucson, AZUSA--http://mail.python.org/mailman/listinfo/python-listThanks!
-- 
http://mail.python.org/mailman/listinfo/python-list

How to draw a dash line in the Tkinter?

2005-11-17 Thread Ben Bush
How to draw a dash line in the Tkinter?
-- 
http://mail.python.org/mailman/listinfo/python-list

line order

2005-11-17 Thread Ben Bush
I run the following code and the red line and black line show at the same time. is there anyway to show the red line first, then the black line? for example, after I click the 'enter' key?
 
from Tkinter import *tk = Tk()canvas = Canvas(tk, bg="white", bd=0, highlightthickness=0)canvas.pack(fill=BOTH, expand=YES)canvas.create_line(100, 200, 350, 200, arrow=LAST,fill='red')canvas.create_line
(100, 0, 100, 200, arrow=FIRST)tk.mainloop()
-- 
http://mail.python.org/mailman/listinfo/python-list

about try and exception

2005-11-17 Thread Ben Bush
I wrote the following code to test the use of "try...exception",
and I want n to be printed out. However, the following code's output is:
Traceback (most recent call last):  File "C:\Python23\lib\site-packages\Pythonwin\pywin\framework\scriptutils.py", line 310, in RunScript    exec codeObject in __main__.__dict__  File "C:\py\use\tryExVa.py", line 7, in ?
    print nNameError: name 'n' is not defined
 
the code is here:
 
try:    m=2/0    n=1/2except ZeroDivisionError:    passprint "Yes!!! This line will always print"print n
-- 
http://mail.python.org/mailman/listinfo/python-list

Tkinter's coordinates setting

2005-11-17 Thread Ben Bush
Tkinter's coordinates setting are: the left upper corner is the smallest X and Y, which is different from our usual think that Y is largest in that location. If i draw some lines on the canvas and show some relationship among them, do I need transfer the coordinates?
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: compare list

2005-11-15 Thread Ben Bush

On 11/15/05, Simon Brunning <[EMAIL PROTECTED]> wrote:
On 15/11/05, Ben Bush <[EMAIL PROTECTED]> wrote:> an error reported:
> Traceback (most recent call last):>   File> "C:\Python23\lib\site-packages\Pythonwin\pywin\framework\scriptutils.py",> line 310, in RunScript> exec codeObject in __main__.__dict__
>   File "C:\temp\try.py", line 8, in ?> from sets import Set as set> ImportError: cannot import name Set> >>>Works for me. You don't have a sets module of your own, do you? Try
this, and report back what you see:import setsprint sets.__file__print dir(sets)--Cheers,Simon B,[EMAIL PROTECTED],
http://www.brunningonline.net/simon/blog/
 
I found I named the following python file as sets.py, which brought the problem (is that right?). i changed it to other name and it works.But the logic output is wrong.
from sets import Set as setlisA=[1,2,5,9]lisB=[9,5,0,2]lisC=[9,5,0,1]def two(sequence1, sequence2):   set1, set2 = set(sequence1), set(sequence2)   return len(set1.intersection(set2)) == 2
print two(lisA,lisB)
False(should be true!)-- Thanks!Ben Bush 
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: compare list

2005-11-15 Thread Ben Bush

On 11/15/05, Simon Brunning <[EMAIL PROTECTED]> wrote:
On 15/11/05, Shi Mu <[EMAIL PROTECTED]> wrote:
> Yes, i am using python 2.3,> I have used from sets import *> but still report the same error:> > > Traceback (most recent call last):> > >   File "", line 1, in ?
> > > NameError: name 'set' is not definedI said analogous, not identical. try (untested):from sets import Set as set--Cheers,Simon B,
[EMAIL PROTECTED],http://www.brunningonline.net/simon/blog/--http://mail.python.org/mailman/listinfo/python-list

an error reported:
Traceback (most recent call last):  File "C:\Python23\lib\site-packages\Pythonwin\pywin\framework\scriptutils.py", line 310, in RunScript    exec codeObject in __main__.__dict__  File "C:\temp\try.py", line 8, in ?
    from sets import Set as setImportError: cannot import name Set>>> -- Thanks!Ben Bush 
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: compare list

2005-11-15 Thread Ben Bush
On 11/15/05, Brian van den Broek <[EMAIL PROTECTED]> wrote:
Ben Bush said unto the world upon 2005-11-15 01:24:
>>  Unfortunately, the indents got screwed up along the way. But the part>>>of my code you asked about was:>>>>for item in list1:>>if item in list2:>>if item + 1 in list1 and item + 1 in list2:
>>return True>>>>In some detail:>>Does that clarify it?>>>>Finally, your response to Alex would have been much more useful if
>>you'd quoted the error rather than just asserting that you got an>>error :-)>>>>Best,>>>>Brian vdBHi Ben,first, while there are those on the list/n.g. who differ, the
majoritarian view is that top posting isn't a good thing. At minimum,if someone bothered to correct it, it would be nice if in a furtherfollow-up you didn't top-post again :-)Second, you might find the tutor list really helpful. It is where I
learned most of what I know, and I still read it more than c.l.p. Itis very newbie friendly.As for your question:>> Hi Brian,>> regarding "if item + 1 in list1 and item + 1 in list2:",
> my understanding is this will check whether the following item ineach list> is the same. How does the code permit the situation that the orderdoes not> matter?> For example, for lisA and lisB, the comparison is true and the two
lists> have 5 and 6 but different order.>  lisA=[1,2,3,4,5,6,9]> lisB=[1,6,5]>  Many Thanks!There are two distinct issues that might be the source of yourconfusion. I will be explicit about both; pardon if only one applied.
>>> num_list = [1, 42, 451]>>> for item in num_list: print item, type(item)1 42 451 >>>Iterating over a list as I did makes item refer to each list member in
turn. So, item + 1 doesn't refer to the next item (except by accidentas it were when two items are sequential ints). Rather, it refers toint that results from adding 1 to the int that is item.You might be thinking of list index notation instead:
>>> index = 1>>> num_list[index], num_list[index + 1](42, 451)>>>(General tip: putting a print or two in my original code would havecleared that up very quickly.)
So, that cleared up, are you wondering why item + 1 suffices, insteadof both item + 1 and item - 1?If so, consider that it the list1 has both n and n - 1 in it and weiterate over list1 checking each case, eventually item will refer to n
- 1. In that case, item + 1 = n and we are covered after all. I did asI did in my original code thinking it was probably quicker to haveonly one test and pay the cost that there might be a quicker exit from
the iteration were I to test both item + 1 and item - 1. f itmattered, I'd test for speed. Of course, if speed mattered and I couldever remember to use sets :-) I'd follow Alex and Duncan's suggestionsinstead!
If that doesn't clear it up, give yourself a few short lists and runtest cases having inserted print statements to see what is going on.To stop it all from whizzing by too fast, put inraw_input("Hit enter and I'll keep working")
somewhere in the loops to slow things down.HTH,Brian vdB
Brian,
Really appreciate your help!!!
Ben-- Thanks!Ben Bush 
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: compare list

2005-11-14 Thread Ben Bush

On 11/14/05, Ben Bush <[EMAIL PROTECTED]> wrote:


Hijacking Brian's response since my newsserver never god Ben's originalrequest...:I would program this at a reasonably high abstraction level, based on 
sets -- since you say order doesn't matter, sets are more appropriatethan lists anyway.  For example:def two_cont_in_common(x, y):   common = set(x).intersection(y)   return bool(common.intersection
 (z+1 for z in common))i.e., given two lists or whatever, first build the set of all elementsthey have in common, then check if that set contains two continuouselements.  Then, as Brian suggests, you can use this dyadic function on 
a reference list and as many others as you wish:def bens_task(reference, others):   return [two_cont_in_common(alist, reference) for alist in others]The call bens_task(lisA, lisB, lisC, lisD) will give you essentially 
what you require, except it uses True and False rather than 1 and 0; ifyou need to fix this last issue, you can add an int(...) call aroundthese booleans in either of the functions in question.Alex 

 
I got invalid syntax error when i run Alex's code. do not know why. 
Alex,
I use PythonWin to run:
def two_cont_in_common(x, y):   common = set(x).intersection(y)   return bool(common.intersection(z+1 for z in common))def bens_task(reference, others):   return [two_cont_in_common(alist, reference) for alist in others] 

 
I got invalid syntax error and the mouse's cursor stay on the 'for' in the statement of    return bool(common.intersection(z+1 for z in common))-- Thanks!Ben Bush 
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: compare list

2005-11-14 Thread Ben Bush
 
Hi Brian,
regarding "if item + 1 in list1 and item + 1 in list2:",
my understanding is this will check whether the following item in each list is the same. How does the code permit the situation that the order does not matter?
For example, for lisA and lisB, the comparison is true and the two lists have 5 and 6 but different order.
 
lisA=[1,2,3,4,5,6,9]lisB=[1,6,5]
 
Many Thanks! 
Unfortunately, the indents got screwed up along the way. But the partof my code you asked about was:
for item in list1:if item in list2:if item + 1 in list1 and item + 1 in list2:return TrueIn some detail:Consider each item in list1 in turn. If the item isn't in list2, move
on, as there is no chance of it meeting your test. If the item is inlist2, then it is pointful to check if your test is met. Given thatyou wanted consecutive numbers, the 'if item + 1 in list1 and item + 1
in list2' condition checks if both lists (which contain item if we'vegone this far) also contain item + 1. If they do, you wanted thefunction to return 1 (I changed it to True as more idiomatic). Aftermy for loop is a return False which we will only hit if the condition
you wanted to test was never satisfied.Does that clarify it?Finally, your response to Alex would have been much more useful ifyou'd quoted the error rather than just asserting that you got anerror :-)
Best,Brian vdB-- Thanks!Ben Bush
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: about invalid syntax

2005-11-14 Thread Ben Bush

On 11/14/05, adDoc's networker Phil <[EMAIL PROTECTED]> wrote:
. maybe you could separate your code into parts { python std, pythonwin-specific},and then use a debugger to know most of the problem sources?
(I'm not familiar with pythonwin, I assume it's a superset of python std) .

On 11/14/05, Ben Bush <
[EMAIL PROTECTED] > wrote:


When I run scripts in PythonWin,
sometimes will get the message of invalid syntax error.
How can I check which error I made?
For example, in VB, you might got the wrong place highlighted and help message too.
-- 
which IDE do you use to run Python?Thanks!Ben Bush 
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: compare list

2005-11-14 Thread Ben Bush

[snip]That's potentially very expensive for large lists, although simplyconverting the lists to sets should give a significant speed up.  I
think the following is *possibly* as good a way as any.>>> def compare(list1, list2):   intersection = sorted(list(set(list1) & set(list2)))   for i in range(len(intersection) - 1):
   if intersection[i] == intersection[i+1] - 1:   return True   return FalseDuncan
 Why it is potentially very expensive for large lists? what does intersection mean in Duncan's code? Thanks a lot for your patience!
-- Thanks!Ben Bush
-- 
http://mail.python.org/mailman/listinfo/python-list

about invalid syntax

2005-11-14 Thread Ben Bush
When I run scripts in PythonWin,
sometimes will get the message of invalid syntax error.
How can I check which error I made?
For example, in VB, you might got the wrong place highlighted and help message too.-- Thanks!Ben Bush
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: compare list

2005-11-14 Thread Ben Bush

Hijacking Brian's response since my newsserver never god Ben's originalrequest...:I would program this at a reasonably high abstraction level, based on
sets -- since you say order doesn't matter, sets are more appropriatethan lists anyway.  For example:def two_cont_in_common(x, y):   common = set(x).intersection(y)   return bool(common.intersection
(z+1 for z in common))i.e., given two lists or whatever, first build the set of all elementsthey have in common, then check if that set contains two continuouselements.  Then, as Brian suggests, you can use this dyadic function on
a reference list and as many others as you wish:def bens_task(reference, others):   return [two_cont_in_common(alist, reference) for alist in others]The call bens_task(lisA, lisB, lisC, lisD) will give you essentially
what you require, except it uses True and False rather than 1 and 0; ifyou need to fix this last issue, you can add an int(...) call aroundthese booleans in either of the functions in question.Alex

 
I got invalid syntax error when i run Alex's code. do not know why. 
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: compare list

2005-11-14 Thread Ben Bush
what does the following code mean?
 if item in list2:                        if item + 1 in list1 and item + 1 in list2: 
On 11/14/05, Brian van den Broek <[EMAIL PROTECTED]> wrote:
Ben Bush said unto the world upon 2005-11-14 05:51:> I have four lists:> lisA=[1,2,3,4,5,6,9]
> lisB=[1,6,5]> lisC=[5,6,3]> lisD=[11,14,12,15]> how can I write a function to compare lisB, lisC and lisD with lisA, if they> share two continuous elements (the order does not matter), then return 1,
> otherwise return 0. For example, lisA, lisB and lisC have 5,6 or 6,5 so> these comparison will return 1 but the comparison between lisD and lisA> return 0.> --> Thanks!> Ben Bush
Hi Ben,the code below is tested no further than shown. And, I'm no expert --I imagine there are better ways. With luck, I'll learn them in afollow-up from someone more skilled :-)>>> def list_common_continuity_comp(list1, list2):
for item in list1:if item in list2:if item + 1 in list1 and item + 1 in list2:return Truereturn False>>> lisA=[1,2,3,4,5,6,9]
>>> lisB=[1,6,5]>>> lisC=[5,6,3]>>> lisD=[11,14,12,15]>>> list_common_continuity_comp(lisA, lisB)True>>> list_common_continuity_comp(lisA, lisC)True
>>> list_common_continuity_comp(lisA, lisD)False>>> list_common_continuity_comp(lisD, lisA)False>>> list_common_continuity_comp(lisA, lisA)True>>> list_common_continuity_comp(lisB, lisA)
TrueThat gets you a binary comparison. To make it a polyadic:>>> def multi_list_continuity_comp(list_of_lists):list1 = list_of_lists[0]for other_list in list_of_lists[1:]:
if not list_common_continuity_comp(list1, other_list):return Falsereturn True>>> meta_list1 = [lisA, lisB, lisC, lisD]>>> meta_list2 = meta_list1[:-1]
>>> multi_list_continuity_comp(meta_list1)False>>> multi_list_continuity_comp(meta_list2)True>>>Some edge cases haven't been handled:>>> multi_list_continuity_comp([])
Traceback (most recent call last):  File "", line 1, in -toplevel-multi_list_continuity_comp([])  File "", line 2, in multi_list_continuity_comp
list1 = list_of_lists[0]IndexError: list index out of range>>>but this should get you started.Best,Brian vdB-- Thanks!
Ben Bush
-- 
http://mail.python.org/mailman/listinfo/python-list

compare list

2005-11-14 Thread Ben Bush
I have four lists:
lisA=[1,2,3,4,5,6,9]
lisB=[1,6,5]
lisC=[5,6,3]
lisD=[11,14,12,15]how can I write a function to compare lisB, lisC and lisD with lisA, if they share two continuous elements (the order does not matter), then return 1, otherwise return 0. For example, lisA, lisB and lisC have 5,6 or 6,5 so these comparison will return 1 but the comparison between lisD and lisA return 0.
-- Thanks!Ben Bush 
-- 
http://mail.python.org/mailman/listinfo/python-list

extend

2005-11-14 Thread Ben Bush
is there any python code doing this:
there are two line segments (2x+y-1=0 with the coordinates of two ending points are (1,-1) and (-1,3);
x+y+6=0 with the coordinates of two ending points are (-3,-3) and 
(-4,-2);). They extend and when they meet each other, stop extending.
how can i use python to implement this in Tkinter?-- Thanks!Ben Bush
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: circle and point

2005-11-14 Thread Ben Bush
is there any python code doing this:
there are two line segments (2x+y-1=0 with the coordinates of two ending points are (1,-1) and (-1,3);
x+y+6=0 with the coordinates of two ending points are (-3,-3) and 
(-4,-2);). They extend and when they meet each other, stop extending.
how can i use python to implement this in Tkinter?-- Thanks!Ben Bush
-- 
http://mail.python.org/mailman/listinfo/python-list

open source and pure python

2005-11-13 Thread Ben Bush
Is there any package written in pure python code?
I know lots of packages borrow the functions from other languages such as C or FORTRAN. So it is still black box to me because I do not know these languages.
Ben
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: the button of cancel

2005-11-13 Thread Ben Bush

On 11/13/05, Fredrik Lundh <[EMAIL PROTECTED]> wrote:
Ben Bush wrote:> When I click the button of cancel,> I got the follwoing message. I want to see "Goodbye" on the console screen.
> What to do?> KeyboardInterrupt: operation cancelled> num = input( "Enter a number between 1 and 100: " )> while num < 1 or num > 100:>print "Oops, your input value (", num, ") is out of range."
>num = input( "Be sure to enter a value between 1 and 100: " )you need to catch the exception, and print the message you want.   try:   ... your code ...   except KeyboardInterrupt:
   print "Goodbye!"see chapter 8.3 in the tutorial for more info:   http://docs.python.org/tut/node10.htmlThanks, now the code works. I wonder when i put the number between 1 and 100, I get the message of 'Your number is in the range' but i still see the input box. How to remove it?
num = input( "Enter a number between 1 and 100: " )try:    while num < 1 or num > 100:    print 'Oops, your input value (', num, ') is out of range.'    num = input( 'Be sure to enter a value between 1 and 100: ' )
    num=input('Your number is in the range')except KeyboardInterrupt:    print 'Goodbye!'
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: circle and point

2005-11-12 Thread Ben Bush

On 11/12/05, Robert Kern <[EMAIL PROTECTED]> wrote:
Ben Bush wrote:> is there any code to decide whether a point is located within the circle> by three other points?
# Converted from my C++ code.# C.f. http://www.ics.uci.edu/~eppstein/junkyard/circumcenter.htmldef circumcenter(x0, y0,x1, y1,
x2, y2):   x0m2 = x0 - x2   y1m2 = y1 - y2   x1m2 = x1 - x2   y0m2 = y0 - y2   x0p2 = x0 + x2   y1p2 = y1 + y2   x1p2 = x1 + x2   y0p2 = y0 + y2   D = x0m2*y1m2 - x1m2*y0m2
   # You probably want to test for D being close to 0 here   centerx = (((x0m2*x0p2 + y0m2*y0p2)/2*y1m2) - (x1m2*x1p2 + y1m2*y1p2)/2*y0m2) / D   centery = (((x1m2*x1p2 + y1m2*y1p2)/2*x0m2)
 - (x0m2*x0p2 + y0m2*y0p2)/2*x1m2) / D   return centerx, centerydef incircle(x0, y0,x1, y1,x2, y2,x,  y):   centerx, centery = circumcenter(x0, y0, x1, y1, x2, y2)
   return ((x-centerx)**2 + (y-centery)**2 <=   (x0-centerx)**2 + (y0-centery)**2)--Robert Kern[EMAIL PROTECTED]"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."-- Richard HarterThanks!
-- 
http://mail.python.org/mailman/listinfo/python-list

circle and point

2005-11-12 Thread Ben Bush
is there any code to decide whether a point is located within the circle by three other points?Thanks!B. Bush
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: triangulation

2005-11-12 Thread Ben Bush
Is there any pure python ocde there thoguh it is slow?
Not many people know C very well and it will be great to have some python code even for education use.
Ben
 
I know someone once mentioned that they tried writing one of theDelaunay triangulation algorithms in pure Python and abandoned it forbeing unusably slow.--Robert Kern
[EMAIL PROTECTED]"In the fields of hell where the grass grows high Are the graves of dreams allowed to die." -- Richard Harter
-- 
http://mail.python.org/mailman/listinfo/python-list

the button of cancel

2005-11-12 Thread Ben Bush
When I click the button of cancel,
I got the follwoing message. I want to see "Goodbye" on the console screen.What to do?
 
Traceback (most recent call last):  File "C:\Python23\lib\site-packages\Pythonwin\pywin\framework\scriptutils.py", line 310, in RunScript    exec codeObject in __main__.__dict__  File "C:\py\use\whileprint.py", line 8, in ? 
    num = input( "Be sure to enter a value between 1 and 100: " )  File "C:\Python23\lib\site-packages\Pythonwin\pywin\framework\app.py", line 368, in Win32Input    return eval(raw_input(prompt)) 
  File "C:\Python23\lib\site-packages\Pythonwin\pywin\framework\app.py", line 363, in Win32RawInput    raise KeyboardInterrupt, "operation cancelled"KeyboardInterrupt: operation cancelled 

 
num = input( "Enter a number between 1 and 100: " )while num < 1 or num > 100:    print "Oops, your input value (", num, ") is out of range."    num = input( "Be sure to enter a value between 1 and 100: " )

-- 
http://mail.python.org/mailman/listinfo/python-list