Python Forum
I'm getting an AttributeError. Don't know how to go arround it.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I'm getting an AttributeError. Don't know how to go arround it.
#1
About the program.
I want the program to output all the possible results in a list. But I'm currently struggling removing the current quotes, commas, spaces and brackets from the output.

If the list contains 1 and A. I'm looking to get an output like the following:

11
1A
AA
A1

The current output without the AttributeError is:

('1', '1')
('1', 'A')
('A', 'A')
('A', '1')


Please find the code attached:
import itertools       
                       
blocks = 2             
List = ["1", "A"]
                       
List.replace(",", "")  

for result in itertools.product(List, repeat=blocks):
	print(str(result)) 
Here is the error:
Error:
List.replace(",", "") AttributeError: 'list' object has no attribute 'replace'
I'm using the Python 3.8.3 in Ubuntu with Pycharm.

I've search online "AttributeError: 'list' object has no attribute 'replace'" but with my limited knowledge in python and overall programing I didn't manage to find a clear solution.

Thank you for your time.
Reply
#2
Remove line 6,as just need to use the List in the loop,then .join() together the result.
import itertools

blocks = 2
List = ["1", "A"]
for result in itertools.product(List, repeat=blocks):
    print(''.join(result))
Output:
11 1A A1 AA
Reply
#3
you need to join the elements of result
import itertools       
                        
blocks = 2             
my_list = ["1", "A"]
 
for result in itertools.product(my_list, repeat=blocks):
    print(''.join(result))
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
results is a tuple you can unpack the two items and display them with string formating
import itertools

blocks = 2
my_list = ["1", "A"]

for first, second in itertools.product(my_list, repeat=blocks):
    print(f'{first}{second}')
Output:
11 1A A1 AA
Reply
#5
:-) three answers within a minute
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
Impressive response time!
@snippsat, @buran and @Yoriz. Thank you very much for helping me solve the problem.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020