By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,676 Members | 2,249 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,676 IT Pros & Developers. It's quick & easy.

numbering variables

P: n/a
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.
Jul 18 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
"remi" <re**@non.com> wrote:
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>

Jul 18 '05 #2

P: n/a
remi wrote:
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() ...
Jul 18 '05 #3

P: n/a
Patrick Useldinger a écrit :
remi wrote:
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 = i +1
or as you suggest :
i = 0
while i<=len(mylist)
variable['s_i']=mylist.pop()
i = i+1
Thanks.
Rémi.
Jul 18 '05 #4

P: n/a
Fredrik Lundh a écrit :
"remi" <re**@non.com> wrote:

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{documen t}"""
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.
Jul 18 '05 #5

P: n/a
"remi" <re**@non.com> wrote:
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>

Jul 18 '05 #6

P: n/a
"remi" <re**@non.com> wrote:
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>

Jul 18 '05 #7

P: n/a
Fredrik Lundh a écrit :
"remi" <re**@non.com> wrote:
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?


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.

</F>

Jul 18 '05 #8

P: n/a
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)
Jul 18 '05 #9

P: n/a
On Mon, 28 Mar 2005 13:39:17 +0200, remi <re**@non.com> wrote:
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
Jul 18 '05 #10

P: n/a
Ron_Adam wrote:
On Mon, 28 Mar 2005 13:39:17 +0200, remi <re**@non.com> wrote:
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:
for i in range(1, len(s)):

.... do_something(s[i])
--
Steven

Jul 18 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.