python中变量转换_如何在python中将变量转换为字符串

我也有类似的问题,但是我不知道这个词是什么。

cat=5

dog=3

fish=7

animals=[cat,dog,fish]

for animal in animals:

print animal_name+str(animal)  #by animal name, i mean the variable thats being used

它会打印出来,

cat5

dog3

fish7

所以我想知道是否有一个实际的方法或函数可以用来检索正在使用的变量并将其转换为字符串。希望这可以在不为每只动物创建字符串的情况下完成。

编辑:

没有字典有没有办法做到这一点?

为什么不使用python字典呢?

没有字典有没有办法做到这一点?

相关:stackoverflow.com/questions/544919/…

为什么你需要避免使用字典?

您基本上是在问"我的代码如何发现对象的名称?"

def animal_name(animal):

# here be dragons

return some_string

cat = 5

print(animal_name(cat))  # prints"cat"

Fredrik Lundh(在comp.lang.python上)的一句话在这里特别合适。

The same way as you get the name of that cat you found on your porch:

the cat (object) itself cannot tell you its name, and it doesn’t

really care — so the only way to find out what it’s called is to ask

all your neighbours (namespaces) if it’s their cat (object)…

….and don’t be surprised if you’ll find that it’s known by many names,

or no name at all!

为了好玩,我尝试使用sys和gc模块来实现animal_name,发现邻里也在用几个名字来称呼你亲切地称为"cat"的对象,即字面整数5:

>>> cat, dog, fish = 5, 3, 7

>>> animal_name(cat)

['n_sequence_fields', 'ST_GID', 'cat', 'SIGTRAP', 'n_fields', 'EIO']

>>> animal_name(dog)

['SIGQUIT', 'ST_NLINK', 'n_unnamed_fields', 'dog', '_abc_negative_cache_version', 'ESRCH']

>>> animal_name(fish)

['E2BIG', '__plen', 'fish', 'ST_ATIME', '__egginsert', '_abc_negative_cache_version', 'SIGBUS', 'S_IRWXO']

对于足够唯一的对象,有时可以获得唯一的名称:

>>> mantis_shrimp = 696969; animal_name(mantis_shrimp)

['mantis_shrimp']

因此,总结如下:

简短的回答是:你不能。

答案很长:实际上,你可以……至少在cpython实现中。要了解如何在我的示例中实现animal_name,请看这里。

正确的答案是:使用dict,正如其他人在这里提到的。当您实际需要知道名称对象关联时,这是最佳选择。

我想有可能使用我在某个地方读到的回溯。我可以故意导致变量出错,并将此错误放入字符串中,然后使用regex检索变量。

一个有趣的主意!如果你能用这种方法取得一些成功,我会很感兴趣的。

如果可以的话,我会给你两张赞成票。

使用字典而不是一堆变量。

animals = dict(cat=5, dog=3, fish=7)

for animal, count in animals.iteritems():

print animal, count

请注意,它们可能不会(可能不会)按照您输入的顺序输出。您可以使用collections.ordereddict来解决这个问题,如果您只需要按照一致的顺序对密钥进行排序,也可以只对它们进行排序:

for animal in sorted(animals.keys()):

print animal, animals[animal]

您将变量名与动物名的字符串混淆:

在这里:

cat = 7

cat是变量,7它的值

在:

cat = 'cat'

cat仍然是变量,"cat"是带有动物名称的字符串。你可以把任何你喜欢的绳子放在猫里面,甚至是cat = 'dog'。

现在回到你的问题上来:你要打印出一个动物的名字和一个对应的号码。

要对名称和数字进行配对,最好的选择是使用dict字典。{和}代表一个dict(在某些情况下,也可以是set)。

d = {3: 'cat', 5: 'dog', 7: 'fish'}

d是您的变量。{3: 'cat', 5: 'dog', 7: 'fish'}是你的字典。3, 5, 7是该字典的键,'cat', 'dog', 'fish'是对应的值。现在你需要重复这本字典。可以调用d.items():

d = {3: 'cat', 5: 'dog', 7: 'fish'}

for key,value in d.items():

print(value, key)

我把值倒过来,按顺序打印,把名字印在号码前。

我不把变量放在列表里,而是把它们放进字典里。

d={}

d['cat']=5

d['dog']=3

d['fish']=7

for item in d.keys():

print item+' '+d[item]

myAnimals = {'cat':5,'dog':3,'fish':7}

animals = ['cat','dog','fish']

for animal in animals:

if myAnimals.has_key(animal): print animal+myAnimals(animal)

我将改为使用字典变量,它可以让您轻松地将字符串名称映射为值。

animalDict['cat'] = 5

animalDict['dog'] = 3

然后你可以通过按键进行交互并打印出你想要的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值