Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
November 25th, 2012, 02:16 AM
 anr180
Registered User

Join Date: Nov 2012
Posts: 3
Time spent in forums: 53 m 12 sec
Reputation Power: 0
Multiplying Elements of Lists Together

Hello Everyone,

I'm trying to write a program to help me understand a game with very complicated probability involved. Because the probability is so complicated, I'm trying to "brute force" the game by using a program to calculate every possible outcome for me.

I'm trying to multiply all the elements of one list to all the elements of another list and form a new list from it. For example:

Let a,b,c be some numbers. (NOT STRINGS! They're numbers, as in floats. I'm only using a,b,c to make the desired output more clear.)

List1 = [1,2,3]
List2 = [a,b,c]

Desired Output = [1a,2a,3a,1b,2b,3b,1c,2c,3c]

I'm a newby with Python. I've only been studying it for about 2.5 weeks. I tried using for loops, but that's posing an issue because I believe I'd need to loop through two lists with the same line of code. As far as I know, that's not possible. Putting a for loop inside of another won't work, unless I'm mistaken? I decided to try combining two for loops as follows. Example:

Code:
```list1 = [1,2,3]
list2 = [4,5,6]
list3 = []
list3.append(m*i for m in list1 for i in list2)
print list3```

The resulting output is:

Code:
`[<generator object <genexpr> at 0x021D5990>]`

That's not an error. That's the actual output. I have absolutely no idea what it means but it's certainly not a list like I want!

Thanks in advance for any and all help. Remember, I'm a newby! I don't know much yet. D:

#2
November 25th, 2012, 02:23 AM
 anr180
Registered User

Join Date: Nov 2012
Posts: 3
Time spent in forums: 53 m 12 sec
Reputation Power: 0
I also tried the following:

Code:
```list1 = [1,2,3]
list2 = [4,5,6]
list3 = []
for i in list1:
list3.append(m*i for m in list2)
print list3```

This resulted in:

Code:
`[<generator object <genexpr> at 0x021D5990>, <generator object <genexpr> at 0x021D5BE8>, <generator object <genexpr> at 0x021D5C38>]`

#3
November 25th, 2012, 05:16 AM
 SuperOscar
Contributing User

Join Date: Jul 2007
Location: Joensuu, Finland
Posts: 420
Time spent in forums: 1 Week 18 h 35 m 57 sec
Reputation Power: 66
This is where zip() comes in handy.

Code:
`list3 = [a * b for a, b in zip(list1, list2)]`

Correction: Oops, sorry, read you wrong. What you want cannot – as far as I can see – to be solved with list comprehensions, so:

Code:
```list3 = []
for a in list1:
for b in list2:
list3.append(a * b)```
__________________
My armada: openSUSE 13.1 (home desktop, home laptop), Ubuntu 13.04 (work laptop), FreeBSD 9.2 (server), Mythbuntu 12.04 LTS (HTPC), Bodhi Linux 2.0 & Windows 7 Ultimate (test desktop), Android 4.1.2 (tablet)

Last edited by SuperOscar : November 25th, 2012 at 05:26 AM.

#4
November 25th, 2012, 09:40 AM
 Dietrich
Contributing User

Join Date: Feb 2005
Posts: 574
Time spent in forums: 5 Days 4 h 6 m 36 sec
Reputation Power: 63
Actually you were on the right track. You didn't turn the generator object into a list with list():
Code:
```list1 = [1,2,3]
list2 = [4,5,6]
list3 = list(m*i for m in list1 for i in list2)
print list3

'''my result >>>
[4, 5, 6, 8, 10, 12, 12, 15, 18]
'''```
__________________
Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25

#5
November 25th, 2012, 12:32 PM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 4,151
Time spent in forums: 1 Month 3 Weeks 2 Days 8 h 22 m 16 sec
Reputation Power: 455
Cartesian product is the j verb { .
Code:
```   { 'abcd' ; '012345'
┌──┬──┬──┬──┬──┬──┐
│a0│a1│a2│a3│a4│a5│
├──┼──┼──┼──┼──┼──┤
│b0│b1│b2│b3│b4│b5│
├──┼──┼──┼──┼──┼──┤
│c0│c1│c2│c3│c4│c5│
├──┼──┼──┼──┼──┼──┤
│d0│d1│d2│d3│d4│d5│
└──┴──┴──┴──┴──┴──┘

{ 'ab' ; '012' ; 'ABCD'
┌───┬───┬───┬───┐
│a0A│a0B│a0C│a0D│
├───┼───┼───┼───┤
│a1A│a1B│a1C│a1D│
├───┼───┼───┼───┤
│a2A│a2B│a2C│a2D│
└───┴───┴───┴───┘

┌───┬───┬───┬───┐
│b0A│b0B│b0C│b0D│
├───┼───┼───┼───┤
│b1A│b1B│b1C│b1D│
├───┼───┼───┼───┤
│b2A│b2B│b2C│b2D│
└───┴───┴───┴───┘

1 2 3 */ 4 5 6   NB. table, or outer product
4  5  6
8 10 12
12 15 18

, 1 2 3 */ 4 5 6
4 5 6 8 10 12 12 15 18

```
__________________
[code]Code tags[/code] are essential for python code!

#6
November 25th, 2012, 01:58 PM
 anr180
Registered User

Join Date: Nov 2012
Posts: 3
Time spent in forums: 53 m 12 sec
Reputation Power: 0
Thank you so much everyone!

Now all I have to do is figure out what exactly is double counted and what isn't for some of the more complicated parts of this program. Then I can finish this up.

You guys rock.

#7
November 25th, 2012, 02:00 PM
 SuperOscar
Contributing User

Join Date: Jul 2007
Location: Joensuu, Finland
Posts: 420
Time spent in forums: 1 Week 18 h 35 m 57 sec
Reputation Power: 66
Quote:
 Originally Posted by Dietrich Code: `list3 = list(m*i for m in list1 for i in list2)`

Oh, I stand corrected.

 Viewing: Dev Shed Forums > Programming Languages > Python Programming > Multiplying Elements of Lists Together