map command ?

print (’ Map None ')
print ()

m = [1,2,3]
n = [1,4,9]
new_tuple =(map(None, m, n))
print (‘new_tuple =’,new_tuple )

trying to make this works but get error

i cannot get the result just the memory address

is there a new way to use this command now ?

i need this to work for some loop like this which is not working right now

for x, y in map(None, X, Y):

Sx = Sx + x
Sy = Sy + y
Sxx = Sxx + xx
Syy = Syy + y
y
Sxy = Sxy + x*y

thanks

new_tuple =(map(None, m, n))

What the hell?

map requires something it can call (function reference, lambda) and a sequence. You supply neither of both. And you also don’t create a tuple, but save a map object to “new_tuple” variable (the parathesis are ignored).

Here’s an example how to use it:

def square(value):
    return value * value

new_tuple = tuple(map(square, (1,2,3)))
#(1, 4, 9)

And note that

(map(square, (1,2,3)),)
#(<map object at 0x000000000ADEE8D0>,)

won’t cast the map object to tuple, but create a tuple with a single entry (the map object).

yes i did find this example with square
and it works fine

it is the other method using

for x, y in map(None, X, Y):

which is not working !

how can that one work with a for loop

i have a code snippet with 2 for loop and not working right now!

did not find any example for it on python forum!

thanks

Dunno what is supposed to be calculates, but it’s definately not possible as stated:

Sxx = Sxx + x*x

How would you assign Sxx to itself without setting an initial value?

Maybe something like this?

for x2, y2, xx, yy, xy in map(lambda x, y: (x+x, y+y, x*x, y*y, x*y), m, n):
    print("x + x =", x2)
    print("y + y =", y2)
    print("x * x =", xx)
    print("y * y =", yy)
    print("x * y =", xy)
    
#~ x + x = 2
#~ y + y = 2
#~ x * x = 1
#~ y * y = 1
#~ x * y = 1
#~ x + x = 8
#~ y + y = 4
#~ x * x = 16
#~ y * y = 4
#~ x * y = 8
#~ x + x = 18
#~ y + y = 6
#~ x * x = 81
#~ y * y = 9
#~ x * y = 27

here is a more complete code snippet




##
	
def vdot(x, y):
	
	
#	if x == 17 :raise ValueError, ’17 is a bad number’

	if len(x) != len(y):
		pass
#		raise ValueError, 'unequal length'
	sum = 0
	
	for a, b in map(None, x, y):
		sum = sum + a * b
	
	return sum
	
	
###
	
def vdot1():
	
#	x, y
	x=[1,2,3,4,5]
	y=[1,2,3,4,5]
	
	
	v1=vdot(x, y)
	
	print (' V1=',v1)
	print (' V2=',v2)
	print ()
	print ('Result of Vdot =',v1)
	
###
	


there is also the line with
if x == 17 :raise ValueError, ’17 is a bad number’

which is not working
did not find any doc on wjy it is refusing the ValueError method

it is suppose to work but might be some changes to this command somehow!

thanks

It’s not py3k:
http://getpython3.com/diveintopython3/porting-code-to-python-3-with-2to3.html

Proper syntax is:
raise ValueError(‘17 is a bad number’)

Somewhat traditional:

sum = 0

length = min(len(a), len(b))

for i in range(length):
    sum += a[i] * b[i]

Preferred:

sum(aa*bb for aa, bb in zip(a,b))