1

lets say we have a very big string. and we want to display only 100 characters in the stdout. and the remaining will go to the next line. my first method will be using index slicing and set the startIndex to increment by 100 and so on:

while (veryLargeStringLength/100 > 0):
    startIndex = 0
    veryLargeString[startIndex :startIndex+100]
    startIndex += 100
    veryLargeStringLength -= 100

Is this the most efficient way to do this? Any other optimization method? I would say that there is no point in cosidering multiThreaded or multiprocessing in this scenario?

Thanks

3
  • 2
    Never put startIndex = 0 inside the while loop Commented Jun 4, 2013 at 4:21
  • 2
    I'd advise you to time your code before trying to optimize it. In this case the operation is much faster than printing no almost no matter how you do it, and writing it clearly (as in Ashwini's answer below) is more important (also: just as fast). Commented Jun 4, 2013 at 4:34
  • @U2EF1 +1. Only worry about optimizing code if you are sure that it is not working fast enough. There is almost always a more efficient way to do things, but why rack your brain over it, if the current approach is suitable for your immediate needs? Commented Jun 4, 2013 at 4:51

2 Answers 2

4

Something like this:

n = 100
for i in xrange(0, len(strs), n):
    print strs[i:i+n]

working version of your code:

veryLargeStringLength = len(strs)
startIndex = 0                     #this should be before the loop
while veryLargeStringLength > 0:   #use just >0, no need of division
    print veryLargeString[startIndex :startIndex+100]
    startIndex += 100
    veryLargeStringLength -= 100
Sign up to request clarification or add additional context in comments.

3 Comments

I think while very_large_string_length > 0: would be much more readable and in accordance with the PEP8's recommendations: python.org/dev/peps/pep-0008/#other-recommendations
isn't better to use veryLargeStringLength > startIndex: instead without decreasing of veryLargeStringLength
thank you for fixing my incomplete codes. Actually I have tried using the xrange. however it is slower than the normal implementation. will it be feasible to use multithread or multiprocessing in this type of scenario?
0

You can use python built-in method range(start, stop[, step]) to achieve this. Start with 0, Stop at the end of the string (i.e. length of the string) and traverse in the steps of 100

for group_of_char in [veryLargeString[i:i+100] for i in range(0, len(veryLargeString), 100)]:
    print group_of_char

Or better convert this logic into a method and call as suited

def slice_string_in_n_size_pieces(input_string, n_size):
    return [input_string[i:i+n_size] for i in range(0, len(input_string), n_size)]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.