Python:使用for循环更改变量后缀 [英] Python: Change variable suffix with for loop

查看:111
本文介绍了Python:使用for循环更改变量后缀的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题经常被问到,但是到目前为止我还无法理解/理解答案.

I know this was asked a lot but I can not work with/understand the answers so far.

我想在for循环中更改变量的后缀.

I want to change the suffix of variables in a for loop.

我尝试了stackoverflow搜索提供的所有答案.但是很难理解提问者经常提出的特定代码.

I tried all answers the stackoverflow search provides. But it is difficult to understand specific codes the questioner often presents.

为澄清起见,我举一个简单的例子.这并不意味着面向应用程序.我只想了解如何更改后缀.

So for clarification I use an easy example. This is not meant as application-oriented. I just want to understand how I can change the suffix.

var_1 = 10
var_2 = 100
var_3 = 1000

for i in range(1,4):
    test_i = var_i + 1
    print(test_i)

预期结果:创建和打印变量:

Expected result: creating and printing variables:

test_1 = 11 
test_2 = 101 
test_3 = 1001


预期产量

11
101
1001

错误:将 var_i 作为变量名读取,而没有更改 i .

Error: var_i is read as a variable name without the changes for i.

推荐答案

例如,不要使用复杂的命名约定,而是尝试使用诸如字典之类的数据结构来构想您的问题.

Instead of doing a convoluted naming convention, try to conceive of your problem using a data structure like dictionaries, for example.

var={}
var[1] = 10
var[2] = 100
var[3] = 1000

test={}
for i in range(1,4):
    test[i] = var[i] +1

print(test)

如果以某种方式为您提供了 var_1 等作为输入,则可以使用 .split("_")检索索引号并将其用作字典键(它们可以是字符串或值).

If somehow you are given var_1 etc as input, maybe use .split("_") to retrieve the index number and use that as the dictionary keys (they can be strings or values).

有关使用索引变量名称的简短解释.如果您刚开始学习编程,则有很多原因不使用 eval exec getattr 方法.最简单的说,它效率低下,不可扩展,并且很难在脚本中的其他任何地方使用.

Small explanation about using indexing variable names. If you are starting out learning to program, there are many reasons not to use the eval, exec, or getattr methods. Most simply, it is inefficient, not scalable, and is extremely hard to use anywhere else in the script.

如果有更简单的方法可以做某事,我不是坚持最佳实践"的人,但是您需要学习避免这种事情.我们编写程序来避免键入此类内容.

I am not one to insist on "best practices" if there is an easier way to do something, but this is something you will want to learn to avoid. We write programs to avoid having to type things like this.

如果为您提供 var_2 文本作为起点,那么我将使用字符串解析工具进行 split 并将字符串转换为值和变量名.

If you are given that var_2 text as a starting point, then I would use string parsing tools to split and convert the string to values and variable names.

通过使用字典,您可以拥有1000个非连续变量,并简单地遍历它们或分配新的关联.例如,如果您要进行实验,并调用值 tree_1 tree_10 等,那么您将始终被困在代码中键入完整的变量名,而不是键入完整的变量名只需遍历名为 tree 的容器中的所有条目即可.

By using a dictionary, you can have 1000 non-consecutive variables and simply loop through them or assign new associations. If you are doing an experiment, for example, and call your values tree_1, tree_10 etc, then you will always be stuck typing out the full variable names in your code rather than simply looping through all the entries in a container called tree.

这与使用一堆 if:else 语句分配值有些相关:

This is a little related to using a bunch of if:else statements to assign values:

# inefficient way -- avoid
if name == 'var_1' then:
    test_1=11
elif name == 'var_2' then:
    test_2=101

说起来容易得多

test[i]= var[i]+1

并且该行将适用于任意数量的值.

and that one line will work for any number of values.

这篇关于Python:使用for循环更改变量后缀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆