0

hi all

i have list like
list1=[2,3,4]
m = 'ma'
m = list1 * m

wen i run the script i'm getting output like
TypeError: can't multiply sequence by non-int

i need a output like
[ma2,ma3,ma4]


so plzzzzz help me:(

4
Contributors
5
Replies
6
Views
6 Years
Discussion Span
Last Post by pyTony
0

thanks for a reply Gribouillis

wen i run this ["ma%d" % n for n in range(2, 5)] i m gettin error:
SyntaxError: invalid syntax

0

You typed wrong, but the way you typed in your message is correct, try again!

IDLE 2.6.6      
>>> ["ma%d" % n for n in range(2, 5)]
['ma2', 'ma3', 'ma4']
>>>

(If you copy code to message push first (CODE) or paint the code and push it afterwards.)

You are expecting also wrong result, here is generic way to do your result, but the output is different:

import itertools

list1=[2,3,4]
m = 'ma'
print(tuple('%s%s' % combination for combination in itertools.product(list1, m)))
""" Output (not what you expected, but logically correct):
('2m', '2a', '3m', '3a', '4m', '4a')
"""

Edited by mike_2000_17: Fixed formatting

0

Actually
[ma2, ma3, ma4]
would imply a list of variable names.
You have to let Python know what those variable names represent.

1

Yes Vegas eater, but expecting variables to come out of the product would be little much.

If the OP, would open his heart and share his dreams, which he want to accomplish together with Great Python, maybe we could help to open the Gate of Heaven ;)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.