473,545 Members | 293 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

beginner, idiomatic python

Would someone like to suggest a replacement for this? It works ok,
but it doesn't look like any of the other code:

tempList = ['1','2','3','4' ,'5','6','7','8 ']
sampleList=[]
for port in tempList:
pagefound = False
for i in range(self.pare nt.GetPageCount ()):
page=self.paren t.GetPage(i)
if hasattr(page, "port"):
if page.port == int(port):
pagefound=True
if not pagefound:
sampleList.appe nd(port)

Thanks!
Aug 23 '07 #1
21 1122
bambam wrote:
Would someone like to suggest a replacement for this? It works ok,
but it doesn't look like any of the other code:

tempList = ['1','2','3','4' ,'5','6','7','8 ']
sampleList=[]
for port in tempList:
pagefound = False
for i in range(self.pare nt.GetPageCount ()):
page=self.paren t.GetPage(i)
if hasattr(page, "port"):
if page.port == int(port):
pagefound=True
if not pagefound:
sampleList.appe nd(port)

Thanks!

Look at good questions. This is a _very_ underspecified question.

One stab at mindreading:

def ported_pages(se lf):
for i in range(self.pare nt.GetPageCount ()):
if hasattr(page, 'port'):
yield page

...
tempList = ['1','2','3','4' ,'5','6','7','8 ']
missing = dict((int(v), v) for v in tempList)
for page in self.ported_pag es():
if page.port in missing:
missing.pop(pag e.port)
if not missing:
break
sampleList = missing.values( )
...

-Scott David Daniels
Aug 23 '07 #2
Does page count change? i.e. is it necessary to retrieve it in every
loop or
tempList = ['1','2','3','4' ,'5','6','7','8 ']
sampleList=[]
page_count = self.parent.Get PageCount()
<snipped>
for i in range(page_coun t):

Also, once pagefound is set to True, all pages following will not be
appended to sampleList because it is not initialized to False under
the "for i in range(self.pare nt.GetPageCount ())" loop.

Finally, if I understand the logic and question correctly, you want
something like
tempList = ['1','2','3','4' ,'5','6','7','8 ']
sampleList=[]
page_count = self.parent.Get PageCount()
for port in tempList:
for i in range(page_coun t):
page=self.paren t.GetPage(i)
if (hasattr(page, "port")) and (page.port != int(port)) :
sampleList.appe nd(port)

or perhaps (I'm not sure)
tempList = [1, 2, 3, 4, 5, 6, 7, 8]
sampleList=[]
page_count = self.parent.Get PageCount()
for i in range(page_coun t):
page=self.paren t.GetPage(i)
if (hasattr(page, "port")) and (page.port not in tempList) :
sampleList.appe nd(port)
HTH

Aug 23 '07 #3
Wos! Several different thoughts:

An object using yield to return only the relevant pages, one at a time.
Pop to remove the items from the list.
A dictionary to map between the strings and the integers.

The dictionary was particularly unexpected. Eventually, I
plan to change the string ports to device names. On the other
hand, it looks like the only reason I have port numbers is
to use as an index in things like this.

After examining your suggestion, I realised that another thing
I am interested in could be generalised: I want the complement
of the set of ports in pages, given a universal set in tempList.
Ignoring the break condition for the moment, and my problem
with int(port)/str(port), would you have offered a different solution
if I had asked for the relative complement of a small set?

a= ['a','b','c']
b= ['b']
c= a-b #set theoretic difference, a\b, a.~b, ['a','c']

Steve.

"Zentrader" <ze********@gma il.comwrote in message
news:11******** ************@l2 2g2000prc.googl egroups.com...
Does page count change? i.e. is it necessary to retrieve it in every
loop or
tempList = ['1','2','3','4' ,'5','6','7','8 ']
sampleList=[]
page_count = self.parent.Get PageCount()
<snipped>
for i in range(page_coun t):

Also, once pagefound is set to True, all pages following will not be
appended to sampleList because it is not initialized to False under
the "for i in range(self.pare nt.GetPageCount ())" loop.

Finally, if I understand the logic and question correctly, you want
something like
tempList = ['1','2','3','4' ,'5','6','7','8 ']
sampleList=[]
page_count = self.parent.Get PageCount()
for port in tempList:
for i in range(page_coun t):
page=self.paren t.GetPage(i)
if (hasattr(page, "port")) and (page.port != int(port)) :
sampleList.appe nd(port)

or perhaps (I'm not sure)
tempList = [1, 2, 3, 4, 5, 6, 7, 8]
sampleList=[]
page_count = self.parent.Get PageCount()
for i in range(page_coun t):
page=self.paren t.GetPage(i)
if (hasattr(page, "port")) and (page.port not in tempList) :
sampleList.appe nd(port)
HTH

Aug 24 '07 #4
En Thu, 23 Aug 2007 23:54:14 -0300, bambam <da***@asdf.asd fescribi�:
After examining your suggestion, I realised that another thing
I am interested in could be generalised: I want the complement
of the set of ports in pages, given a universal set in tempList.
Ignoring the break condition for the moment, and my problem
with int(port)/str(port), would you have offered a different solution
if I had asked for the relative complement of a small set?

a= ['a','b','c']
b= ['b']
c= a-b #set theoretic difference, a\b, a.~b, ['a','c']
If you want a set - just use a set:

pya = set(['a','b','c'])
pyb = set(['b'])
pyc = a-b
pyc
set(['a', 'c'])

--
Gabriel Genellina

Aug 24 '07 #5
Wos! Several different thoughts:

An object using yield to return only the relevant pages, one at a time.
Pop to remove the items from the list.
A dictionary to map between the strings and the integers.

The dictionary was particularly unexpected. Eventually, I
plan to change the string ports to device names. On the other
hand, it looks like the only reason I have port numbers is
to use as an index in things like this.

After examining your suggestion, I realised that another thing
I am interested in could be generalised: I want the complement
of the set of ports in pages, given a universal set in tempList.
Ignoring the break condition for the moment, and my problem
with int(port)/str(port), would you have offered a different solution
if I had asked for the relative complement of a small set?

a= ['a','b','c']
b= ['b']
c= a-b #set theoretic difference, a\b, a.~b, ['a','c']

Steve.

"Scott David Daniels" <da*****@dsl-only.netwrote in message
news:13******** *****@corp.supe rnews.com...
bambam wrote:
>Would someone like to suggest a replacement for this? It works ok,
but it doesn't look like any of the other code:

tempList = ['1','2','3','4' ,'5','6','7','8 ']
sampleList=[]
for port in tempList:
pagefound = False
for i in range(self.pare nt.GetPageCount ()):
page=self.paren t.GetPage(i)
if hasattr(page, "port"):
if page.port == int(port):
pagefound=True
if not pagefound:
sampleList.appe nd(port)

Thanks!

Look at good questions. This is a _very_ underspecified question.

One stab at mindreading:

def ported_pages(se lf):
for i in range(self.pare nt.GetPageCount ()):
if hasattr(page, 'port'):
yield page

...
tempList = ['1','2','3','4' ,'5','6','7','8 ']
missing = dict((int(v), v) for v in tempList)
for page in self.ported_pag es():
if page.port in missing:
missing.pop(pag e.port)
if not missing:
break
sampleList = missing.values( )
...

-Scott David Daniels

Aug 24 '07 #6
Excellent. By symmetry, I see that "list" casts the set back into a list.

I wonder why list has not been extended with the same (difference,
interesection) methods? Casting to set looks a little kludgy:

c = list(set(a)-set(b))

I wonder if that is clearer than the explicit loop?

Steve.

"Gabriel Genellina" <ga*******@yaho o.com.arwrote in message
news:ma******** *************** *************** @python.org...
En Thu, 23 Aug 2007 23:54:14 -0300, bambam <da***@asdf.asd fescribi?:
>After examining your suggestion, I realised that another thing
I am interested in could be generalised: I want the complement
of the set of ports in pages, given a universal set in tempList.
Ignoring the break condition for the moment, and my problem
with int(port)/str(port), would you have offered a different solution
if I had asked for the relative complement of a small set?

a= ['a','b','c']
b= ['b']
c= a-b #set theoretic difference, a\b, a.~b, ['a','c']

If you want a set - just use a set:

pya = set(['a','b','c'])
pyb = set(['b'])
pyc = a-b
pyc
set(['a', 'c'])

--
Gabriel Genellina

Aug 24 '07 #7
bambam wrote:
Excellent. By symmetry, I see that "list" casts the set back into a list.

I wonder why list has not been extended with the same (difference,
interesection) methods? Casting to set looks a little kludgy:

c = list(set(a)-set(b))
I wonder if that is clearer than the explicit loop?
This isn't a "cast" in the sense of some less-strongly-typed languages;
it's just a conversion. The `list` function/type iterates over its
argument and turns it into a list. Sets are iterable, so that's all
that's really going on here.

The reason that lists don't have set-like methods is because lists
aren't sets -- lists can contain duplicate elements, whereas sets
cannot. You should use the proper type for your needs; if you want to
take two lists, remove duplicate elements, and then end up with a list,
then the sets-difference-and-then-make-a-list mechanism is appropriate.

--
Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM, Y!M erikmaxfrancis
It [freedom] must be demanded by the oppressed.
-- Dr. Martin Luther King, Jr.
Aug 24 '07 #8
This isn't a "cast" in the sense of some less-strongly-typed languages;
it's just a conversion. The `list` function/type iterates over its
argument and turns it into a list. Sets are iterable, so that's all
that's really going on here.
oh :~). Learning experience happening here... Thank you.
The reason that lists don't have set-like methods is because
lists aren't sets -- lists can contain duplicate elements
Interesting point -- if that's all there is in it, then lists should
have difference and intersection methods. Not because they
are the same as sets -- because they are slightly different than
sets. In this case it doesn't matter - my lists don't contain
duplicate elements this time - but I have worked with lists in
money market and in inventory, and finding the intersection
and difference for matching off and netting out are standard
operations.
Still, any built in feature would probably be too simple to
use in any but the simplest cases.

Steve.

"Erik Max Francis" <ma*@alcyone.co mwrote in message
news:Cq******** *************** *******@speakea sy.net...
bambam wrote:
>Excellent. By symmetry, I see that "list" casts the set back into a list.

I wonder why list has not been extended with the same (difference,
interesectio n) methods? Casting to set looks a little kludgy:

c = list(set(a)-set(b))
I wonder if that is clearer than the explicit loop?

This isn't a "cast" in the sense of some less-strongly-typed languages;
it's just a conversion. The `list` function/type iterates over its
argument and turns it into a list. Sets are iterable, so that's all
that's really going on here.

The reason that lists don't have set-like methods is because lists aren't
sets -- lists can contain duplicate elements, whereas sets cannot. You
should use the proper type for your needs; if you want to take two lists,
remove duplicate elements, and then end up with a list, then the
sets-difference-and-then-make-a-list mechanism is appropriate.

--
Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM, Y!M erikmaxfrancis
It [freedom] must be demanded by the oppressed.
-- Dr. Martin Luther King, Jr.

Aug 24 '07 #9


bambam wrote:
>
In this case it doesn't matter - my lists don't contain
duplicate elements this time - but I have worked with lists in
money market and in inventory, and finding the intersection
and difference for matching off and netting out are standard
operations.
I would use a list comprehension for that case:

A = ['a','b','c','a' ,'c','d']
U = ['a','b','e']
B = [x for x in A if x in U]

The result would be B==['a','b','a']

/MiO
Aug 24 '07 #10

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

Similar topics

3
2857
by: Art | last post by:
NEWBIE ALERT! Esteemed List Participants and Lurkers: (System: P-II 350, 192 meg, Win98 SE, Python 2.2.3, wxPythonWIN32-2.4.1.2-Py22.exe) I'm having a lot of fun getting started with Python ... it is the most elegant and graceful language I have ever used (Fortran, Cobol, Basic, many assemblers, Forth, C, VB, etc.). I don't have the...
1
1571
by: sarmin | last post by:
Hi Gurus... I am using pythonwin to write the programming code and boa constructor to build the GUI... the GUI and the python code work fine when run through pythonwin platform... i m now trying to create an executable python code that can be used on computers that do not have python software installed... py2exe is the definitely the one...
6
3079
by: Atip Asvanund | last post by:
Dear sirs, I am trying to learn how to use Boehm's garbage collector: http://www.hpl.hp.com/personal/Hans_Boehm/gc/ on a Linux machine. I am a beginner, and I find its documentation inadequate. I have followed all instructions for installation, yet I still have the following questions. 1) The instruction said to include the header file,...
6
1560
by: Alex | last post by:
Hello I am intersting in developing and my background is VBA used in Excel and a brief intro to Java. I am interested in learning beyond VB and feel that C++ would be a very good language to get to know and give a lot of flexibility (if not tough to learn). I would like some advice on the merits of learning C++ versus C# or Visual...
6
2987
by: Qun Cao | last post by:
Hi Everyone, I am a beginner on cross language development. My problem at hand is to build a python interface for a C++ application built on top of a 3D game engine. The purpose of this python interface is providing a convenient scripting toolkit for the application. One example is that a user can write a python script like: player =...
4
1154
by: Steven W. Orr | last post by:
I understand that two leading underscores in a class attribute make the attribute private. But I often see things that are coded up with one underscore. Unless I'm missing something, there's a idiom going on here. Why do people sometimes use one leading underscore? TIA -- Time flies like the wind. Fruit flies like a banana. Stranger...
2
1166
by: Steven W. Orr | last post by:
On Friday, Feb 23rd 2007 at 11:12 -0500, quoth Steven W. Orr: =>I understand that two leading underscores in a class attribute make the =>attribute private. But I often see things that are coded up with one =>underscore. Unless I'm missing something, there's a idiom going on here. => =>Why do people sometimes use one leading underscore? ...
11
1190
by: bambam | last post by:
Would someone like to suggest a replacement for this? This is a function that returns different kinds of similar objects, depending on what is asked for. PSP and PWR are classes. I don't really want to re-write the calling code very much: I'm just wondering if the function can be replaced with some kind of OOP pattern. def...
3
1564
by: James Fassett | last post by:
Hi all, Simple question really on a best practice. I want to avoid adding duplicates to a list. my_list = dup_map = {} for item in my_list: dup_map = True
0
7453
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7390
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...
0
7647
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. ...
0
7802
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...
0
7742
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...
1
5318
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...
0
4940
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...
1
1007
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
692
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...

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.