0

I was trying to solve up a problem that was going on cause my IDE could not retain a sequence of numbers cause of the range function which works as so.

And i made a Previous question about it so this is a follow-up to the question. Here's my list comment on the previous question.

I actually made some adjustments by adding a line; 'My_list = list(range(100)) before applying your code so it actually worked. But it combines the answers without commas, for example 10 does this '0123456789' instead of '0,1,2,3,4,5,.....8,9'. any suggestions?

I decided to post this question not to allow the other question go out of context (as i was advised to).

Any suggestions?

Laurent LAPORTE
  • 21,958
  • 6
  • 58
  • 103

2 Answers2

0

You just need to insert a comma after printing each number:

my_list = list(range(100))

with open("output.txt", "w") as o:
    for i in range(len(my_list)):
        o.write("%d," % my_list[i]) # Here, after '%d' you can place a comma, or any text you want
molfo
  • 75
  • 7
  • I don't understand. – Dave Kent Nov 21 '20 at 03:21
  • In the original code you execute `o.write("%d" % my_list[i])`, which prints one number after the other without any separation between them. If you want to add a comma after each number you have to user `o.write("%d," % my_list[i])` (i.e. you add a "," after the %d) – molfo Nov 21 '20 at 07:47
  • Can i get your email to send a question? – Dave Kent Nov 21 '20 at 09:25
0

You need to understand how strings works in Python.

Strings are constants (literals) kept in a closed bucket. In official docs you can find that "Strings are immutable sequences of Unicode code points".

But programmers need to change or manipulate text in a programmable way. In your case you want:

"[x1][space][comma][x2][comma]...[xn][space][comma]" where "xn" is a number, and " ," is constant.

In order to achieve this, in a programmable way, programmers can use "masks" to tell the software where they want to place their changes. One can use string format operators:

"%d , %f" %(my_first_integer, my_float)
[0][1][2][3][4][\0]
# Hey Python, return a string, using the above template, 
# but place useful stuff where you find magic keywords.

Which means:

  • Create a 6 positions sequence;
  • In [0], place my_integer of type int converted into chr;
  • In [1], copy " ";
  • In [2], copy ",".
  • In [3], copy " ";
  • In [4], place my_float of type float converted into chr;
  • In [5], place "\0" so the string is over. (Automatically placed in Python)

There are other ways to do this, i.e., the string object has a handy method called formatto handle this construction:

my_integer = 2
my_string = "{0}*pi = {1}".format(my_integer, my_integer*3.14)
print(my_string)

# 2*pi = 6.28

The programmer will achieve the same final result using one or another startegy.

In Python, as well as in other languages, one can combine strings, concatenate, get sub-strings and so on, using specific methods and/or operators.

In order to keep readability you maybe (I guess) want to place each value in a line. In strings you can use special characters like \n for new lines.

my_list = list(range(100))

# ... useful code here and there ...

with open("output.txt", "w") as o:
    o.write("My list:\n")
    o.write("\tSize: {0}\n\n".format(len(my_list)))
    o.write("\t----start----\n")
    for i in range(len(my_list)):
        o.write("%d\n" % my_list[i])
    o.write("\n\t----end----\n")

# That writes:
# My list:
#     Size: 100
#
#     ----start----
# 0
# 1
# 2
# 3
...
# 99
#
#    ----end----

Remember, this is not a comprehensive guide, but a layman one. I'm skipping a lot of boring words and technical details that you'll better find in Python books and courses.