简体   繁体   中英

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 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.

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).

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 .

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.

@Green Cloak Guy have already stated the answer, but it can be improvised usinglist comprehension . It also helps in usage of slightly less memory than that of list , less usage of lines and also less time consumption. I would suggest using array by numpy , but let's leave it.

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. If you want that output, you can do, just what @Green Cloak Guy did, but still here's an example:

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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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