简体   繁体   English

如何生成变量,然后从列表中为它们分配一个整数(python3)

[英]How to generate variables and then assigning an integer to them from a list (python3)

divisions = int(input('Divisions: '))
divisible = []

while divisions > 0:
    x = input('Divisble: ')
    divisible.append(x)
    divisions = divisions - 1

Trying to get the 'divisible' list to split into separate integer variables, but the problem i have is that the amount of variables that houses the integers changes. 试图获取“可分割”列表以拆分为单独的整数变量,但是我遇到的问题是容纳整数的变量数量发生了变化。 How would I solve this? 我该如何解决?

You could use the built-in reduce function 您可以使用内置的reduce功能

import functools

l = [2, 3, 4]
n = 24
result = functools.reduce(lambda a, v: a / v, l, n)

print('result: {}'.format(result))

This basically starts with n, divides, by 2, takes the answer and divides by 3, .. for each value in the list l, so the output is 1. 对于列表l中的每个值,这基本上从n开始,除以2,得到答案,然后除以3,..,因此输出为1。

reduce was built in in python 2; reduce是在python 2中内置的; now it's in the functools module. 现在在functools模块中。

Here 'sa link to see it running interactively at pythontutor.com 是一个链接,可在pythontutor.com上看到它的交互运行

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

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