1

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!

Update:

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!

2
  • Apologies for the syntax in the for loop - it went funny when I posted it! Commented Dec 19, 2009 at 0:04
  • You can use the code button in the editor to format your code. Simply select it and click on the icon in 1s and 0s. Commented Dec 19, 2009 at 0:07

5 Answers 5

2
# 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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

2 Comments

Yes, on second thought, I think I knew that! Stupid, stupid, stupid me! Thanks!
Not stupid, just a senior moment! ;) I get this all the time!
0
  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]
    

1 Comment

Oh, another senior moment! Computing is cruel to us biologists!
0

do you want:

a, b, c = i[2:5]

Comments

0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.