473,666 Members | 2,571 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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,wid th=410,height=4 10)
m1.pack(fill=BO TH, expand=1)
for i in range(10):
x=random.randra nge(1,400)
y=random.randra nge(1,400)
m1.create_oval( x,y,x+10,y+10,f ill="red",tags= ("etoile","roug e"))

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

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

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

m1.create_recta ngle(x,y,x+10,y +10,fill="green ",tags=("artefa ct","green"))

#
i=m1.find_witht ag("artefact")
j=m1.find_witht ag("rouge")

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

s3=s1.intersect ion(s2)
myIntersection= tuple(s3._data. keys())

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

root.mainloop()

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

Oct 20 '05 #2
jm*********@gma il.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("r ouge") and
item.has_tag("a rtefact")]

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
4312
by: Hilde Roth | last post by:
This may have been asked before but I can't find it. If I have a rectangular list of lists, say, l = ,,], is there a handy syntax for retrieving the ith item of every sublist? I know about for i in l] but I was hoping for something more like l. Hilde
13
1842
by: Amit Khemka | last post by:
Hello, Is there a *direct* way of doing set operations on lists which preserve the order of the input lists ? For Ex. l1 = l2 = and (l1 intersect l2) returns .... (and (l2 intersect l1) returns ) thanks in advance, amit.
9
4568
by: SMB | last post by:
I have two lists of data like the following: LIST1 , ] LIST2 , 'label': 'First Name', 'width': 0L, 'separator': ',', 'height': 0L, 'type': 2L, 'order': 1L}, {'code': 14L, 'name': 'Last Name', 'value': , 'label': 'Last Name', 'width': 0L, 'separator': ',', 'height': 0L, 'type': 2L, 'order': 2L},
3
1789
by: Hallvard B Furuseth | last post by:
I'm wondering how to design this: An API to let a request/response LDAP server be configured so a user-defined Python module can handle and/or modify some or all incoming operations, and later the outgoing responses (which are generated by the server). Operations have some common elements, and some which are distinct to the operation type (search, modify, compare, etc). So do responses. There are also some "operations" used...
1
1682
by: Simon Forman | last post by:
I've got a function that I'd like to improve. It takes a list of lists and a "target" element, and it returns the set of the items in the lists that appear either before or after the target item. (Actually, it's a generator, and I use the set class outside of it to collect the unique items, but you get the idea. ;-) ) data = , , ,
11
1513
by: Hakusa | last post by:
Pythonic lists are everything I want in a one dimensional array . . . but I'm trying to do a text adventure and simplify the proces by creating a grid as opposed to a tedious list of rooms this room connects to. So I want to know a good way to do a SIMPLE two dimensional array. Python's lists are a little confusing when it comes to how to do this. I realized that I could do list = * N ] * N
16
3649
by: agent-s | last post by:
Basically I'm programming a board game and I have to use a list of lists to represent the board (a list of 8 lists with 8 elements each). I have to search the adjacent cells for existing pieces and I was wondering how I would go about doing this efficiently. Thanks
5
44440
by: TokiDoki | last post by:
Hi! I have a Python problem which is my last problem to solve to finish up a Django application. This is amazingly simple but I have been stuck now for a couple of days. It is embarrisingly simple. . . but not simple for me. I have two lists. I end up creating the two lists, with the end result being that each gets created at a different indent level: mylist = entry.objects.filter(title__contains=searchterms1) |...
11
7165
by: breal | last post by:
I have three lists... for instance a = ; b = ; c = ; I want to take those and end up with all of the combinations they create like the following lists
0
8363
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8883
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8561
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8645
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6203
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4372
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2776
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1778
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.