Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I have written a large block of code in Python that I just realized is indented exactly three spaces (not four!) Using Notepad++ as my IDE, I cannot find any way to indent exactly one additional space to make it line up with everything else.
I imagine there is some way to write a macro to shift everything by one space, but I have little intention on mastering Notepad++'s macros just for this one case. Perhaps there is even a setting I missed?
Is there a non-manual way to indent to the proper alignment (adding one space)?
Just to write up the comment as an answer (as asked by the OP).
You just need to do a find and replace with a Regular Expression that matches for 3 space characters at the beginning of a line and replace it with four characters. So the pattern to match would be something like ^\s{3}[^\s].
Option 1:
Search Replace
Search Mode - Regex
Find what ^\s
Replace with <2 space characters>
Option 2:
Do a block select of all the columns. For block select, use ALT + SHIFT followed by dragging your mouse all the way from start to end
Add as many spaces as you want
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
name=str(input("enter the string:"))
count=0
for x in name:
if x.isupper():
count=count+1
print("The number of capital letters found in the string is:",count)
How can I rewrite this code without a for loop that gets the same function?
Since this seems like a homework problem, it's probably not appropriate to just post an answer. To give you some hints:
you could re-write the for loop as a while loop that uses a counter
you could re-write the for loop as a while loop that pops characters off of name one-at-a-time, and terminates when name is empty
you could use a list comprehension with a filter to get just the upper-case characters, and report the length of the resulting string
you could write a recursive function
you could use filter the same way you would use a list comprehension
you could use sum, as suggested in comments above
you could use functools.reduce (or just reduce if you're using a geriatric python interpreter)
if you're feeling really perverse, you could use regular expressions
Along with probably a dozen other ways that I'm not thinking of now...
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
When using re.search or re.match, is it faster to use .lower() or re.IGNORECASE?
By faster I mean which takes the least time to execute?
Also is one more pythonic than the other? - This is potentially opinionated, but I would like to know for my own knowledge, the most important thing is which executes faster when the script is run.
I.E
mystring = "TeSt"
lowerresults = re.match("^[a-z]{4}$", mystring.lower())
ignoreresults = re.match("^[a-z]{4}$", mystring, re.IGNORECASE)
Edit:
The case of the output is not important, speed and matching the regex I have built is all I am concerned about.
Generally the re.IGNORECASE is a better solution overall.
Speedwise, .lower() has to transform the string, potentially taking up more time. Also, the resulting matches will be lowercase. In case you want to retain the case in the result, this won't work. Also, space wise you will have to store the lowercase string as well.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
I have a vector of strings (phrases with several words).
For reasons out of the scope of this question I need to comply with a length limit of N characters per string.
The very first thing I thought was to splice each string, but unfortunately the result of the operation will be facing end user (the end users will have to read the truncated strings and make sense out of them).
That means that I can't just slice the strings, because if I did so the following:
This is a simple test with FOO
This is a simple test with BAR
will be converted to
This is a simple te...
This is a simple te...
Meaning that data will be lost and the users won't be able to distinguish between the two strings.
After thinking a little bit more I figured out the best possible solution is to abbreviate as little characters of as little words as possible, always in accordance with the max length constraint.
With such a behaviour the previous example would be converted to
This is a sim. te. with FOO
This is a sim. te. with BAR
I figured out I'll ask here for an alternative/better solution, before coding this.
Also, if there isn't any better alternative, what things should I keep in mind while implementing this? Can you give me any tips?
I have a few thoughts... which may or may not meet your needs. To begin, here are some additional forms of abbreviation that you may be able to programatically implement.
Remove Vowels
If you remove vowels, you may be able to abbreviate words within the desired lengths, and be slightly more readable. Removing vowels is an acceptable form of abbreviation. Keep in mind, you will need to keep the first and last letter of the word even if they are vowels. organization = orgnztn
Use Abbreviation API
https://Abbreviations.com has an API with abbreviations. This might be useful for abbreviating longer words. For example, to find the abbreviation of "organization": https://www.abbreviations.com/abbreviation/organization abbreviates as ORG
It appears this user has attempted to do this in python. If you know you will have frequent phrases, you can create a dictionary of the abbreviated form.
Unfortunately, no matter where you truncate the data, there is a chance that two strings will end up looking the same to the end user. You could do some string comparison to determine where the differences are, then write some logic to truncate characters in other locations.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Suppose I have a data as follows,
data['sentences']
This is a sentence
Donald Trump
Machine Learning
Python is good
I want to search for pattern of characters and if we find one, need to remove that word which contains the characters.
Suppose I want to remove words with "enc" , "ood" and "ump", the output should be,
data['sentences']
This is a
Donald
Machine Learning
Python is
I tried the following where I used re.sub,
re.sub("enc", "", y)
But this is giving output like, This is a sente . I am not sure how to remove the entire word.
Can anybody help me in doing this is python? I want to find the efficient way to do this because, I want to run this for nearly 1 Billion records using pyspark. Can anybody help me in doing this?
Thanks
Add iterations before and after the identifier:
re.sub(r'\w*enc\w*', '', y)
That would replace with blank all the alphanumeric characters along with the specified string (i.e. the word it is contains within).
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
What I mean by the title is how can I transform this:
Example Example Example Example Example Example Example Example Example Exam
ple Example
Into this:
Example Example Example Example Example Example Example Example Example
Example Example
Any ideas?
Use the textwrap module.
From the docs:
textwrap.wrap(text[, width[, ...]])
Wraps the single paragraph in text (a string) so every
line is at most width characters long. Returns a list
of output lines, without final newlines.
You would need to get the width of the thing you are printing to. (Probably the terminal). And then keep track of the length of string as you add the words. If the word would make python print it over two lines use a line break before it. This link is for getting terminal width How to get Linux console window width in Python