We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,664 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

finding columns of a row in a list

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?

4
Contributors
8
Replies
10 Hours
Discussion Span
1 Year Ago
Last Updated
9
Views
straylight
Light Poster
28 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

use enumerate:

for column, value in enumerate(row):
pyTony
pyMod
Moderator
6,299 posts since Apr 2010
Reputation Points: 879
Solved Threads: 986
Skill Endorsements: 26

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.

straylight
Light Poster
28 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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

pyTony
pyMod
Moderator
6,299 posts since Apr 2010
Reputation Points: 879
Solved Threads: 986
Skill Endorsements: 26

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 ?

Gribouillis
Posting Maven
Moderator
3,101 posts since Jul 2008
Reputation Points: 1,130
Solved Threads: 761
Skill Endorsements: 11

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.

straylight
Light Poster
28 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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)
pyTony
pyMod
Moderator
6,299 posts since Apr 2010
Reputation Points: 879
Solved Threads: 986
Skill Endorsements: 26

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

straylight
Light Poster
28 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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.

woooee
Posting Maven
2,703 posts since Dec 2006
Reputation Points: 827
Solved Threads: 779
Skill Endorsements: 9

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.3466 seconds using 2.67MB