1,105,226 Community Members

finding columns of a row in a list

Member Avatar
straylight
Light Poster
28 posts since Feb 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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?

Member Avatar
pyTony
pyMod
6,103 posts since Apr 2010
Reputation Points: 818 [?]
Q&As Helped to Solve: 1,056 [?]
Skill Endorsements: 42 [?]
Moderator
Featured
 
0
 

use enumerate:

for column, value in enumerate(row):
Member Avatar
straylight
Light Poster
28 posts since Feb 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Member Avatar
pyTony
pyMod
6,103 posts since Apr 2010
Reputation Points: 818 [?]
Q&As Helped to Solve: 1,056 [?]
Skill Endorsements: 42 [?]
Moderator
Featured
 
0
 

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

Member Avatar
Gribouillis
Posting Maven
3,452 posts since Jul 2008
Reputation Points: 1,140 [?]
Q&As Helped to Solve: 883 [?]
Skill Endorsements: 18 [?]
Moderator
 
0
 

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 ?

Member Avatar
straylight
Light Poster
28 posts since Feb 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Member Avatar
pyTony
pyMod
6,103 posts since Apr 2010
Reputation Points: 818 [?]
Q&As Helped to Solve: 1,056 [?]
Skill Endorsements: 42 [?]
Moderator
Featured
 
0
 

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)
Member Avatar
straylight
Light Poster
28 posts since Feb 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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!!

Member Avatar
woooee
Posting Maven
2,792 posts since Dec 2006
Reputation Points: 783 [?]
Q&As Helped to Solve: 836 [?]
Skill Endorsements: 12 [?]
 
0
 

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.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: