[英]For loop inside while loop not working in Python
I am facing very unusual problem, below is code inside a class where pitnamebasename
is 2d list. 我面临一个非常不寻常的问题,下面是
pitnamebasename
是2d列表的类中的代码。
For example:= 例如:=
pitnamebasename= [['a','b'],['n','m'],['b','c'],['m','f'],['c','d'],['d',''],['h','f']]
Above list is not necessary to be in any order like ['d','']
can be at 0th order. 上面的列表不需要以
['d','']
任何顺序排列['d','']
可以以0的顺序排列。
Here is my function (inside a class):- 这是我的功能(在类中):
def getRole(self,pitname):
basename=pitname
print '\n\n\nbasename=',basename
ind=True
while pitname and ind:
ind=False
basename=pitname
print 'basename=',basename
for i in self.pitnamebasename:
print 'comparing-',i[0],'and',pitname
if i[0] == pitname:
pitname=i[1]
print 'newpitname=',pitname
ind=True
break
print 'returning-',basename
return basename
pitname
is the string for example here it can be 'a'. pitname
是字符串,例如,此处可以是“ a”。 I want return value to be 'd' mean the traversing must be like a to b, b to c and d to None, hence return value must be d. 我希望返回值是“ d”,意味着遍历必须像a到b,b到c以及d到None一样,因此返回值必须是d。 Please don't suggest me any other methods to solve.
请不要建议我其他解决方法。
Now my problem is that in the for loop its not looping till last but getting out in middle. 现在我的问题是,在for循环中,它直到最后都没有循环,而是从中间退出。 Like return value is either b or c or even d depends on what I am searching.
就像返回值是b还是c甚至d取决于我要搜索的内容。 Actually list is very very long.
实际上列表很长。 Strange thing I noted that for loop loops only to that index where it loops till its first time.
奇怪的是,我注意到for循环仅循环到该索引直到第一次循环。 Like here first time for loop gets end when it find 'a' and
pitname
becomes 'b' but when it search for 'b' it loops till it find 'a' only. 像这里一样,第一次for循环在找到'a'并且
pitname
变成'b'时结束,但是在搜索'b'时循环直到找到'a'。 Does anyone knows how it is happening? 有谁知道这是怎么回事?
pitnamebasename= [['a','b'],['n','m'],['b','c'],['m','f'],['c','d'],['d',''],['h','f']]
First, change your '2d' array into a dict
: 首先,将您的“ 2d”数组更改为
dict
:
pitnamebasename = dict(pitnamebasename)
Now, it should be a simple matter of walking from element to element, using the value associated with the current key as the next key, until the value is the empty string; 现在,只需将与当前键关联的值用作下一个键,从一个元素到另一个元素,直到该值为空字符串为止,这很简单。 then return the current key.
然后返回当前密钥。 If
pitname
ever fails to exist as a key, it's treated as if it does exist and maps to the empty string. 如果
pitname
曾经pitname
作为键存在,则将其视为确实存在并映射到空字符串。
def getRole(self, pitname):
while pitnamebasename.get('pitname', '') != '':
pitname = pitnamebasename[pitname]
return pitname
A defaultdict
could also be used in this case: 在这种情况下,也可以使用
defaultdict
:
import collections.defaultdict
pitnamebasename = defaultdict(str, pitnamebasename)
def getRole(self, pitname):
while pitnamebasename[pitname] != '':
pitname = pitnamebasename[pitname]
return pitname
You asked for the solution to your problem but I am having trouble replicating the problem. 您要求问题的解决方案,但我在复制问题上遇到困难。 This code does the same thing without requiring you to change your entire class storage system.
该代码执行相同的操作,而无需您更改整个类存储系统。
By converting your list of lists into a dictionary lookup (looks like the following) 通过将列表列表转换为字典查找(如下所示)
as_list_of_lists = [['a','b'],['n','m'],['b','c'],['m','f'],['c','d'],['d',''],['h','f']]
as_dict = dict(as_list_of_lists)
# as_dict = {'a': 'b', 'c': 'd', 'b': 'c', 'd': '', 'h': 'f', 'm': 'f', 'n': 'm'}
we can do a default dictionary lookup using the dictionary method .get. 我们可以使用字典方法.get进行默认的字典查找。 This will look for an item (say we pass it 'a') and return the associated value ('b').
这将查找一个项目(例如,我们将其传递给“ a”)并返回关联的值(“ b”)。 If we look for something that isn't in the dictionary, .get will return the second value (a default value) which we can supply as ''.
如果我们查找字典中没有的内容,.get将返回第二个值(默认值),我们可以将其提供为“”。 Hence as_dict.get('z','') will return ''
因此as_dict.get('z','')将返回''
class this_class
def __init__(self):
self.pitnamebasename= [['a','b'],['n','m'],['b','c'],['m','f'],['c','d'],['d',''],['h','f']]
def getRole(self,pitname):
lookup = dict(self.pitnamebasename)
new_pitname = pitname
while new_pitname != '':
pitname = new_pitname
new_pitname = lookup.get(pitname, '')
return pitname
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.