-2

I'm working on my python script to count on each value that start from 0 to count it to 17.

Here is what I use:

row = range(0, 18)
print row

Result:

19:39:14 T:5816  NOTICE: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17]

I want to make the result like this:

>>> 0
>>> 1
>>> 2
>>> 3
>>> 4
>>> 5
>>> 6
>>> 7
>>> 8
>>> 9
>>> 10
>>> 11
>>> 12
>>> 13
>>> 14
>>> 15
>>> 16
>>> 17

Can you please tell me how I can split the values from the array to allow me to print each value like what I have show on above?

EDIT: You can see why I don't want to use the for loop, is because I want to generate the ids to use the variable outside the for loop and I want to get the list of elements from the arrays so I can store in a database. Otherwise it will be firing thousands of times.

#generating the program button ids
for generate_ids in range(3002, 3485):
    program_id.append(generate_ids)

#create the rows to count for the 69 program buttons
row = range(0, 484)
row += row
print row

#program_id[row]


for elem in programs_button:
    program_width.append(elem.getWidth())

#store the buttons in the database
#cur.execute("INSERT INTO buttons(button_ids, button_width)" + " VALUES(?, ?)", [program_id, program_width])

2 Answers2

0

You probably want to iterate over an array. This is usually made using for loop.

for number in row:
    print number

EDIT: You may also want to use more functional approach. You can map your array with a print function. print is a function in python 3, so if you are on python 2.7 (where print is language expression) you will need to import needed function using __futures__.

from __future__ import print_function
map(print, row)
Nebril
  • 3,153
  • 1
  • 33
  • 50
  • thank you but i don't want to use the for loop, because i want to get it to work with the variable `program_id` outside of the loop. are there any other ways i can use without the loop? –  Dec 23 '14 at 20:18
  • You may use `program_id` inside a `for` block if you have it in the same function scope. Read on python variable scopes here: http://stackoverflow.com/questions/291978/short-description-of-python-scoping-rules – Nebril Dec 23 '14 at 20:20
  • @Danny, what is `program_id` exactly? – Padraic Cunningham Dec 23 '14 at 20:22
  • @PadraicCunningham it is the actual id that i'm generating that start from 3002 all the way to the end 3485. –  Dec 23 '14 at 20:28
  • @Danny, I still cannot follow your logic. – Padraic Cunningham Dec 23 '14 at 20:31
  • @PadraicCunningham what you mean you cant follow my logic? –  Dec 23 '14 at 20:32
  • @PadraicCunningham i have already explains why I dont want to use the for loop with the other loop as it will get firing if i get them to work together. PLEASE SEE IN MY UPDATE POST IN MY QUESTION. –  Dec 23 '14 at 20:32
  • @Danny, I did and I still don't get it, your comments don't help either *#create the rows to count for the 69 program buttons* but you loop over a range(484), if you want to use the numbers as id's put the other code in the loop – Padraic Cunningham Dec 23 '14 at 20:35
  • @PadraicCunningham yeah but I can't use the loop `for elem in programs_button:` under the loop `for generate_ids in range(3002, 3485):` because it will keep firing like thousands of times. what i'm trying to do is to use the numbers as id to get the list of ids and also I want to get the list of width size from the arrays then store them in the database. Do you know how I can write in a proper way? –  Dec 23 '14 at 20:39
  • so you want `n` id's each time? – Padraic Cunningham Dec 23 '14 at 20:40
  • @PadraicCunningham i dont understand what you mean? –  Dec 23 '14 at 20:41
  • @Danny - do you want to join array contents into a string to put it in the database query? If yes, you can do it with `', '.join(row)`. – Nebril Dec 23 '14 at 20:42
  • @Nebril thank you, can you please show me an example what I can use to join array contents into a string to put it in the database query? –  Dec 23 '14 at 20:43
  • @Danny, so you just want a string like `"0,1,2,3..."`? – Padraic Cunningham Dec 23 '14 at 20:45
  • @Danny you can use the snippet I wrote in last comment: `', '.join(row)` will give you a comma-delimited string of values of `row` array. Then you can join this new string to your DB query. – Nebril Dec 23 '14 at 20:46
  • yes i do, but i want to print a string in each line without show `[0, 1, 2, 3...etc]` as i only want to show a string to something is like `0, `1`, `2`, `3`..etc. hope you get my point? –  Dec 23 '14 at 20:48
  • @Nebril how I can use program_id and program_width to join array contents into a string to put it in the database query? –  Dec 23 '14 at 20:49
  • @Nebril like this? `program_id = ', '.join[row]` and `program_width = ', '.join[row]` –  Dec 23 '14 at 20:51
  • @Danny I am not sure about what exactly do you want to do, but use round parentheses (`(` and `)`) instead of square ones (`[` and `]`) to call `join` function with `row` as it's argument. – Nebril Dec 23 '14 at 20:53
  • @Nebril Well I have the list of ids and width size that show in the list so I want to split them to allow me to print each of them at a time so I can put it in a database query. If you can show me an example how I can use `', '.join[row]` to get it to work with `program_id` and `program_width` then I would be grateful. –  Dec 23 '14 at 20:58
  • @Nebril i am waiting for your answer. I have the list of ids and width size that show in the list so I want to split them to allow me to print each of them at a time so I can put it in a database query. If you can show me an example how I can use `', '.join[row]` to get it to work with `program_id` and `program_width` then I would be grateful –  Dec 23 '14 at 21:32
  • @Danny sorry mate, but I have no idea what do you want to achieve. Try to break your problem into smaller ones and then ask question about each of them separately. You will get responses, as they seem to be quite easy. Cheers. – Nebril Dec 23 '14 at 21:37
0

You should read about Input and Output in Python here

As for your question, you could print it like this -

for number in range(18):
    print number
thomas
  • 1,133
  • 1
  • 12
  • 31
  • @thomas thank you but i dont want to use the for loop because i am using the variable outside of the for loop so how i can count the values to each line? something is like use the split method? –  Dec 23 '14 at 20:20