如何在循环中创建不同的变量名?

时间:2021-03-01 23:08:37

For example purposes...

例如目的……

for x in range(0,9):
    string'x' = "Hello"

So I end up with string1, string2, string3... all equaling "Hello"

最后是string1, string2, string3。所有相当于“你好”

6 个解决方案

#1


82  

Sure you can; its called a dictionary:

你当然可以;它叫字典:

d={}
for x in range(1,10):
        d["string{0}".format(x)]="Hello"

In [7]: d["string5"]
Out[7]: 'Hello'

In [8]: d
Out[8]: 
{'string1': 'Hello',
 'string2': 'Hello',
 'string3': 'Hello',
 'string4': 'Hello',
 'string5': 'Hello',
 'string6': 'Hello',
 'string7': 'Hello',
 'string8': 'Hello',
 'string9': 'Hello'}

I said this somewhat tongue in check, but really the best way to associate one value with another value is a dictionary. That is what it was designed for!

我说过这个有点拗口,但是把一个值和另一个值联系起来的最好方法是字典。这就是它的设计初衷!

#2


30  

It is really bad idea, but...

这真是个坏主意,但是……

for x in range(0, 9):
    globals()['string%s' % x] = 'Hello'

and then for example:

然后为例:

print(string3)

will give you:

会给你:

Hello

However this is bad practice. You should use dictionaries or lists instead, as others propose. Unless, of course, you really wanted to know how to do it, but did not want to use it.

然而,这是不好的做法。你应该像其他人建议的那样使用字典或列表。当然,除非你真的想知道怎么做,但又不想用。

#3


8  

It's simply pointless to create variable variable names. Why?

创建变量名是毫无意义的。为什么?

  • They are unnecessary: You can store everything in lists, dictionarys and so on
  • 它们是不必要的:你可以把所有东西都存储在列表、字典等等
  • They are hard to create: You have to use exec or globals()
  • 它们很难创建:您必须使用exec或globals()
  • You can't use them: How do you write code that uses these variables? You have to use exec/globals() again
  • 不能使用它们:如何编写使用这些变量的代码?您必须再次使用exec/globals()

Using a list is much easier:

使用列表要容易得多:

# 8 strings: `Hello String 0, .. ,Hello String 8`
strings = ["Hello String %d" % x for x in range(9)]
for string in strings: # you can loop over them
    print string
print string[6] # or pick any of them

#4


4  

Don't do this use a dictionary

不要用字典

import sys
this = sys.modules[__name__] # this is now your current namespace
for x in range(0,9):
    setattr(this, 'string%s' % x, 'Hello')

print string0
print string1
print string2
print string3
print string4
print string5
print string6
print string7
print string8

don't do this use a dict

不要这么做

globals() has risk as it gives you what the namespace is currently pointing to but this can change and so modifying the return from globals() is not a good idea

globals()有风险,因为它提供了名称空间当前指向的内容,但这可能会改变,因此修改globals()的返回不是一个好主意。

#5


2  

I would use a list:

我会列出一个清单:

string = []
for i in range(0, 9):
  string.append("Hello")

This way, you would have 9 "Hello" and you could get them individually like this:

这样,你就有了9个“Hello”,你可以像这样分别得到它们:

string[x]

Where x would identify which "Hello" you want.

x会识别你想要的“你好”。

So, print(string[1]) would print Hello.

因此,print(字符串[1])将打印Hello。

#6


2  

One way you can do this is with exec(). For example:

一种方法是使用exec()。例如:

for k in range(5):
    exec(f'cat_{k} = k*2')

print(cat_0)
0
print(cat_1)
2
print(cat_2)
4
print(cat_3)
6
print(cat_4)
8

打印(cat_0) 0打印(cat_1) 2打印(cat_2) 4打印(cat_3) 6打印(cat_4) 8。

Here I am taking advantage of the handy f string formatting in python 3.6+

这里我利用了python 3.6+中方便的f字符串格式

#1


82  

Sure you can; its called a dictionary:

你当然可以;它叫字典:

d={}
for x in range(1,10):
        d["string{0}".format(x)]="Hello"

In [7]: d["string5"]
Out[7]: 'Hello'

In [8]: d
Out[8]: 
{'string1': 'Hello',
 'string2': 'Hello',
 'string3': 'Hello',
 'string4': 'Hello',
 'string5': 'Hello',
 'string6': 'Hello',
 'string7': 'Hello',
 'string8': 'Hello',
 'string9': 'Hello'}

I said this somewhat tongue in check, but really the best way to associate one value with another value is a dictionary. That is what it was designed for!

我说过这个有点拗口,但是把一个值和另一个值联系起来的最好方法是字典。这就是它的设计初衷!

#2


30  

It is really bad idea, but...

这真是个坏主意,但是……

for x in range(0, 9):
    globals()['string%s' % x] = 'Hello'

and then for example:

然后为例:

print(string3)

will give you:

会给你:

Hello

However this is bad practice. You should use dictionaries or lists instead, as others propose. Unless, of course, you really wanted to know how to do it, but did not want to use it.

然而,这是不好的做法。你应该像其他人建议的那样使用字典或列表。当然,除非你真的想知道怎么做,但又不想用。

#3


8  

It's simply pointless to create variable variable names. Why?

创建变量名是毫无意义的。为什么?

  • They are unnecessary: You can store everything in lists, dictionarys and so on
  • 它们是不必要的:你可以把所有东西都存储在列表、字典等等
  • They are hard to create: You have to use exec or globals()
  • 它们很难创建:您必须使用exec或globals()
  • You can't use them: How do you write code that uses these variables? You have to use exec/globals() again
  • 不能使用它们:如何编写使用这些变量的代码?您必须再次使用exec/globals()

Using a list is much easier:

使用列表要容易得多:

# 8 strings: `Hello String 0, .. ,Hello String 8`
strings = ["Hello String %d" % x for x in range(9)]
for string in strings: # you can loop over them
    print string
print string[6] # or pick any of them

#4


4  

Don't do this use a dictionary

不要用字典

import sys
this = sys.modules[__name__] # this is now your current namespace
for x in range(0,9):
    setattr(this, 'string%s' % x, 'Hello')

print string0
print string1
print string2
print string3
print string4
print string5
print string6
print string7
print string8

don't do this use a dict

不要这么做

globals() has risk as it gives you what the namespace is currently pointing to but this can change and so modifying the return from globals() is not a good idea

globals()有风险,因为它提供了名称空间当前指向的内容,但这可能会改变,因此修改globals()的返回不是一个好主意。

#5


2  

I would use a list:

我会列出一个清单:

string = []
for i in range(0, 9):
  string.append("Hello")

This way, you would have 9 "Hello" and you could get them individually like this:

这样,你就有了9个“Hello”,你可以像这样分别得到它们:

string[x]

Where x would identify which "Hello" you want.

x会识别你想要的“你好”。

So, print(string[1]) would print Hello.

因此,print(字符串[1])将打印Hello。

#6


2  

One way you can do this is with exec(). For example:

一种方法是使用exec()。例如:

for k in range(5):
    exec(f'cat_{k} = k*2')

print(cat_0)
0
print(cat_1)
2
print(cat_2)
4
print(cat_3)
6
print(cat_4)
8

打印(cat_0) 0打印(cat_1) 2打印(cat_2) 4打印(cat_3) 6打印(cat_4) 8。

Here I am taking advantage of the handy f string formatting in python 3.6+

这里我利用了python 3.6+中方便的f字符串格式