473,626 Members | 3,351 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Simple - looking for a way to do an element exists check..

Hi all,

I have a simple list to which I want to append another tuple if
element 0 is not found anywhere in the list.

element = ('/smsc/chp/aztec/padlib/5VT.Cat',
'/smsc/chp/aztec/padlib',
'5VT.Cat', (33060))

element1 = ('/smsc/chp/aztec/padlib/5VT.Cat2',
'/smsc/chp/aztec/padlib',
'5VT.Cat2', (33060))

a = [ ('/smsc/chp/aztec/padlib/5VT.Cat',
'/smsc/chp/aztec/padlib',
'5VT.Cat', (33060)),
('/smsc/chp/aztec/padlib/padlib.TopCat%' ,
'/smsc/chp/aztec/padlib',
'padlib.TopCat% ', (33204)),
('/smsc/chp/aztec/padlib/Regulators.Cat% ',
'/smsc/chp/aztec/padlib',
'Regulators.Cat %', (33204))]

So my code would look something like this.

found = False
for item in a:
if item[0] == element[0]
found = True
break
if not found:
a.append(elemen t)

But this is just ugly - Is there a simpler way to interate over all
items in a without using a found flag?

Thanks
Feb 22 '08 #1
11 1379
On Feb 22, 11:20*am, rh0dium <steven.kl...@g mail.comwrote:
>
found = False
for item in a:
* if item[0] == element[0]
* * found = True
* * break
if not found:
* a.append(elemen t)

But this is just ugly - Is there a simpler way to interate over all
items in a without using a found flag?

Thanks

for item in a:
if item[0] == element[0]
break
else: # only called if we never 'break' out of the for loop
a.append(elemen t)
But what about a dict?

adict = dict((elem[0],elem) for elem in a)

if item[0] not in adict:
adict[item[0]] = item

# need the final list?
a = adict.values()

No list searching, and will scale well if a gets real long.

-- Paul
Feb 22 '08 #2
On Feb 22, 11:20*am, rh0dium <steven.kl...@g mail.comwrote:
Hi all,

I have a simple list to which I want to append another tuple if
element 0 is not found anywhere in the list.

element = *('/smsc/chp/aztec/padlib/5VT.Cat',
* '/smsc/chp/aztec/padlib',
* '5VT.Cat', (33060))

element1 = *('/smsc/chp/aztec/padlib/5VT.Cat2',
* '/smsc/chp/aztec/padlib',
* '5VT.Cat2', (33060))

a = *[ ('/smsc/chp/aztec/padlib/5VT.Cat',
* '/smsc/chp/aztec/padlib',
* '5VT.Cat', (33060)),
*('/smsc/chp/aztec/padlib/padlib.TopCat%' ,
* '/smsc/chp/aztec/padlib',
* 'padlib.TopCat% ', (33204)),
*('/smsc/chp/aztec/padlib/Regulators.Cat% ',
* '/smsc/chp/aztec/padlib',
* 'Regulators.Cat %', (33204))]

So my code would look something like this.

found = False
for item in a:
* if item[0] == element[0]
* * found = True
* * break
if not found:
* a.append(elemen t)

But this is just ugly - Is there a simpler way to interate over all
items in a without using a found flag?

Thanks
Well, that's what I get for typing before thinking...

If the remaining items in each element tuple are the same for any
given element[0], then just use a set.

aset = set(a)
for element in list_of_new_ele ment_tuples:
aset.add(elemen t)

-- Paul
Feb 22 '08 #3
On Feb 22, 10:20 am, rh0dium <steven.kl...@g mail.comwrote:
Hi all,

I have a simple list to which I want to append another tuple if
element 0 is not found anywhere in the list.

element = ('/smsc/chp/aztec/padlib/5VT.Cat',
'/smsc/chp/aztec/padlib',
'5VT.Cat', (33060))

element1 = ('/smsc/chp/aztec/padlib/5VT.Cat2',
'/smsc/chp/aztec/padlib',
'5VT.Cat2', (33060))

a = [ ('/smsc/chp/aztec/padlib/5VT.Cat',
'/smsc/chp/aztec/padlib',
'5VT.Cat', (33060)),
('/smsc/chp/aztec/padlib/padlib.TopCat%' ,
'/smsc/chp/aztec/padlib',
'padlib.TopCat% ', (33204)),
('/smsc/chp/aztec/padlib/Regulators.Cat% ',
'/smsc/chp/aztec/padlib',
'Regulators.Cat %', (33204))]

So my code would look something like this.

found = False
for item in a:
if item[0] == element[0]
found = True
break
if not found:
a.append(elemen t)

But this is just ugly - Is there a simpler way to interate over all
items in a without using a found flag?

Thanks
How-about using a generator expression and Python's built-in "in"
operator:
>>def example(myData, newData):
.... if newData[0] not in (x[0] for x in myData):
.... myData.append( newData )
....
>>l = []
example( l, ('a', 'apple', 'aviary') )
l
[('a', 'apple', 'aviary')]
>>example( l, ('s', 'spam', 'silly') )
l
[('a', 'apple', 'aviary'), ('s', 'spam', 'silly')]
>>example( l, ('s', 'suck-tastic') )
l
[('a', 'apple', 'aviary'), ('s', 'spam', 'silly')]
>>>
Feb 22 '08 #4
Paul Rubin <http://ph****@NOSPAM.i nvalidwrites:
if any(x==element[0] for x in a):
a.append(elemen t)
Should say:

if any(x[0]==element[0] for x in a):
a.append(elemen t)
Feb 22 '08 #5
On Feb 22, 12:54*pm, Paul Rubin <http://phr...@NOSPAM.i nvalidwrote:
Paul Rubin <http://phr...@NOSPAM.i nvalidwrites:
* * if any(x==element[0] for x in a):
* * * a.append(elemen t)

Should say:

* * *if any(x[0]==element[0] for x in a):
* * * * a.append(elemen t)
I think you have this backwards. Should be:

if not any(x[0]==element[0] for x in a):
a.append(elemen t)

or

if all(x[0]!=element[0] for x in a):
a.append(elemen t)

-- Paul
Feb 22 '08 #6
Paul McGuire <pt***@austin.r r.comwrites:
I think you have this backwards. Should be:

if not any(x[0]==element[0] for x in a):
a.append(elemen t)
I think you are right, it was too early for me to be reading code when
I posted that ;-)
Feb 22 '08 #7
Paul Rubin wrote:
if any(x[0]==element[0] for x in a):
How come this list comprehension isn't in [] brackets?
Feb 23 '08 #8
Boris Ozegovic wrote:
Paul Rubin wrote:
> if any(x[0]==element[0] for x in a):

How come this list comprehension isn't in [] brackets?
It isn't list comprehension, it is generator expression
http://en.wikipedia.org/wiki/Python_...or_expressions

--
Tero
Feb 23 '08 #9
TeroV wrote:
It isn't list comprehension, it is generator expression
http://en.wikipedia.org/wiki/Python_...or_expressions
Nice. :)
Feb 23 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
34368
by: Noah Spitzer-Williams | last post by:
Hello guys, I would like to do something seemingly simple: find out if an element in an array that is passed to my function exists. I used to think I could just do: if (arr) ... However, if this element's value happens to be 0, the conditional will return false, even though the element actually exists. Any ideas? Can you check if the element == null?
16
2877
by: Terry | last post by:
Hi, This is a newbie's question. I want to preload 4 images and only when all 4 images has been loaded into browser's cache, I want to start a slideshow() function. If images are not completed loaded into cache, the slideshow doesn't look very nice. I am not sure how/when to call the slideshow() function to make sure it starts after the preload has been completed.
2
6015
by: Mike | last post by:
I´ve got a number of SPAN elements named "mySpan1", "mySpan2", "mySpan3" etc, and want to set their "style.display" to "inline". This works (only needs to work on IE5.5+): for (var x = 1; x < 20; x++) { document.all('mySpan'+x).style.display = "inline"; } But I don´t know how many SPAN elements there are, so I need to set x to a
3
1662
by: Christopher Benson-Manica | last post by:
I need a simple HTML element whose text I can change dynamically. What is it? (it doesn't seem to be <div>, since I can't seem to find what properties a <div> has...) -- Christopher Benson-Manica | I *should* know what I'm talking about - if I ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
8
1469
by: Pierre Couderc | last post by:
I am looking for a "special" kind of map : - it is read like a map - if the searched element exists, it is given back imediately - if the searched element does not exist, an initialise() is called to do the job. - anyway the map has a limited size, when it is full, the oldest element is dropped ( if recalled later, will need again an initialise()) Is there some STL (or not) to do that?
27
4604
by: one man army | last post by:
Hi All- I am new to PHP. I found FAQTS and the php manual. I am trying this sequence, but getting 'no zip string found:'... PHP Version 4.4.0 $doc = new DomDocument; $res = $doc->loadHTMLFile("./aBasicSearchResult.html"); if ( $res == true ) { $zip = $doc->getElementById('zipRaw_id')->value; if ( 0 != $zip ) {
27
1833
by: karan.shashi | last post by:
Hey all, I was asked this question in an interview recently: Suppose you have the method signature bool MyPairSum(int array, int sum) the array has all unique values (no repeats), your task is to find two
4
10050
by: ug26jhw | last post by:
Is there any way to determine whether an element in a document exists. I have some code like this where the variable i changes: if(document.getElementById('pref'+i).innerHTML... When it tries to check an element that doesn't exist I get this error: document.getElementById("pref" + i) has no properties
0
8637
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8364
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
8504
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...
0
7193
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5574
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
4092
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4197
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2625
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
1511
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.