初学者,惯用的蟒蛇 [英] beginner, idiomatic python

查看:121
本文介绍了初学者,惯用的蟒蛇的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人想建议更换吗?它工作正常,

但它看起来不像其他任何代码:


tempList = [''1'',''2 '',''''',''4'',''''',''''''''''''''''''''''' ]

for tempList中的端口:

pagefound = False

for i in range(self.parent.GetPageCount()):

page = self.parent.GetPage(i)

if hasattr(page," port"):

if page.port == int(端口):

pagefound = True

如果不是pagefound:

sampleList.append(端口)


谢谢!

解决方案

bambam写道:


有人想建议一个替换这个?它工作正常,

但它看起来不像其他任何代码:


tempList = [''1'',''2 '',''''',''4'',''''',''''''''''''''''''''''' ]

for tempList中的端口:

pagefound = False

for i in range(self.parent.GetPageCount()):

page = self.parent.GetPage(i)

if hasattr(page," port"):

if page.port == int(端口):

pagefound = True

如果不是pagefound:

sampleList.append(端口)


谢谢!



看看好问题。这是一个非常缺乏的问题。


一次刺激思维:


def ported_pa​​ges(个体经营):

我在范围内(self.parent.GetPageCount()):

if hasattr(page,''port''):

yield页面


...

tempList = [''''',''''',''3'',''4'',''5'', ''6'',''7'',''8'']

缺少= dict((int(v),v)for temp in tempList)

为self.ported_pa​​ges()中的页面:

如果缺少page.port:

missing.pop(page.port)

if不要错过:

休息

sampleList = missing.values()

...


-Scott David Daniels


页数是否有变化?即是否有必要在每个

循环中检索它或

tempList = [''1'',''2'',''3'''' 4'','''',''''',''''',''8'']

sampleList = []

page_count = self.parent.GetPageCount()

< snipped>

for i in range(page_count):


此外,一次pagefound设置为True,后面的所有页面都不会附加到sampleList,因为它没有在

下初始化为Falsefor i in range(self.parent) .GetPageCount())"循环。


最后,如果我理解逻辑和问题,你想要

类似

tempList = ['' 1'',''''',''''',''''',''''''''''''''''''''''''''''''''
sampleList = []

page_count = self.parent.GetPageCount()

for tempList中的端口:

for i in range (page_count):

page = self.parent.GetPage(i)

if(hasattr(page," port"))和(page.port!= int (端口)):

sampleList.append(端口)


或者(我不确定)

tempList = [1,2,3,4,5,6,7,8]

sampleList = []

page_count = self.parent.GetPageCount()

for i in range(page_count):

page = self.parent.GetPage(i)

if(hasattr(page," port") )和(page.port不在tempList中):

sampleList.append(端口)

HTH


WOS!几种不同的想法:


一个使用yield的对象,一次只返回一个相关页面。

弹出从列表中删除项目。 br />
字符串和整数之间的字典。


字典特别出乎意料。最终,我计划将字符串端口更改为设备名称。在另一个

手上,看起来我有端口号的唯一原因是

在这样的事情中用作索引。


在检查了你的建议之后,我意识到我感兴趣的另一件事

可能会被推广:我想要页面中端口集的补充

,在tempList中给出一个通用集。

暂时忽略中断条件,我的问题

与int(port)/ str(port),你会提供一个不同的解决方案

如果我要求一小套的相对补充?


a = [''a'',''b'',' 'c'']

b = [''b'']

c = ab #set理论差异,a \ b,a。~b,[''a '',''c'']


史蒂夫。


" Zentrader" < ze ******** @ gmail.com写信息

新闻:11 ******************** @ l22g2000prc .googlegrou ps.com ...


页数是否会发生变化?即是否有必要在每个

循环中检索它或

tempList = [''1'',''2'',''3'''' 4'','''',''''',''''',''8'']

sampleList = []

page_count = self.parent.GetPageCount()

< snipped>

for i in range(page_count):


此外,一次pagefound设置为True,后面的所有页面都不会附加到sampleList,因为它没有在

下初始化为Falsefor i in range(self.parent) .GetPageCount())"循环。


最后,如果我理解逻辑和问题,你想要

类似

tempList = ['' 1'',''''',''''',''''',''''''''''''''''''''''''''''''''
sampleList = []

page_count = self.parent.GetPageCount()

for tempList中的端口:

for i in range (page_count):

page = self.parent.GetPage(i)

if(hasattr(page," port"))和(page.port!= int (端口)):

sampleList.append(端口)


或者(我不确定)

tempList = [1,2,3,4,5,6,7,8]

sampleList = []

page_count = self.parent.GetPageCount()

for i in range(page_count):

page = self.parent.GetPage(i)

if(hasattr(page," port") )和(page.port不在tempList中):

sampleList.append(port)

HTH


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.parent.GetPageCount()):
page=self.parent.GetPage(i)
if hasattr(page, "port"):
if page.port == int(port):
pagefound=True
if not pagefound:
sampleList.append(port)

Thanks!

解决方案

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.parent.GetPageCount()):
page=self.parent.GetPage(i)
if hasattr(page, "port"):
if page.port == int(port):
pagefound=True
if not pagefound:
sampleList.append(port)

Thanks!

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

One stab at mindreading:

def ported_pages(self):
for i in range(self.parent.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_pages():
if page.port in missing:
missing.pop(page.port)
if not missing:
break
sampleList = missing.values()
...

-Scott David Daniels


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.GetPageCount()
<snipped>
for i in range(page_count):

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.parent.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.GetPageCount()
for port in tempList:
for i in range(page_count):
page=self.parent.GetPage(i)
if (hasattr(page, "port")) and (page.port != int(port)) :
sampleList.append(port)

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


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********@gmail.comwrote in message
news:11********************@l22g2000prc.googlegrou ps.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.GetPageCount()
<snipped>
for i in range(page_count):

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.parent.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.GetPageCount()
for port in tempList:
for i in range(page_count):
page=self.parent.GetPage(i)
if (hasattr(page, "port")) and (page.port != int(port)) :
sampleList.append(port)

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



这篇关于初学者,惯用的蟒蛇的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆