简体   繁体   English

Python:从for循环返回列表

[英]Python: return list from for loop

so I have所以我有

packs_purchased = (10, 6, 36)
dice_purchased = (2, 0, 1)
board_games_purchased = (3, 2, 0)

and

prices = (4.25, 125, 11, 50)

How do I get this for loop to return a list and not just the output我如何让这个 for 循环返回一个列表而不仅仅是输出

for packs_purchased in packs_purchased:
    if packs_purchased < 35:
        packs_spent = packs_purchased * prices[0]
    else:
        packs_spent = (int(math.floor(packs_purchased / 36)) * prices[1]) + ((packs_purchased % 36) * prices[0])
    print("%.2f" % packs_spent)

right now it prints现在它打印

42.50
25.50
125.00

and I want it to return a list, something like我希望它返回一个列表,比如

packs_spent(42.50, 25.50, 125.00)

Simply create a list prior to the for loop, and then append to that list instead of printing.只需在for循环之前创建一个列表,然后附加到该列表而不是打印。

packs_spent = []
for pack in packs_purchased:
    if pack < 35:
        spent = pack * prices[0]
    else:
        spent = (int(math.floor(pack / 36)) * prices[1]) + ((pack % 36) * prices[0])
    packs_spent.append(spent)

print(packs_spent)
# [42.5, 25.5, 125.0]

To print with the proper number of decimal places, and in the format shown in the question, you can use a list comprehension:要使用正确的小数位数并以问题中显示的格式打印,您可以使用列表理解:

print("packs_spent(", 
      ', '.join(f"{i:.2f}" for i in packs_spent),
      ")",
      sep="")
# packs_spent(42.50, 25.50, 125.00)

Just declare an empty list and append results into that list.只需声明一个空列表并将结果附加到该列表中。

packs_list = []    
for packs_purchased in packs_purchased:
   if packs_purchased < 35:
        packs_spent = packs_purchased * prices[0]
   else:
        packs_spent = (int(math.floor(packs_purchased / 36)) * prices[1]) + ((packs_purchased % 36) * prices[0])
packs_list.append(packs_spent)
print(packs_list)

There are a few silly mistakes in your Python code as well as some styling error (but we are not going there, as it's not that important).您的 Python 代码中有一些愚蠢的错误以及一些样式错误(但我们不会去那里,因为它并不重要)。

The mistake includes, you are using same variable for iteration variable as well as for iterable , it can create major confusion when using the iterable for future use, and you might get int instead of tuple .错误包括,您对迭代变量和iterable使用相同的variable ,在使用iterable以供将来使用时可能会造成重大混淆,并且您可能会得到int而不是tuple

Also, side note , list comprehension has variable in local scope instead of global , so it will not create confusion, but still I recommend changing the iteration varable name.另外,旁注list comprehensionlocal范围内具有变量而不是global ,因此它不会造成混淆,但我仍然建议更改迭代变量名称。

@Green Cloak Guy have already stated the answer, but it can be improvised usinglist comprehension . @Green Cloak Guy 已经给出了答案,但可以使用列表理解即兴创作。 It also helps in usage of slightly less memory than that of list , less usage of lines and also less time consumption.它还有助于使用比list略少的内存,更少的行使用和更少的时间消耗。 I would suggest using array by numpy , but let's leave it.我建议使用array by numpy ,但让我们离开它。

print([f'{packs * prices[0]:.2f}' if packs < 35 else f'{(int(math.floor(packs / 36)) * prices[1]) + ((packs % 36) * prices[0]):.2f}' for packs in packs_purchased])

OUTPUT:输出:

>>> [42.50, 25.50, 125.00]

The output above is called list and not what you have stated in your desired output.上面的输出称为list而不是您在所需输出中声明的内容。 If you want that output, you can do, just what @Green Cloak Guy did, but still here's an example:如果你想要那个输出,你可以做,就像@Green Cloak Guy 所做的那样,但仍然是一个例子:

CREATED_LIST = [f'{packs * prices[0]:.2f}' if packs < 35 else f'{(int(math.floor(packs / 36)) * prices[1]) + ((packs % 36) * prices[0]):.2f}' for packs in packs_purchased]
print(f'packs_spent({", ".join(CREATED_LIST)})')

OUTPUT:输出:

>>> packs_spent(42.50, 25.50, 125.00)
import math
packs_purchased = (10, 6, 36)
dice_purchased = (2, 0, 1)
board_games_purchased = (3, 2, 0)
prices = (4.25, 125, 11, 50)

result = []
for packs_purchased in packs_purchased:
    if packs_purchased < 35:
        packs_spent = packs_purchased * prices[0]
    else:
        packs_spent = (int(math.floor(packs_purchased / 36)) * prices[1]) + ((packs_purchased % 36) * prices[0])
    a = ("%.2f" % packs_spent)
    result.append(a)
    
result = list(map(float, result))
print("packs_spent:", (result))

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

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