Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
String handling
#9
(Sep-15-2019, 09:55 AM)snippsat Wrote: In a ordinary loop it would be like this.
>>> string = 'The quick brown fox jumps over the lazy dog. '
>>> characters = 'aeiou'
>>> 
>>> lst = []
>>> for c in string:
...     if c not in characters:
...         lst.append(c)
...         
>>> print(''.join(lst))
Th qck brwn fx jmps vr th lzy dg.
So a list comprehension construct make code over in line.

Can also remove the list as it's make a generator object first.
>>> g = (c for c in string if c not in characters)
>>> g
<generator object <genexpr> at 0x03C8C8B0>

>>> next(g)
'T'
>>> next(g)
'h'
See that there is a small difference no [],and it still work as ''.join() run the generator expressions.
>>> print(''.join(c for c in string if c not in characters))
Th qck brwn fx jmps vr th lzy dg. 


Thanks snippsat Smile
The ordinary loops is much softer on my understanding as I have only worked with a couple of them till date and haven't been taught or tasked to use a for loop as a one liner. the one liners though look more simple as there seems no need for indentation and all that.

Since this thread is named string handling and the use of for loops are being discussed.
I have another task bugging me which I need advice on.

So I have a txt file with a list of names surnames and date of births.
it looks like this:

Orville Wright 21 July 1988
Rogelio Holloway 13 September 1988
Marjorie Figueroa 9 October 1988
Debra Garner 7 February 1988

And so on.

I need to write python code which reads in the data from the list and restructures and prints it in this format:

Name:
Debra Garner    

Birthdate:
7 February 1988

My attempt :

f = open
doc = f.read()
print(doc.replace(' ','\n'))
f.close()                                                                                                             
Now I am getting:

Debra
Surname
21
Aug
1999


Is it possible to refer to the second white space in a line or how would one efficiently go about solving this?
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,323 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