Array , extract all index 2 elements

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 763 Views - Last Post: 21 January 2014 - 12:14 PM Rate Topic: -----

#1 giny28  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 21
  • Joined: 07-January 14

Array , extract all index 2 elements

Posted 07 January 2014 - 12:36 PM

Hello,

I would like to encode a column2 () function which allows to extract all index M 2 elements , and must then generalize a
column () function to any column of a matrix of n rows by m columns . " I must use python langage .I am a biginner in python

The objective is to extract the index or index elements 2 of each row 2 eg 3 , 6 .... .
But col2 () function will it be binary ? 2 arguments: the matrix and the index.
You must use 2 loops ... well I think!

Already for encoding an array of n rows by m columns, eg matrix (2,3) gives [ [ 1 , 2, 3 ] , [ 4 , 5, 6 ] ] .
>>> def matrix (line, col):
    m = [ ]
    for e in range ( line):
        z = [ ]
        for o in range ( col):
            z + = [ 3 * e + o + 1]
        m + = [z]
    return m


_ Also, I can't use the append () method , append is supposed to add an item to the end of such a list:

I know, I didn't use the append () method to the code PX10 -1, but I found something interesting :

  >>> m = []
     m.append >>> (range (3) )
     m.append >>> (range (3, 6)
     m.append >>> (range (6, 9) )

     >>>m
     [ [0 , 1, 2 ], [3 , 4, 5 ], [6 , 7, 8 ] ] # there, it starts at 0 , but in my case here must begin with 1.



Thank you in advance!

Is This A Good Question/Topic? 0
  • +

Replies To: Array , extract all index 2 elements

#2 DK3250  Icon User is offline

  • D.I.C Head

Reputation: 27
  • View blog
  • Posts: 105
  • Joined: 27-December 13

Re: Array , extract all index 2 elements

Posted 07 January 2014 - 03:02 PM

I hope I understood your question correct.

Here is a small working program that you probably can use:

def matrix (row, col):
    m = [ ]
    for e in range (row):
        z = [ ]
        for o in range (col):
            z.append(3 * e + o + 1)
        m.append(z)
    return m

def extract(m, col):
    for item in m:
        print item[col]
    

m = matrix(2,3)
print m

extract(m, 2)



I've modified your matrix function a bit and added a simple extract function.
Let me know if this is of any help.
Was This Post Helpful? 0
  • +
  • -

#3 giny28  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 21
  • Joined: 07-January 14

Re: Array , extract all index 2 elements

Posted 08 January 2014 - 12:46 AM

Sorry , your first code works really good , but the second one , no .I have to use 2 arguments with function col2 without using print or the method numpy .I have to use two loops , with 'range' or 'append' , extract an element from a column .I don't have a clue , but i know that i must put a matrix in past argument ....
Anyway , thank you ^^
Was This Post Helpful? 0
  • +
  • -

#4 giny28  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 21
  • Joined: 07-January 14

Re: Array , extract all index 2 elements

Posted 08 January 2014 - 01:05 AM

Sorry, But the option with 'print' is a good way to resolve this exercices .
Was This Post Helpful? 0
  • +
  • -

#5 DK3250  Icon User is offline

  • D.I.C Head

Reputation: 27
  • View blog
  • Posts: 105
  • Joined: 27-December 13

Re: Array , extract all index 2 elements

Posted 08 January 2014 - 03:19 AM

Hi,

I have 'polished' the code a bit, making it more general:

def matrix (row, col):
    m = [ ]
    for e in range (row):
        z = [ ]
        for o in range (col):
            z.append(col * e + o + 1)
        m.append(z)
    return m

def extract(m, col):
    for row in m:
        print row[col]
    
var1 = input("enter number of rows ")
var2 = input("enter number of columbs ")

m = matrix(var1,var2)
print m

extract(m, 2)




I use print statement only to document the algorith progress - you will need to implement you own code.
If the extract function is not what you need, please try to explain again. I will be glad to help.

From a programming point of view, I am a little reluctant with the constant (the value 2) in the function call. But until I better understand the purpose of the code, I can do no better.
Was This Post Helpful? 0
  • +
  • -

#6 giny28  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 21
  • Joined: 07-January 14

Re: Array , extract all index 2 elements

Posted 08 January 2014 - 03:51 AM

thanks !I have to use 2 arguments with function col2 without the method numpy .Also ,I have to use two loops , with 'range' or 'append' , extract an element from a column .I don't have a clue , but i know that i must put a matrix in past argument .It's like using the first code with the result of matrix .

Excuse me ,I am a bigginer in python . I don't know yet what mean 'input' , usually , a function have to 'return' a value , but you don't use the term 'return' in the end of the code ...
The goal is to extract a liste with element of index 2 and the result will be for example : O = [3,6 ....]
You see here in O =[ [ 1 , 2, 3 ] , [ 4 , 5, 6 ] ] , i have put the element to extract in bold ^^
Was This Post Helpful? 0
  • +
  • -

#7 DK3250  Icon User is offline

  • D.I.C Head

Reputation: 27
  • View blog
  • Posts: 105
  • Joined: 27-December 13

Re: Array , extract all index 2 elements

Posted 08 January 2014 - 04:22 AM

Hi,

'input' simply ask the user for keyboard input
Try run the code and you will see!

I guess you need the output list for something, so I changed the program to make it available in the main:

def matrix (row, col):
    m = [ ]
    for e in range (row):
        z = [ ]
        for o in range (col):
            z.append(col * e + o + 1)
        m.append(z)
    return m

def extract(m, col):
    outputList = []
    for row in m:
        outputList.append(row[col])
    return outputList
    
var1 = input("enter number of rows ")
var2 = input("enter number of columbs ")

m = matrix(var1,var2)
print m

outputList = extract(m, 2)
print "Output = ", outputList




Again, I just print the output, but now from the main program.

I am glad to help - just keep asking.
Was This Post Helpful? 0
  • +
  • -

#8 giny28  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 21
  • Joined: 07-January 14

Re: Array , extract all index 2 elements

Posted 08 January 2014 - 10:35 AM

>>> def matrix (row, col):
m = [ ]
for e in range (row):
z = [ ]
for o in range (col):
z.append(col * e + o + 1)
m.append(z)
return m

>>> def extract(m, col):

File "<pyshell#10>", line 1
def extract(m, col):
^
Indentationerror: unexpected indent
>>> def extract(m, col):
outputList = []
for row in m :
outputList.append(row[col])
return outputList

>>> extract(2, 3)

Traceback (most recent call last):
File "<pyshell#17>", line 1, in <module>
extract(2, 3)
File "<pyshell#16>", line 3, in extract
for row in m :
TypeError: 'int' object is not iterable


I tried your first code , it works really good ^^ but the second i failed in testing , where is the problem ?
Was This Post Helpful? 0
  • +
  • -

#9 giny28  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 21
  • Joined: 07-January 14

Re: Array , extract all index 2 elements

Posted 08 January 2014 - 10:53 AM

i have done a new code using yours :

>>> def matrix (row, col):
	m = [ ]
	for e in range (row):
		z = [ ]
		for o in range (col):
			z.append(col * e + o + 1)
		m.append(z)
	return m

>>> def extract(m, col):
    elements=[]
    for row in m:
         elements += [i[col]]
    return elements


But i always fail to get the element with specific column , why ? :angel:
Was This Post Helpful? 0
  • +
  • -

#10 DK3250  Icon User is offline

  • D.I.C Head

Reputation: 27
  • View blog
  • Posts: 105
  • Joined: 27-December 13

Re: Array , extract all index 2 elements

Posted 08 January 2014 - 11:24 AM

Hi,

When adding an element to a list you need to use the 'append' method; you cannot use '+'.
'+' is for arithmetic or strings, - not for lists.

Further, you mix 'row' and 'i' in line 3 and 4.
So, if you change line 4 to:

elements.append(row[col])

it should work.

Another thing: If it is always, always, always col no. 2 you want to extract, then the function can be simplified. I made the function with the parameter 'col' to be able to change the position for extraction.

But for now, just change line 4 in the 'extract' function.
Was This Post Helpful? 0
  • +
  • -

#11 giny28  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 21
  • Joined: 07-January 14

Re: Array , extract all index 2 elements

Posted 08 January 2014 - 11:37 AM

Thanks for the correction! You know , the second part of this exercice is to do for others columns , could you simplify your second code without print and var1 ,var2 ?
Was This Post Helpful? 0
  • +
  • -

#12 giny28  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 21
  • Joined: 07-January 14

Re: Array , extract all index 2 elements

Posted 08 January 2014 - 11:46 AM

Here is the correction :

>>> def extract(m, col):
elements=[]
for row in m:
elements.append(row[col])
return elements
Was This Post Helpful? 0
  • +
  • -

#13 DK3250  Icon User is offline

  • D.I.C Head

Reputation: 27
  • View blog
  • Posts: 105
  • Joined: 27-December 13

Re: Array , extract all index 2 elements

Posted 08 January 2014 - 12:01 PM

ok - but hard coding constants in a program is really not a good way.
It is a bit contrary to the idea of programming.
A program should be a general code which is used with (new) specific constants from run to run.
Anyway:

def matrix (row, col):
    m = [ ]
    for e in range (row):
        z = [ ]
        for o in range (col):
            z.append(col * e + o + 1)
        m.append(z)
    return m

def extract(m, col):
    outputList = []
    for row in m:
        outputList.append(row[col])
    return outputList
    
m = matrix(2,3)
print m # this line is just to show the algoritm progress - can be removed

outputList = extract(m, 2)
print "Output =", outputList # this print can be substituted with other code




Please reveal the purpose of this - is it school work or leisure ?
Until now, we have concentrated on making the code work, can you explain how this code will be used ?
Was This Post Helpful? 0
  • +
  • -

#14 giny28  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 21
  • Joined: 07-January 14

Re: Array , extract all index 2 elements

Posted 08 January 2014 - 12:16 PM

This code will be used just to extract a element from an array , it's an exercise .It's really a pleasure to program , thanks for your help .
Was This Post Helpful? 0
  • +
  • -

#15 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5832
  • View blog
  • Posts: 12,685
  • Joined: 16-October 07

Re: Array , extract all index 2 elements

Posted 08 January 2014 - 01:38 PM

For the sake of learning, consider using list comprehensions. e.g
def matrix(rows, cols):
    return [ [ r * cols + c + 1 for c in range(cols) ] for r in range(rows) ]

def extract(m, col):
    return [ row[col] for row in m ]

m = matrix(2,3)
print m
print extract(m,2)



Also, a Matrix object could make sense here:
class Matrix(object):
    def __init__(self, rows, cols):
        self.rows, self.cols = rows, cols
        self.dat = [ [ r * cols + c + 1 for c in range(cols) ] for r in range(rows) ]

    def extract(self, col):
        return [ row[col] for row in self.dat ]

m = Matrix(2,3)
print m.dat
print m.extract(2)


Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2