The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
Multiplying Elements of Lists Together
Discuss Multiplying Elements of Lists Together in the Python Programming forum on Dev Shed. Multiplying Elements of Lists Together Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

November 25th, 2012, 01:16 AM
|
|
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:
|

November 25th, 2012, 01:23 AM
|
|
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>]

|

November 25th, 2012, 04:16 AM
|
|
Contributing User
|
|
Join Date: Jul 2007
Location: Joensuu, Finland
|
|
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 12.3 (home desktop, laptop, work desktop), Ubuntu 12.04 LTS (mini laptop), Debian GNU/Linux 7.0 (server), Mythbuntu 12.04 LTS (HTPC), Bodhi Linux 2.0 & Windows 7 Ultimate (test desktop), FreeBSD 9.1 (test server)
Last edited by SuperOscar : November 25th, 2012 at 04:26 AM.
|

November 25th, 2012, 08:40 AM
|
 |
Contributing User
|
|
|
|
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
|

November 25th, 2012, 11:32 AM
|
 |
Contributing User
|
|
|
|
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!
|

November 25th, 2012, 12:58 PM
|
|
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.
|

November 25th, 2012, 01:00 PM
|
|
Contributing User
|
|
Join Date: Jul 2007
Location: Joensuu, Finland
|
|
Quote: | Originally Posted by Dietrich
Code:
list3 = list(m*i for m in list1 for i in list2)
|
Oh, I stand corrected.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|