Home > OS >  How to remove the trailing , following a end=','
How to remove the trailing , following a end=','

Time:06-29

New to Python & the community. Navigating different exercises and I'm having trouble finding the solution. I need to remove the trailing, that follows when using end=','

Any wisdom or guidance is very appreciated!

low = 10000
up = 10050

for num in range(low, up   1):
   if num > 1:
       for i in range(2, num):
           if (num % i) == 0:
               break
       else:
           print(num,end=',')

CodePudding user response:

It looks like you're trying to print prime numbers in a given range. Arguably, mixing discovery of prime numbers and printing them is what causes the problem. With proper decomposition, this problem won't exist:

def generate_primes(low, up):
    for num in range(max(low, 2), up 1):
       if all(num % i for i in range(2, num)):
           yield num

print(*generate_primes(low, up), sep=',')

As a positive side effect, you can now reuse prime generator in other parts of the program which don't require printing.

Also note that checking all numbers up to num is not necessary - if the number is composite one of the factors will be less or equal to sqrt(num). So, a faster prime generator would be something like:

def generate_primes(low, up):
    for num in range(max(low, 2), up 1):
       if all(num % i for i in range(2, int(num**0.5   1))):
           yield num

CodePudding user response:

Try this :

low = 10000
up = 10050

nums = []

for num in range(low, up   1):
   if num > 1:
       for i in range(2, num):
           if (num % i) == 0:
               break
       else:
           nums.append(str(num))

a = ",".join(nums)

print(a)

Essentially, just add all the elements that will be outputted in a list and then use the join function to convert them into a string and print it.

CodePudding user response:

This code could work:

low = 10000
up = 10050

ret = ""

for num in range(low, up   1):
   if num > 1:
       for i in range(2, num):
           if (num % i) == 0:
               break
       else:
           if num != up:
             ret  = str(num)   ","

print(ret[:-1])

Output:

10007,10009,10037,10039

Instead of printing everything out immediately, we add it into a string, then cut off the last character (trailing comma), from that string, and print it out.

CodePudding user response:

Solution

import sys
low = 10000
up = 10050
firstValuePrinted = 0
for rootIndex, num in enumerate(range(low, up   1)):
  if num > 1:
     for subIndex, i in enumerate(range(2, num)):
         if (num % i) == 0:
             break
     else:
       print("", end = "," if firstValuePrinted==1 else "")
       firstValuePrinted = 1
       print(num, end = "")
       sys.stdout.flush()

Explanation

Since we can't determine on which number num the loop will exit we can't exactly know the end condition. But we can figure out the starting number.

So based on that logic, instead of printing the "," at the end we actually print it at the start by using a variable to maintain if we have printed any values or not in the above code that is done by firstValuePrinted.

sys.stdout.flush() is called to ensure the console prints the output right away and does not wait for the program to complete.

Note: Don't forget to import sys at the start of the file.

Benefits when compared to other answers

This type of implementation is useful if you want to output the num variable continuously so as to not have to wait for all the numbers to be calculated before writing the value.

If you put all the values into an array and then print, you actually need large memory for storing all the numbers (if low and up is large) as well as the console will wait till the for loop is completed before executing the print command.

Especially when dealing with prime numbers if the low and up variables are far apart like low=0 and up=1000000 it will take a long time for it to be processed before any output can be printed. While with the above implementation it will be printed as it is being calculated. While also ensuring no additional "," is printed at the end.

  • Related