Dear members,

I need to write a small loop to generate the following:

0 0 0 0 .... 0
1 1 1 .... 1
2 2 .... 2
3 .... 3
4
:
299

I think it needs a double loop but since i am still beginner in python i am struggling to work it out.

Could you please comment or suggest an easy way?

Thanks very much in advance.

Nicholas

Recommended Answers

All 7 Replies

Member Avatar for masterofpuppets

hi,
it depends on how many numbers you want in a row :) And yeah you need a double loop for this.

Here's a hint:

for i in range( number of rows ):
    for r in j in range( number of numbers per row ):
        print j,
    print

try to be more specific about what you want to do and put your code here so that we can see what you've done so far and give you advice :)

Hi,

Sorry but i have just realised that it does what i wrote, does not appear correct for some reason.

ok

for i in range(0, 20, 1):
for j in range(1, i, 1):
print j

the outcome print:

0 0 1 0 1 2 0 1 2 3 ....

What i need to have is the values like that on each line

0
0 1
0 1 2
0 1 2 3
.....
0 1 2 3 4 .... maxValue

I hope that this is more clear

Thanks very much for your reply.

Nicholas

Member Avatar for masterofpuppets

thanks, this makes it better :)

right, so for your first loop the range should be range( maxValue ) or range( maxValue + 1 ) if you want to include maxValue in the output.
In the second loop wou want to pring all the numbers up to the value of the counter for the first loop. Sorry if this sounds confusing, here's what I mean:

maxValue = input( "Enter a value >> " )

# Here you start from 1 so that in the second loop '0' is printed once
# You need to add 2 to maxValue because you start counting from 1 and the range function
# doesn't include the second parameter, so if you had maxValue + 1 and maxValue is
# 20, say, the loop will continue until i reaches 19, 20 is not inicluded.
for i in range( 1, maxValue + 2 ):
    for j in range( i ):
        print j,
    print

# output:
>>> 
Enter a value >> 10
0
0 1
0 1 2
0 1 2 3
0 1 2 3 4
0 1 2 3 4 5
0 1 2 3 4 5 6
0 1 2 3 4 5 6 7
0 1 2 3 4 5 6 7 8
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9 10
>>>

the comma after the first print statement means that the next print is on the same line and the print after the second loop finished moves the marker one line down :)

hope this helps :)

Fantastic,

thanks very much.....

Nicholas

Hi again,

I have a file with lines like that:

$ns at 0.1 "$traf0 start"
$ns at 0.1 "$traf1 start"
$ns at 0.1 "$traf2 start"
$ns at 0.1 "$traf3 start"
$ns at 0.1 "$traf4 start"
$ns at 0.1 "$traf5 start"
$ns at 0.1 "$traf6 start"
$ns at 0.1 "$traf7 start"
$ns at 0.1 "$traf8 start"
$ns at 0.1 "$traf9 start"
$ns at 0.1 "$traf10 start"
….
maxValue number of lines

I am reading the file line by line and store each line in a list called startList

I have made a loop that generates files
File0.tcl
File1.tcl
File2.tcl
File3.tcl
…..
File(maxValue).tcl

The files are generated via:


for i in range( 0, maxValue, 1 ):
    for j in range( i ):
        tclFiles = "File%d.tcl" % i
        out = open(tclFiles, "w")
        out.close()

I am struggling to automatically include in a loop the startList to write the lines of the file in the generated *.tcl as shown below:

The File0.tcl will contain:
$ns at 0.1 "$traf0 start"

The File1.tcl ->
$ns at 0.1 "$traf0 start"
$ns at 0.1 "$traf1 start"

The File2.tcl ->
$ns at 0.1 "$traf0 start"
$ns at 0.1 "$traf1 start"
$ns at 0.1 "$traf2 start"

File3.tcl ->
$ns at 0.1 "$traf0 start"
$ns at 0.1 "$traf1 start"
$ns at 0.1 "$traf2 start"
$ns at 0.1 "$traf3 start"

Until the maxValue is reached.

I assume is just a small cleverly setup loop. I have tried several things that didn’t work.

It would be much appreciated if you could help on this one.

Again thanks very much in advance.

Nicholas

There is nothing very clever here. You must learn to think about algorithms. You want to output a series of blocks of lines; each block is indexed by the the number k in file<k>.tcl. So the structure of your algorithm is (this is pseudo code, not python)

for k in 0, 1, 2, ..., maxValue -1:
    print block k

Now you must find how to print block k. You must first print a header line with the file name, and then a series of lines indexed by the number p in $traf<p>. This index goes from 0 to k, so your structure is now

for k in 0, 1, 2, ..., maxValue -1:
    print header line with file<k>tcl
    for p in 0, 1, ..., k:
        print line with $traf<p>

All you have to do is translate this to python.

Thanks very much...

Nicholas

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.