Home > Net >  in python language create several variables in a loop and after append items in each ones
in python language create several variables in a loop and after append items in each ones

Time:04-14

I'm trying to find a way to append items in variables created on the fly

c = ('a','b','g','d','j')
p = 2

for r in c:
    globals()['ssvar%s' % r] = []

for z in range (0,10,1):
    for r in c:
        p = p   2
        (['ssvar%s' % r]).append (p)
        
    print ssvara        #result     >>> []
    print ssvarb        #result     >>> []
    print ssvarg        #result     >>> []
    print ssvard        #result     >>> []
    print ssvarj        #result     >>> []

but the expression (['ssvar%s' % poire]).append doesn't work. can you direct me to the same topic or tell me how to vary the variable name to be fill ?

CodePudding user response:

I try to create temporaly variables with multiple items inside. in fact I need to have, for exemple, this result:

ssvara
>>>> ['4','6','8','10','12','14','16', '8','20','22']
ssvara
>>>> ['24','26','28','30','32','34','36', '38','40','42']

the value of p is not important, the main thing is to be able to append. even with a local variable.

CodePudding user response:

Don't do this, but I think what you were looking to do is

c = ('a','b','g','d','j')
p = 2

for r in c:
    globals()['ssvar%s' % r] = []

for z in range (0, 10, 1):
    for r in c:
        p = p   2
        globals()['ssvar%s' % r].append(p)

Instead, you can create your own dictionary (container of key: value pairs) and store the lists in there as values and use the keys as names. If this dictionary is called my_dict, then my_dict['ssvara'] references the list corresponding to 'ssvara', my_dict['ssvarb'] references the list corresponding to 'ssvarb' and so on.

c = ('a','b','g','d','j')
p = 2

my_dict = {}
for r in c:
    my_dict['ssvar%s' % r] = []

for z in range (0, 10, 1):
    for r in c:
        p = p   2
        my_dict['ssvar%s' % r].append(p)

print my_dict

Output

{'ssvara': [4, 14, 24, 34, 44, 54, 64, 74, 84, 94],
 'ssvarb': [6, 16, 26, 36, 46, 56, 66, 76, 86, 96],
 'ssvard': [10, 20, 30, 40, 50, 60, 70, 80, 90, 100],
 'ssvarg': [8, 18, 28, 38, 48, 58, 68, 78, 88, 98],
 'ssvarj': [12, 22, 32, 42, 52, 62, 72, 82, 92, 102]}

If the actual names are not important (you are, after all, creating them dynamically), you can just create a list of lists. If this list is called my_list, my_list[0] references the first sublist, my_list[1] references the second, and so on.

c = ('a','b','g','d','j')
p = 2

my_list = [[] for i in range(len(c))]

for z in range (0, 10, 1):
    for l in my_list:
        p = p   2
        l.append(p)

print my_list

Output

[[4, 14, 24, 34, 44, 54, 64, 74, 84, 94],
 [6, 16, 26, 36, 46, 56, 66, 76, 86, 96],
 [8, 18, 28, 38, 48, 58, 68, 78, 88, 98],
 [10, 20, 30, 40, 50, 60, 70, 80, 90, 100],
 [12, 22, 32, 42, 52, 62, 72, 82, 92, 102]]

I don't use Python 2 so I had to make a few interpolations.

  • Related