I am very fresh to python. As part of an assignment, I've written the following code to remove punctuation from a string and convert it to lowercase.
import string
words = "Dave, Laura, Maddy, Dave, Laura, Maddy, Dave, Laura, Dave";
translation = str.maketrans("","", string.punctuation);
new = words.translate(translation);
lower = new.lower();
However, this seems crude to me and I feel like I can accomplish my task with fewer function calls/less code. Does anyone have any suggestions as to how I could do this?
-
\$\begingroup\$ I mean I've done the tasked asked of me no problem, and this is maybe a quarter of it. I'm simply asking if theres a cleaner method to it. This'll suffice just fine for my class. \$\endgroup\$David Tamrazov– David Tamrazov2015年11月11日 02:38:23 +00:00Commented Nov 11, 2015 at 2:38
-
1\$\begingroup\$ This question is on the line for off-topic category right now, and I am not sure what to do with it. It seems to be unfinished, so I will vote to close this question. \$\endgroup\$TheCoffeeCup– TheCoffeeCup2015年11月11日 03:36:23 +00:00Commented Nov 11, 2015 at 3:36
-
\$\begingroup\$ This doesn't look too off-topic to me. It's fully functional code, and David is asking for improvements. \$\endgroup\$anon– anon2015年11月11日 05:00:19 +00:00Commented Nov 11, 2015 at 5:00
-
1\$\begingroup\$ There is absolutely nothing wrong with this. OP has accomplished his task, and is asking how could it be better. This is 100% what CR is about. We wouldn't have a homework tag if this wasn't acceptable. \$\endgroup\$Legato– Legato2015年11月11日 05:39:38 +00:00Commented Nov 11, 2015 at 5:39
-
1\$\begingroup\$ There is a bit of a mess, though. The original title mentioned splitting the string, which the code never actually does. Yet @PatrickS has incorporated splitting into his answer. \$\endgroup\$200_success– 200_success2015年11月11日 05:41:00 +00:00Commented Nov 11, 2015 at 5:41
3 Answers 3
To reduce the number of lines and statements for your problem note:
- For your input format splitting on spaces and removing punctuation can be a single operation: split on
,
(comma-space). - You don't need to use a (new) variable for every intermediate step.
String methods can be chained. Here words
is first made lower-case and then split
is called.
words = "Dave, Laura, Maddy, Dave, Laura, Maddy, Dave, Laura, Dave";
result = words.lower().split(", ")
Alternatively you can use a list comprehension. Here the words
are split
and then every word of the intermediate result is made lower-case.
words = "Dave, Laura, Maddy, Dave, Laura, Maddy, Dave, Laura, Dave";
result = [word.lower() for word in words.split(", ")]
-
\$\begingroup\$ Your answer returns a result, while I think OP just wants a string as output? \$\endgroup\$kushj– kushj2015年11月11日 05:49:43 +00:00Commented Nov 11, 2015 at 5:49
-
\$\begingroup\$ Oh brilliant, that's so much cleaner. Thats exactly the kind of revision I was looking for, thanks @PatrickS. \$\endgroup\$David Tamrazov– David Tamrazov2015年11月11日 07:36:43 +00:00Commented Nov 11, 2015 at 7:36
-
\$\begingroup\$ @kushj No I actually need a string to work with. \$\endgroup\$David Tamrazov– David Tamrazov2015年11月11日 07:37:18 +00:00Commented Nov 11, 2015 at 7:37
Instead of just import string
you could get the punctuation list directly:
from string import punctuation
words = "Dave, Laura, Maddy, Dave, Laura, Maddy, Dave, Laura, Dave"
translation = str.maketrans("", "", punctuation)
I'd also remove the semicolons. Python doesn't need them to know when a line of code ends since it uses newlines instead (there are exceptions, but none here) so you can remove them.
Here's how I would do it:
Mesh the following operations into a single line:
- step 1: str.split(",") # split string into a list, remove "," chars
- step 2: "".join(lst) # join lst items with "" in between
- step 3: str.lower() # turn string to lowercase
Your desired code:
res = "".join(words.split(",")).lower()
p.s. best I am aware, join() is faster than list comprehension.