简体   繁体   English

python 3.x中的列表理解/列表创建循环

[英]List comprehension/list creating loop in python 3.x

I've been trying to make a loop or list comprension thingy that does the following: 我一直在试图做一个循环或列表强制,它可以执行以下操作:

prints "Please give me the number of bread sold at ", self.bakeryname[k], ":" then prompts the user for the number of break sold at the given bakery, and stores it in a list 打印"Please give me the number of bread sold at ", self.bakeryname[k], ":"然后提示用户输入在给定面包店出售的"Please give me the number of bread sold at ", self.bakeryname[k], ":"数量,并将其存储在列表中

"Please give me the number of [breadtype] sold at:"
"The hungry baron": [here you enter a number]
"The flying bulgarian": [here you enter another number]

It's to be stored in a list of integers that starts at the first prompted value and ends at the last -||-. 它要存储在一个整数列表中,该整数列表从第一个提示值开始,到最后一个-||-结尾。

The number of bakeries is potentially infinite, there's only 3 different types of bread. 面包店的数量可能是无限的,只有3种不同类型的面包。

I've dug myself into the hole that is this function: 我将自己挖入了这个功能的孔中:

def soldbread(self):
    amount = ((len(self.bakeryname))*3)
    k = 0
    j = 0
    h = 0
    i = 0
    while j < (len(self.breadtype)):
            print("Please give me the number of",self.breadtype[k], "sold at:")
            while i < amount:
                self.breadsold.append(i)
                self.breadsold[i] = int(input(self.bakeryname[h]))
                j += 1
                h += 1
                i += 1
                if k == 3:
                    break
                else:
                    while j >= len(self.bakeryname):
                        k += 1
                        print("Please give me the number of",self.breadtype[k], "soldat:")
                        j = 0
                        h = 0

The function will go to the 15'th type of bread (there's 5 bakeries in self.bakeryname >at the moment<, so atleast that number is accurate) then it will complain about "IndexError: list index out of range". 该函数将转到第15种面包(self.bakeryname中目前有5个面包店,<现在,因此该数字至少是准确的),然后它将抱怨“ IndexError:列表索引超出范围”。 I've tried a bunch of "if"'s and "breaks"'s but I can't pull it off. 我已经尝试了很多“如果”和“休息”的情况,但是我无法实现。

The names etc. in the code is translated from my native language, so eventual typos are likely to not be in the code. 代码中的名称等是从我的母语翻译过来的,因此最终的错别字很可能不在代码中。

bakeries = ['a','b','c']
breadtypes = ['flat','rye','white']

results = []
for i in bakeries:
   print('How many of each type of bread for {0}:'.format(i))
   number_of_types = []
   for bread in breadtypes:
       number_of_types.append(input('{0}:'.format(bread)))
   results.append(number_of_types)

for k,v in enumerate(bakeries):
   print('Here are the different types of breads for {0}'.format(v))
   print(''.join('{0}:{1}'.format(a,b) for a,b in zip(breadtypes, results[k])))
# Your data
bakeries = ['a','b','c']
breadtypes = ['flat','rye','white']
# Output data
results = {}

for bakery in bakeries:
    # A line for each bakery
    print('How many of each type of bread for %s: ' % bakery)

    # The python3 dict comprehension and a generator
    results[bakery] = {
        breadtype: breadtypevalue for breadtype, breadtypevalue
        in ((bt, input('%s: ' % bt)) for bt in breadtypes)
    }

# Wanna print that?
for bakery in results:
    print('Here are the different types of breads for %s:' % bakery)
    print(', '.join(['%s: %s' % (breadtype, number) for breadtype, number in results[bakery].items()]))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM