Please help

Hi everyone i am new in python.
this is my code

import random
mylist = [(1,2,3),(4,5,6)]
yourlist = [(random.uniform(-1.0,1.0)),(random.uniform(-1.0,1.0)),(random.uniform(-1.0,1.0))]

newlist =[]

for i in range(len(mylist)):
for j in range(len(yourlist)):
newlist.append(mylist[i][j]*yourlist[j])

print newlist


But result is a long list but i want to have it as same as mylist two group in ()

[-0.19764502283518248, -0.55113076405225403, -0.71136863026775843, -0.79058009134072993, -1.3778269101306351, -1.4227372605355169]

First, invest in this book.

for now, the answer to your problem lies here and here.

Thankx for your answer but my problem is not floating point,
I want the result as [( , , )( , , )] not all of them in one list[ , , , , , , ,]

Hi, masoudmaya

I really can’t figure out what you are hoping your code will do. You will get much more useful answers if you describe the problem you code is trying to solve.

The following will group your results in to lists of 3. But the code is still badly written and not very pythonic.

import random
mylist = [(1,2,3),(4,5,6)]
yourlist = [(random.uniform(-1.0,1.0)),(random.uniform(-1.0,1.0)),(random.uniform(-1.0,1.0))]

newlist =[]

for i in range(len(mylist)):
    tempList = list()
    for j in range(len(yourlist)):
        tempList.append(mylist[i][j]*yourlist[j])
    newlist.append(tempList)

print newlist

Here are a couple of variations.

import random
mylist = [(1,2,3),(4,5,6)]

newlist = list()
for group in mylist:
    tempList = list()
    for value in group:
        tempList.append(value * random.uniform(-1.0,1.0))
    newlist.append(tempList)
print newlist
import random
mylist = [(1,2,3),(4,5,6)]
newlist = [ [value * random.uniform(-1.0,1.0) for value in group] for group in mylist]
print newlist

Keir

I was going to post a cool one, but Keir stole them all. :smiley:

I think what masoudmaya wanted was something like:

import random
mylist = [(1,2,3),(4,5,6)]
yourlist = [(random.uniform(-1.0,1.0)),(random.uniform(-1.0,1.0)),(random.uniform(-1.0,1.0))]
newlist = list()
for i in range(len(mylist)):
newlist.append((mylist[i][0] * yourlist[0], mylist[i][1] * yourlist[1], mylist[i][2] * yourlist[2]))
print newlist