By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,957 Members | 1,936 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,957 IT Pros & Developers. It's quick & easy.

Set operations for lists: pythonic hints please!

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
Ouppsss!
the title should have read:Set operation for tuples...
(sigh!)

Oct 20 '05 #2

P: n/a
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.