-3

I'm working on a school project in which we need to create a solution for 8-puzzle problems using the a* algorithm. Anyways, I would like to ask a user for an input say:

123  
456  
7 8

How do I separate those and store in a list? I know how to store it on a list, my problem is the separation.

sampleList = ["1", "2", "3", "4", "5", "6", "7", "0", "8"]
Shai
  • 111,146
  • 38
  • 238
  • 371
Mac Mac
  • 33
  • 1
  • 5
  • http://stackoverflow.com/questions/974952/split-an-integer-into-digits-to-compute-an-isbn-checksum – 0x90 Jan 26 '13 at 11:37

2 Answers2

2

One approach would be to access the digits one at a time while they were still in string form.

>>> input = ['123', '456', '7 8']
>>> sampleList = []
>>> for digit_str in input:
...     for digit in digit_str:
...         sampleList.append(digit)
... 
>>> sampleList
['1', '2', '3', '4', '5', '6', '7', ' ', '8']

Now since this is for school I'll leave it to you to work out how to change the space into a zero :)

scanny
  • 26,423
  • 5
  • 54
  • 80
0

Maybe these can solve your problem

s = '''
123
456
7 8
'''

def digitsFromStr(string):
    return [
            x if x != ' ' else '0'
            for x in string
            if x != '\n'
    ]

print digitsFromStr(s)
Daiver
  • 1,488
  • 3
  • 18
  • 47