开发者

using %s to change the name of a list

Is there a way to change the name of a list using %s? Here's an example:

dict1[key]=value

for x in dict1.keys():
    %s %开发者_Python百科(x)profile=[]
    if dict1[x]=1:
        %s %(x)profile.append('yes')

This code didn't work, but I'm looking for something that will give me 'n' lists, one for each x in dict1.keys().


Short answer: no.

Long answer: no, you can't do exactly that but if you're a little more clear with your intentions (it's hard to tell with your pseudocode what you're looking to do), there is certainly a way you can do what you need to do even though you can't do what you're trying to do

Short and dangerous answer: Actually yes you sort of can, but no you generally shouldn't

edit for your updated comment

So instead of having specifically named lists, what you want to use is another dictionary:

new_dict = {}
for key in dict1.keys()
    new_key = "%sprofile" % key
    if dict1[key] == 1:   # note your = is actually a SyntaxError
        new_dict[new_key] = ['yes']
    else:
        new_dict[new_key] = []

This would result in a new dictionary with keys named "(key)profile" and each value associated would be a list either with "yes" in it if the original dict's value was 1 for the original key, or an empty list.


dict1 = {'a':0, 'b':0, 'c':1}

profile = { k:(['yes'] if v==1 else []) for k,v in dict1.iteritems() }

print profile
>>> {'a': [], 'b': [], 'c': ['yes']}


Does this help you ? :

dic = { 1:['a','h','uu'] , 3:['zer','rty'] , 4:['hj','125','qsd'] }

print 'BEFORE LOOP :\n\nglobals()==',globals()
print
print "keys of globals() not written __xxx__ :",' , '.join(u for u in globals() if not u.startswith('__'))
print '\n-----------------------------------------------------------------\nDURING LOOP :\n'
for x in dic:
    print 'x==',x
    globals()['L'+str(x)+'profile'] = dic[x]
    print 'keys of globals() not written __xxx__ :',' , '.join(u for u in globals() if not u.startswith('__'))

print '\n-----------------------------------------------------------------\nAFTER LOOP :\n'
print 'keys of globals() not written __xxx__ :',' , '.join(u for u in globals() if not u.startswith('__'))
print
for li in (L1profile,L3profile,L4profile):
    print 'li==',li

result

BEFORE LOOP :

globals()== {'__builtins__': <module '__builtin__' (built-in)>, '__package__': None, '__name__': '__main__', 'dic': {1: ['a', 'h', 'uu'], 3: ['zer', 'rty'], 4: ['hj', '125', 'qsd']}, '__doc__': None}

keys of globals() not written __xxx__ : dic

-----------------------------------------------------------------
DURING LOOP :

x== 1
keys of globals() not written __xxx__ : L1profile , x , dic
x== 3
keys of globals() not written __xxx__ : L1profile , L3profile , x , dic
x== 4
keys of globals() not written __xxx__ : L1profile , L3profile , x , dic , L4profile

-----------------------------------------------------------------
AFTER LOOP :

keys of globals() not written __xxx__ : L1profile , L3profile , x , dic , L4profile

li== ['a', 'h', 'uu']
li== ['zer', 'rty']
li== ['hj', '125', 'qsd']

As the name of objects must not begin with a digit character, 'L' is put systematically as first letter of the created names.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜