Title says it all. I am looking for a way to find the values of a column within a row. I am unsuccessful at being about to create a for loop that searches each value in the row and inputs a value if it is x.
For example:

there are 3 rows
if row is 0:
   do x 
elif row is 1:
   do y
else:
   for columns in row:
              if column is the first in row:
                         do x
              if column is the last in row:
                         do y

Any ideas?

Recommended Answers

All 8 Replies

use enumerate:

for column, value in enumerate(row):

use enumerate:

for column, value in enumerate(row):

I was thinking of using enumerate but for what I am using I do not think it will work because the values in the rows are ints. I should have mentioned that sorry.

What have the values to do it. Give example how it does no work and what you would like to get instead.

Your pseudo code is cryptic. In the 2 first ifs, row seems to be an integer, then in the else part it's an iterable, and you don't say what 'the first in row' and 'the last in row mean'. Could you give an example of what the row variable contains and what you are searching in this value ?

Thanks for the responses and yes I can provide some more info. Basically it will ask for the height

h=input("enter height")

then I have a for loop that produces a range from 0 to the height

for row in range(0,height)

and then a few if statements that check if the row is the first row and if the row is the second

if row==0:
       newrow.append(1)
elif row==1:
       newrow.append(1,1)

then an else statement that has a for loop that checks each value in the 3rd+rows and if it is the first column in the row it needs to print 1 and if it is the last column in the row it needs to print 1. Here is where I am confused as to how to put the for statement but it needs to be something like for each column in the new rows the first column needs to be a 1 and the last needs to be a 1.
like so
[1]
[1,1]
[1,2,1]
[1,2,2,1]
etc

Sorry for the complete lack of communication on my end. My python skills are amateur at best.

Looks similar to pascal triangle, maybe you could adapt this code, I do not however use any indexes:

pascal = []
row = []

for count in range(int(input('Height of triangle: '))):
    new_row = []
    prev = 0
    for val in row:
        new_row.append(val+prev)
        prev = val
    new_row.append(1)
    row = new_row
    print row
    pascal.append(row)

Looks similar to pascal triangle, maybe you could adapt this code, I do not however use any indexes:

pascal = []
row = []

for count in range(int(input('Height of triangle: '))):
    new_row = []
    prev = 0
    for val in row:
        new_row.append(val+prev)
        prev = val
    new_row.append(1)
    row = new_row
    print row
    pascal.append(row)

Thanks for the reply. The way I am having to do it is through a series of if and else statements but I'll see if I can manipulate your version and adapt it to mine. Thanks!!

You have to know what you are coding before you can write the code. What solution are you trying to write. Just throwing code against the wall to see what sticks doesn't work. If you draw it out, it becomes somewhat clear.

Hint: it has to do with the length being odd or even.

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.