views:

197

answers:

6

Hi folks,

I am new to python and would appreciate a little help.

How does one do the following:

  1. Having converted each line within a file to a nested list, e.g. [['line 1', 'a'], ['line 2','b']] how do I flatten the list so that each line is associated with a variable. Assume that the first member in each list, i.e. i[:][0], is known.

  2. Is it possible to associate more than one list with one variable, i.e. can x = [list1], [list2]?

  3. Having used a for loop on a list, how those one associate aspects of that list with a variable? See example below.

Example:

for i in list_1:
    if i[:][0] == 'm':
        i[2] = a
        i[3] = b
        i[4] = c

The above returns NameError, a, b, c, not defined. How does one define variables resulting from iterations in a for loop or loops in general?

Hope I was clear and succinct as I am perplexed!

Thanks in advance, Seafoid.

A: 
  1. Why do you want to associate each item in a list with a variable? You cannot tell the number of list entries beforehand thus you do not know the exact number of variables to use.

  2. You can use tuple: x = ([list1], [list2])

  3. You should write assignment vice-a-versa:

    for i in list_1:
        if i[:][0] == 'm':
            a = i[2]
            b = i[3]
            c = i[4]
    
Li0liQ
Oh, another senior moment! Computing is cruel to us biologists!
Seafoid
+1  A: 

No. 2: I can't see how that would be possible - surely you can only assign one value to a variable?

mr1989foster
Yes, on second thought, I think I knew that! Stupid, stupid, stupid me! Thanks!
Seafoid
Not stupid, just a senior moment! ;) I get this all the time!
mr1989foster
lol - very, very true!
Seafoid
A: 

do you want:

a, b, c = i[2:5]
cobbal
+2  A: 
# Answer to question 1 - just use the built-in functionality of lists.
#
# There is no need to use variables when lists let you do so much more
#   in a quick and organised fashion.
lines = []
for line in open_file:
   lines.append(line)

Since Li0liQ already answered questions 2 and 3, I'd just like to add a recommendation regarding question 3. You really don't need to make a copy of the list via i[:] since you're just testing a value in the list.

Dustin
A: 

if I understand well, you have a list of lists, which can have length 2 or 1 (when the variable name is not known)

you would probably want to use a dict to store the lines

yet to mention i[:][0] means something different you wanted, it's the same as i[0] (i[:] would be a copy of list i)

list_1 = [['line 1', 'a'], ['line 2','b'], ['line 3']]
d = {}

for i in list_1:
    if len(i) != 2:
        continue
    key = i[1]
    value = i[0]
    d[key] = value

then for a, you would use d[a]

if you eventually want to convert them to variables, you can call locals().update(d)

mykhal
A: 

On reading my initial post, I wasn't quiet as succinct as I hoped.

To clarify:

I have a nested list, where each list within the nest holds strings. These strings are actually numbers. I wish to convert the strings to integers in order to perform arithmetic operations.

Example:

[['1', '2', '3'], ['4', '5', '6'], ['7', '8', '9']]

Now, to convert each string to an integer, is abs() appropriate? How should this be implemented?

Also, to sum the third item of each list within the nest and assign the total to a variable? Should I define a function for this?

Any suggestions on how to deal with this are much appreciated!

Also, the earlier suggestions, made me realise that my thinking was creating the problem! Thanks!

Regards, Seafoid.

Seafoid
you can comprehend a list to another list using the friendly syntax: e.g. [x[2] for x in l].. if you want the sum of integers: sum([int(x[2]) for x in l]). funny, isn't it?
mykhal