Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
String handling
#15
(Sep-15-2019, 05:38 PM)perfringo Wrote: There is always couple of minutes to spare and use them for thinking. Majority of assignments are solvable with 'pure' thinking. Some potentially useful tidbits:

- separate what from how - define what you want to do first and only after that start coding.

- use spoken language for defining what you want to do

- decompose tasks into subtasks

What for current assignment:

- I want get names and birthdays from rows
- I want to print out name and birthday in specific formatting

What: Get names and birthdays

Using just common sense analyse rows. The pattern which should emerge is that row must be split into two, starting from first decimal encountered (as discussed earlier, there can be more than two names therefore whitespace splitting can be error prone). This observation has nothing to do with coding, it's about finding general pattern.

What: Split line onto two parts on first decimal

How? We need index of the first decimal number and then slice string on this index.

Getting index is easy enough:

>>> s = 'a1b2c3'
>>> s.index('1')
1
>>> s.find('2')
3
However, we must find occurrence any decimal, not specific one. How? Quite logical step will be 'iterate over string character by character and when decimal is encountered return it's index':

>>> [s.index(char) for char in s if char.isdecimal()]  # alternatively char in '0123456789'
[1, 3, 5]
We have all indices but actually need only first. We could 'create list of decimal indexes and get first index'

>>> [s.index(char) for char in s if char.isdecimal()][0]
1
or we could 'create generator of decimal indices and take first':

>>> index = next(s.index(char) for char in s if char.isdecimal())
>>> index
1
Now we know the index where to split. Let's test:

>>> n = 'Guido van Rossum 31 January 1956'
>>> index = next(n.index(char) for char in n if char.isdecimal())
>>> name, birthday = n[:index], n[index:]
>>> name                                                                  
'Guido van Rossum '       # observe space at end, to get rid of it one can use n[:index].strip() on row #3
>>> birthday                                                              
'31 January 1956'
What: print out in specific formatting

When we have name and birthday it's easy to construct string in required formatting:

>>> s = 'Guido van Rossum 31 January 1956', 'Orville Wright 21 July 1988'
>>> for row in s: 
...     index = next(row.index(char) for char in row if char.isdecimal()) 
...     name, birthday = row[:index], row[index:] 
...     print(f'Name:\n{name}\n\nBirthday:\n{birthday}\n') 
...                                                                              
Name:
Guido van Rossum 

Birthday:
31 January 1956

Name:
Orville Wright 

Birthday:
21 July 1988
                         # observe newline at end

Holy snaps perfringo I almost feel like I am in debt to you, for you having to spend so much effort explaining this all to me. I absolutely agree with your advice , think, write pseudo code and lay it all out in logical terms before starting to code. Problem I find currently is that I simply do not have all the knowledge and tools at hand to approach and solve the tasks I need to do and these tasks are getting ever more complex as one would expect in such a boot camp.

I think its going to take time and lots of research and practice for to get where I need to be and in the mean time I just need to stay strong and push trough.

EDIT: I could not run the code at first but sorted it. Thanks again for your thorough explanation deeply appreciated perfringo
Reply


Messages In This Thread
String handling - by YoungGrassHopper - Sep-14-2019, 10:30 AM
RE: String handling - by metulburr - Sep-14-2019, 10:41 AM
RE: String handling - by YoungGrassHopper - Sep-14-2019, 10:42 AM
RE: String handling - by YoungGrassHopper - Sep-15-2019, 07:02 AM
RE: String handling - by perfringo - Sep-15-2019, 07:32 AM
RE: String handling - by ndc85430 - Sep-15-2019, 07:34 AM
RE: String handling - by YoungGrassHopper - Sep-15-2019, 08:12 AM
RE: String handling - by snippsat - Sep-15-2019, 09:55 AM
RE: String handling - by YoungGrassHopper - Sep-15-2019, 12:01 PM
RE: String handling - by jefsummers - Sep-15-2019, 12:21 PM
RE: String handling - by YoungGrassHopper - Sep-15-2019, 12:33 PM
RE: String handling - by YoungGrassHopper - Sep-15-2019, 01:12 PM
RE: String handling - by newbieAuggie2019 - Sep-15-2019, 09:20 PM
RE: String handling - by perfringo - Sep-15-2019, 01:05 PM
RE: String handling - by perfringo - Sep-15-2019, 05:38 PM
RE: String handling - by YoungGrassHopper - Sep-15-2019, 08:28 PM
RE: String handling - by YoungGrassHopper - Sep-15-2019, 10:37 PM
RE: String handling - by jefsummers - Sep-16-2019, 12:47 AM
RE: String handling - by YoungGrassHopper - Sep-16-2019, 04:45 AM
RE: String handling - by perfringo - Sep-16-2019, 05:11 AM
RE: String handling - by YoungGrassHopper - Sep-16-2019, 05:23 AM
RE: String handling - by perfringo - Sep-16-2019, 06:09 AM
RE: String handling - by YoungGrassHopper - Sep-16-2019, 06:46 AM
RE: String handling - by buran - Sep-16-2019, 06:54 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
Star python exception handling handling .... with traceback mg24 3 1,322 Nov-09-2022, 07:29 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

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