我想通过循环定义(创建)并使用不同的变量(使用后缀)(特别是循环)

发布于 2025-01-23 15:14:08 字数 674 浏览 0 评论 0原文

我想创建通过循环的多变量以进一步使用和比较程序。

这是代码 -

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

悲喜皆因你 2025-01-30 15:14:08

我强烈建议您检查词典。
,我强烈建议您检查词典。
字典允许您使用关联的键存储变量,因此:

variable_dict = dict()
for i in range(0,len(Header_list)):
  variable_dict[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(variable_dict[f"len_{i}"]) :
        variable_dict[f'len_{i}'] = len(str(company[i]))
        print(f"len_{i}")

这允许您使用相同的键访问值:

len_of_4 = variable_dict['len_4']

如果您确实需要动态创建变量,则可以使用exec函数来作为Python代码运行字符串。重要的是要注意,exec函数在Python中不安全,可以用来运行任何潜在的恶意代码:

for i in range(0,len(Header_list)):
  exec(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 exec(f"len(str(company[i])) > len(len_{i})"):
        exec(f'len_{i} = len(str(company[i]))')
        print(f"len_{i}")

Instead of dynamically creating variables, I would HIGHLY recommend checking out dictionaries.
Dictionaries allow you to store variables with an associated key, as so:

variable_dict = dict()
for i in range(0,len(Header_list)):
  variable_dict[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(variable_dict[f"len_{i}"]) :
        variable_dict[f'len_{i}'] = len(str(company[i]))
        print(f"len_{i}")

This allows you to access the values using the same key:

len_of_4 = variable_dict['len_4']

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 the exec function is not safe in python, and could be used to run any potentially malicious code:

for i in range(0,len(Header_list)):
  exec(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 exec(f"len(str(company[i])) > len(len_{i})"):
        exec(f'len_{i} = len(str(company[i]))')
        print(f"len_{i}")
浅笑依然 2025-01-30 15:14:08

在python中,一切都是对象,所以将当前模块用作对象并这样使用

import sys
module = sys.modules[__name__]
Header_list=[0,1,2,3,4]
len_ = len(Header_list)
for i in range(len_):
  setattr(module, f"len_{i}", Header_list[i]+2)

print(len_0)
print(len_1)

In python everything is object so use current module as object and use it like this

import sys
module = sys.modules[__name__]
Header_list=[0,1,2,3,4]
len_ = len(Header_list)
for i in range(len_):
  setattr(module, f"len_{i}", Header_list[i]+2)

print(len_0)
print(len_1)
请止步禁区 2025-01-30 15:14:08
Header_list=[0,1,2,3,4]
for i in range(0,5):
  exec(f'len_{i} = {Header_list[i] + 2 }')
  print(f'len{i}') 

输出:

len0
len1
len2
len3
len4
Header_list=[0,1,2,3,4]
for i in range(0,5):
  exec(f'len_{i} = {Header_list[i] + 2 }')
  print(f'len{i}') 

output:

len0
len1
len2
len3
len4
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文