Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 25th, 2012, 01:16 AM
anr180 anr180 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 3 anr180 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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:

Reply With Quote
  #2  
Old November 25th, 2012, 01:23 AM
anr180 anr180 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 3 anr180 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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>]



Reply With Quote
  #3  
Old November 25th, 2012, 04:16 AM
SuperOscar SuperOscar is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2007
Location: Joensuu, Finland
Posts: 412 SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 7 h 13 m 32 sec
Reputation Power: 65
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.

Reply With Quote
  #4  
Old November 25th, 2012, 08:40 AM
Dietrich's Avatar
Dietrich Dietrich is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Posts: 498 Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 4 Days 3 h 4 m 11 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

Reply With Quote
  #5  
Old November 25th, 2012, 11:32 AM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,458 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 4 Days 6 h 26 m 43 sec
Reputation Power: 403
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!

Reply With Quote
  #6  
Old November 25th, 2012, 12:58 PM
anr180 anr180 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 3 anr180 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #7  
Old November 25th, 2012, 01:00 PM
SuperOscar SuperOscar is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2007
Location: Joensuu, Finland
Posts: 412 SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 7 h 13 m 32 sec
Reputation Power: 65
Quote:
Originally Posted by Dietrich
Code:
list3 = list(m*i for m in list1 for i in list2)


Oh, I stand corrected.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Multiplying Elements of Lists Together

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

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


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap