我想通过循环定义(创建)并使用不同的变量(使用后缀)(特别是循环)
我想创建通过循环的多变量以进一步使用和比较程序。
这是代码 - ,
for i in range(0,len(Header_list)):
(f'len_{i} = {len(Header_list[i]) + 2 }')
print(len_0);print(f'len{i}')
for company in Donar_list:
print(company[i],f"len_{i}")
if len(str(company[i])) > len((f"len_{i}")) :
(f'len_{i}') = len(str(company[i]))
print(f"len_{i}")
但是正在发生的事情,尽管我设法创建了变量 len_0,len_1,len_2 ...在第2行中,在行-3我也可以仅使用print(len_0)打印len_0..ETC
变量我的直觉。我希望它确实会创建变量并在必要时进行进一步比较,在这种情况下为循环。我现在应该做什么?我是一个初学者,我可以使用IF语句做到这一点,但这不会有效,我的意图也不是为此创建任何**数据结构**。
我不知道我能否设法给你我想说的话。无论我只想使用后缀创建不同的变量,并在这种情况下还可以通过循环组成它们。
I want to create multiple variable through for loop to further use and compare in the program.
Here is the code -
for i in range(0,len(Header_list)):
(f'len_{i} = {len(Header_list[i]) + 2 }')
print(len_0);print(f'len{i}')
for company in Donar_list:
print(company[i],f"len_{i}")
if len(str(company[i])) > len((f"len_{i}")) :
(f'len_{i}') = len(str(company[i]))
print(f"len_{i}")
But what is happening, though I managed to create variable len_0,len_1,len_2... in line-2, in line - 3 I also can print len_0..etc variable by only using print(len_0), but I can't print it the values of these by - print(f'len_{i}')
In line 5 I also can't compare with the cofition with my intension. I want it do create variable and compare it further when as it necessary, in this case under for loop.What should I do now? I am a beginner and I can do it using if statement but that wouldn't be efficient, also my intention is not to create any **data structure ** for this.
I don't know whether I could manage to deliver you what I am trying to say. Whatever I just wanna create different variable using suffix and also comprare them through for loop in THIS scenario.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我强烈建议您检查词典。
,我强烈建议您检查词典。
字典允许您使用关联的键存储变量,因此:
这允许您使用相同的键访问值:
如果您确实需要动态创建变量,则可以使用
exec
函数来作为Python代码运行字符串。重要的是要注意,exec
函数在Python中不安全,可以用来运行任何潜在的恶意代码:Instead of dynamically creating variables, I would HIGHLY recommend checking out dictionaries.
Dictionaries allow you to store variables with an associated key, as so:
This allows you to access the values using the same key:
If you REALLY REALLY need to dynamically create variables, you could use the
exec
function to run strings as python code. It's important to note that theexec
function is not safe in python, and could be used to run any potentially malicious code:在python中,一切都是对象,所以将当前模块用作对象并这样使用
In python everything is object so use current module as object and use it like this
输出:
output: