1

I have a variable of int type. I am running for loop on it and making a variable with adding some string info.

a = 5 // variable with integer value
list = '' // empty variable
for x in range(a):
  list.append('MA[' + x + '] |')

Expected output:

MA[5] | MA[4] | MA[3] | MA[2] | MA[1] | MA[0]
1
  • Note that you'll get AttributeError: 'str' object has no attribute 'append'. And apart from that, str.join - what you want can be written as ' | '.join('MA[%d]' % i for i in ...). Commented Jan 5, 2011 at 10:13

5 Answers 5

3

As @Frank points out, list is a string which has no .append() method. You seriously should rename it to something more meaningful, but at the very least don't confuse the reader of your code (yourself included!) by naming a string list. Renaming it to string and using str() to cast x to a string:

string += 'MA[' + str(x) + '] |'

Or better, learn string formatting. In this case it would be:

string += 'MA[%d] | % x

You'll also need to change 5 t 6 if you want to iterate over the range 0-5 inclusive. And if you want to iterate in reverse, use range(a, -1, -1).

Here's a generator combined with join():

>>> ' | '.join('MA[%d]' % x for x in xrange(5, -1, -1))
'MA[5] | MA[4] | MA[3] | MA[2] | MA[1] | MA[0]'
Sign up to request clarification or add additional context in comments.

2 Comments

+1 for finding the most errors and showing a textbook solution.
@delnan With the help of others here!
1

Use string formatting. In Python 2.x:

list += ('MA[%s] |' % x)

The whole thing can more easily be written with a list comprehension and join:

my_list = ' | '.join('MA[%s]' % x for x in reversed(xrange(a)))

Also, don't call your variables list, that shadows the built-in function.

4 Comments

%d (is this the weFail question? :P)
+1 for margcog's comment, although strictly speaking %s works as well (it's just not the best choice semantically, behaves different for e.g. %02(d|s) and unlike %d it won't catch non-integers).
what's the big difference between %d and %s? Both give the same result here. Is %d faster?
@Frank %d formats an integer, %s a string. Since x is an integer, best use %d.
0

Here's a version with a generator expression:

>>> ' | '.join('MA[%s]' % x for x in range(5))
'MA[0] | MA[1] | MA[2] | MA[3] | MA[4]'

I also don't know why the other answers use .append. Strings don't have that method. Just because a variable is called list doesn't mean it is one.

Also note that your desired output is reversed. range(5) gives you [0,1,2,3,4] to iterate through. So your elements are created in the other order (from 0 up to 4) and do not include a 5..

5 Comments

Gar, caught out by the list name.
Drop the pointless list comprehension. Generator expressions are your friend.
Thanks for the pointer delnan. Still use the list comprehensions too often out of habit.
thanks frank for the promt ans. can you point me to some advance doc for python
Please use 'MA[{0}]'.format( x ) for x in range(5) instead of the % operator. % will go away in Python 3.
0

To get what you want you need also to revert the list on which you iterate: very simple example:

a = 5
lst = ''
it_list = range(a+1)
it_list.reverse()
for x in it_list:
    lst += 'MA[' + str(x) + '] |'

print lst[:-3]

Output:

MA[5] | MA[4] | MA[3] | MA[2] | MA[1] | MA[0]

Comments

0

Problems:

  1. You are using a bad comment-string format - Python uses the pound char (#)

  2. calling a variable 'list' is bad because it 'writes over' the list() constructor

  3. worse, you are making "list" a string!

  4. You are trying to use the list append() method - but because you have defined "list" as a string, and there is no string append() method, it fails

  5. You are iterating backwards from the desired output (you are going '0 up to 4', not '5 down to 0')

  6. You have an off-by-one error - Python does not include the last value in a range, so range(5) returns 0,1,2,3,4

Try:

def makeCallString(n, fmt="MA[{0}]", jn=" | "):
    "Return string consisting of fmt[n] + jn + fmt[n-1] + jn + ... + fmt[0]"
    return jn.join(fmt.format(a) for a in range(n, -1, -1))

Then

makeCallString(5)

returns

MA[5] | MA[4] | MA[3] | MA[2] | MA[1] | MA[0]

as a string.

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.