numbering variables

R

remi

Hello,

I have got a list like : mylist = ['item 1', 'item 2',....'item n'] and
I would like to store the string 'item1' in a variable called s_1,
'item2' in s_2,...,'item i' in 's_i',... The lenght of mylist is finite ;-)
Any ideas ?
Thanks a lot.
Rémi.
 
F

Fredrik Lundh

remi said:
I have got a list like : mylist = ['item 1', 'item 2',....'item n'] and I would like to store the
string 'item1' in a variable called s_1, 'item2' in s_2,...,'item i' in 's_i',...

why?

</F>
 
P

Patrick Useldinger

remi said:
Hello,

I have got a list like : mylist = ['item 1', 'item 2',....'item n'] and
I would like to store the string 'item1' in a variable called s_1,
'item2' in s_2,...,'item i' in 's_i',... The lenght of mylist is finite ;-)
Any ideas ?
Thanks a lot.
Rémi.

Use a dictionary: variable['s_1']= mylist.pop(), variable['s_2'] =
mylist.pop() ...
 
R

remi

Patrick Useldinger a écrit :
remi said:
Hello,

I have got a list like : mylist = ['item 1', 'item 2',....'item n']
and I would like to store the string 'item1' in a variable called s_1,
'item2' in s_2,...,'item i' in 's_i',... The lenght of mylist is
finite ;-)
Any ideas ?
Thanks a lot.
Rémi.


Use a dictionary: variable['s_1']= mylist.pop(), variable['s_2'] =
mylist.pop() ...

Ok thanks but is there a way to automate this variable numbering ? Such
like :
i = 0
for i <=len(mylist)
s_i=mylist
i = i +1
or as you suggest :
i = 0
while i<=len(mylist)
variable['s_i']=mylist.pop()
i = i+1
Thanks.
Rémi.
 
R

remi

Fredrik Lundh a écrit :
I have got a list like : mylist = ['item 1', 'item 2',....'item n'] and I would like to store the
string 'item1' in a variable called s_1, 'item2' in s_2,...,'item i' in 's_i',...


why?

I want to convert a very simple *.tex file into a very simple *.xml file
mylist is made by :

file_in = open('in.tex','r')
file_string = file_in.read()

corps = r"""\\begin{document}(?P<contents>.*)\\end{document}"""
compile_obj = re.compile(corps, re.IGNORECASE| re.DOTALL)
match_obj = compile_obj.search(file_string)
contents = match_obj.group('contents')

sect = r'\\section{(.*)}'
motif1 = re.compile(sect)
mylist = re.split(motif1,contents)

I want to store each item of mylist in a variable (and after "xmlize" it
little by little). The lengh of mylist isn't fixed it depends on the
*.tex file.
Thanks.
Rémi.
 
F

Fredrik Lundh

remi said:
i = 0
while i<=len(mylist)
variable['s_i']=mylist.pop()
i = i+1

variable['s_' + str(i)]=mylist.pop()

but that while and pop stuff is pretty weird; maybe you should read the
sections on lists, for loops, and string formatting in the Python tutorial?

here's a shorter way to create that dictionary:

variable = {}
for item in mylist:
variable["s_%d" % len(variable)] = item

and here's the obligatory one-liner:

variable = dict(("s_" + str(k), v) for k, v in enumerate(mylist))

(shorter, but not exactly clearer, especially not if you're new to Python)

</F>
 
F

Fredrik Lundh

remi said:
I want to store each item of mylist in a variable (and after "xmlize" it little by little).

why not just process the items in the list?

for item in mylist:
print tex2xml(item)

</F>
 
R

remi

Fredrik Lundh a écrit :
why not just process the items in the list?

That's right.
for item in mylist:
print tex2xml(item)

where tex2xml() is the main converting function i guess.
So i should change "print tex2xml(item)" to a kind of
"output.write(tex2xml(item))".

Ok, i try this too but i think it will lead to a "line by line"
processing of the file... I try.
Thanks.
Rémi.
 
R

remi

Fredrik Lundh a écrit :
variable['s_i']=mylist.pop()
variable['s_' + str(i)]=mylist.pop()

but that while and pop stuff is pretty weird;

Why ? Because it destroys the list ?
maybe you should read the
sections on lists, for loops, and string formatting in the Python tutorial?

here's a shorter way to create that dictionary:

variable = {}
for item in mylist:
variable["s_%d" % len(variable)] = item

maybe a dictionnary is more easely usable and reusable than a list...
Thanks a lot for your help.
and here's the obligatory one-liner:

variable = dict(("s_" + str(k), v) for k, v in enumerate(mylist))

(shorter, but not exactly clearer, especially not if you're new to Python)

As a beginner, I agree at 100% even if the latter is nicer !
Rémi.
I send to people form clp a little sun from Toulouse (France)
 
R

Ron_Adam

Hello,

I have got a list like : mylist = ['item 1', 'item 2',....'item n'] and
I would like to store the string 'item1' in a variable called s_1,
'item2' in s_2,...,'item i' in 's_i',... The lenght of mylist is finite ;-)
Any ideas ?
Thanks a lot.
Rémi.


Why not just access the list by index? Just start with zero instead
of 1.

mylist = ['item 1', 'item 2',....'item n']


mylist[0] is 'item 1'
mylist[1] is 'item 2'
..
..
mylist[n-1] is 'item n'


Ron Adam
 
S

Steve

Ron_Adam said:
Hello,

I have got a list like : mylist = ['item 1', 'item 2',....'item n'] and
I would like to store the string 'item1' in a variable called s_1,
'item2' in s_2,...,'item i' in 's_i',... The lenght of mylist is finite ;-)
Any ideas ?
Thanks a lot.
Rémi.

Why not just access the list by index? Just start with zero instead
of 1.

If you _really_ must have one-based indexes, use a
sentinal at the beginning of the list:
>>> mylist = ['item 1', 'item 2',....'item n']
>>> s = [None] + mylist # the sentinal is never used
>>> print s[1] 'item 1'
>>> print s[28] 'item 28'
>>> print s[29] # there are only 28 items!
Traceback (most recent call last):
File "<stdin>", line 1, in ?
IndexError: list index out of range


Then you can process all the items without needing to
remember how many there are:
.... do_something(s)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,230
Messages
2,571,161
Members
47,796
Latest member
AlphonseNa

Latest Threads

Top