468,303 Members | 1,405 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,303 developers. It's quick & easy.

Set operations for lists: pythonic hints please!

Working with several thousand tagged items on a Tkinter Canvas, I want
to change different configurations of objects having a certain group of
tags.

I've used the sets module, on the tuple returned by Tkinter.Canvas.
find_withtag() method.

Since this method takes only one tag at time, I use it for each tag
that is part of the look-up group, create a set object for each tuple
and intersect the resulting sets, that I tuple out again to feed a loop
to change the configuration of the items.

Anyone have a more pythonic way (or better performing ) way to suggest
?

Thanks in advance,

Jean-Marc

# My test code
# the code is not generic here
# my question only pertains to the intersection algorithm perse

from Tkinter import *
import random, sets

root=Tk()

m1 = Canvas(root,width=410,height=410)
m1.pack(fill=BOTH, expand=1)
for i in range(10):
x=random.randrange(1,400)
y=random.randrange(1,400)
m1.create_oval(x,y,x+10,y+10,fill="red",tags=("eto ile","rouge"))

for i in range(10):
x=random.randrange(1,400)
y=random.randrange(1,400)

m1.create_rectangle(x,y,x+10,y+10,fill="red",tags= ("artefact","rouge"))

for i in range(10):
x=random.randrange(1,400)
y=random.randrange(1,400)

m1.create_rectangle(x,y,x+10,y+10,fill="green",tag s=("artefact","green"))

#
i=m1.find_withtag("artefact")
j=m1.find_withtag("rouge")

s1=sets.Set(i)
s2=sets.Set(j)

s3=s1.intersection(s2)
myIntersection=tuple(s3._data.keys())

for i in myIntersection:
m1.itemconfig(i,fill="black")

root.mainloop()

Oct 20 '05 #1
2 1553
Ouppsss!
the title should have read:Set operation for tuples...
(sigh!)

Oct 20 '05 #2
jm*********@gmail.com wrote:
Working with several thousand tagged items on a Tkinter Canvas, I want
to change different configurations of objects having a certain group of
tags.

I've used the sets module, on the tuple returned by Tkinter.Canvas.
find_withtag() method.

Since this method takes only one tag at time, I use it for each tag
that is part of the look-up group, create a set object for each tuple
and intersect the resulting sets, that I tuple out again to feed a loop
to change the configuration of the items.

Anyone have a more pythonic way (or better performing ) way to suggest
?


It depends. If the search on tags is implemented linear, it might be
better to loop over all items and filter suing a compound predicate,
like this:

rouge_artefacts = [item for m1.all_itmes() if item.has_tag("rouge") and
item.has_tag("artefact")]

Please note that I don't know TKinter's API, but I can't imagine that it
lacks the possibility to fettch a list of all items and query an item
for it tags. The synopsis may vary from above, thogh.

But if tkinter stores the tagged items as mapping of tag -> set/list,
then I think your approach is as fast as you can get. Time it out. And
maybe another thing would be to manage that tagging relation yourself.

Regards,

Diez
Oct 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

21 posts views Thread by Hilde Roth | last post: by
13 posts views Thread by Amit Khemka | last post: by
9 posts views Thread by SMB | last post: by
3 posts views Thread by Hallvard B Furuseth | last post: by
11 posts views Thread by Hakusa | last post: by
11 posts views Thread by breal | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by Teichintx | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.