【问题标题】:What is the while loop equivalent to this for loop?与此 for 循环等效的 while 循环是什么?
【发布时间】:2022-01-12 00:26:30
【问题描述】:

我想将我代码中的两个for 循环都转换为while 循环。这是代码。

class CountryCatalogue:
    def __init__(self, countryFile):  # constructor method that takes name of file
        self.countryCat = []  # The instance variable country cat needed
        file = open(countryFile, "r")  # open and read the file
        for strip in file:
            strip = strip.strip()
            data = strip.split('|')  # Remove '|' in each line
            clean_data = Country(data[0], data[2], data[3], data[1])  # Create an object to country
            self.countryCat.append(clean_data)  # Append the item to the list
        file.close()  # Close the file

    def setPopulationOfCountry(self, country, population):
        for pop in range(len(self.countryCat)):  # iterate through countryCat
            if self.countryCat[pop].getName() == country.getName():
                self.countryCat[pop].setPopulation(population)  # updating data if needed

我尝试以这种方式更改第二个 for 循环:

def setPopulationOfCountry(self, country, population):
    pop = 0
    while pop < (len(self.countryCat)):
        if self.countryCat[pop].getName() == country.getName():
            self.countryCat[pop].setPopulation(population)  # updating data if needed
            pop = pop + 1

但它不起作用,我不确定第一个 for 循环。

【问题讨论】:

  • 但是为什么?另外,为什么countryCat 不是以国家/地区名称为键的字典,以便您可以按名称查找它们没有循环?
  • 因为我向我的朋友展示了我的代码并且我害怕他会复制它,所以我想将我所有的 for 循环更改为 while 循环。 CountryCat 不是 dict 键控,因为用户之前输入了文件并且程序不知道将输入哪个文件,它只是创建一个空列表,然后用文件数据填充它。

标签: python loops for-loop while-loop


【解决方案1】:

我发现将文件 for 循环更改为 while 循环的最简单方法是遍历每一行:

file = open(countryFile, "r")  # open and read the file
strip = file.readline()
while strip != "":
    strip = file.readline()

【讨论】:

  • 这是我更改代码的方式。我得到一个 IndexError: list index out of range
  • class CountryCatalogue: def __init__(self, countryFile): self.countryCat = [] # 实例变量 country cat 需要 file = open(countryFile, "r") # 打开并读取文件 strip = file.readline() while strip != "": strip = file.readline() data = strip.split('|') # 去掉'|'在每一行中 clean_data = Country(data[0], data[2], data[3], data[1]) # 为国家创建一个对象 self.countryCat.append(clean_data) # 将项目附加到列表文件中。 close() # 关闭文件
  • strip = file.readline() 应该是 while 循环中的第一行,而不是第一行
  • 当我尝试评论我的代码时,一切都搞砸了。你能告诉我它对“Country(data[0], data[2], data[3], data[1])”和“elf.countryCat.append(clean_data)”等所有其他内容的要求吗?再次非常感谢你
【解决方案2】:

你的 while 循环:

def setPopulationOfCountry(self, country, population):
    pop = 0
    while pop < (len(self.countryCat)):
        if self.countryCat[pop].getName() == country.getName():
            self.countryCat[pop].setPopulation(population)  # updating data if needed
            pop = pop + 1

你把 pop = pop + 1 放在 if 语句中,把它拿出来:

def setPopulationOfCountry(self, country, population):
    pop = 0
    while pop < (len(self.countryCat)):
        if self.countryCat[pop].getName() == country.getName():
            self.countryCat[pop].setPopulation(population)  # updating data if needed
        pop = pop + 1

【讨论】:

  • 非常感谢。我不知道为什么我没有意识到这一点。您如何看待第一个 for 循环?
  • 在for循环中,你一行一行地走?
  • 如何将构造函数方法中的第一个 for 循环“for strip in file:”转换为 while 循环。我尝试了一些方法,但都没有奏效。
  • 我问,for循环中的strip是文件中的一行吗?
  • 否,该条带不在文件中。我本来可以用 x 代替
猜你喜欢
  • 1970-01-01
  • 2019-06-03
  • 1970-01-01
  • 2015-04-17
  • 2023-03-07
  • 1970-01-01
  • 2014-03-23
  • 2020-06-19
  • 1970-01-01
相关资源
最近更新 更多