By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,688 Members | 1,121 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,688 IT Pros & Developers. It's quick & easy.

How to manipulate data in a list while rely on references to it with a dictionary?

Jory R Ferrell
P: 62
"while relying on...." :P

Anyways...

Basically I am starting off with a user defined number of variables. For each object needed, I 'assign a variable'
by using the key of a dictionary. How do I use object references, append to list and what not, if i am relying on a dictionary to call it?

Expand|Select|Wrap|Line Numbers
  1.  
  2. dict = {1:['a','b','c','d'], 2:classObj1, 3:classObj2}
  3.  
  4. dict[1].append('e','f','g','h','i')
  5. dict[2].classObj2Method()
  6. dict[3].classObj3Method(2, myData)
  7. print(dict[3].name)
  8.  
  9.  
Jan 23 '12 #1

✓ answered by bvdet

Your problem is list method append() accepts one argument. You are passing 5 arguments. Use method extend() instead.
Expand|Select|Wrap|Line Numbers
  1. >>> p1 = Pt3D(1,2,3)
  2. >>> p1
  3. Pt3D(1.000000, 2.000000, 3.000000)
  4. >>> p2 = Pt3D(4,5,6)
  5. >>> dd = {1:[1,2,3,4,5,6], 2:p1, 3:p2}
  6. >>> dd
  7. {1: [1, 2, 3, 4, 5, 6], 2: Pt3D(1.000000, 2.000000, 3.000000), 3: Pt3D(4.000000, 5.000000, 6.000000)}
  8. >>> dd[1].extend([7,8,9,10])
  9. >>> dd
  10. {1: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 2: Pt3D(1.000000, 2.000000, 3.000000), 3: Pt3D(4.000000, 5.000000, 6.000000)}
  11. >>> dd[2].cross(dd[3])
  12. Pt3D(-3.000000, 6.000000, -3.000000)
  13. >>>

Share this Question
Share on Google+
1 Reply

bvdet
Expert Mod 2.5K+
P: 2,851
Your problem is list method append() accepts one argument. You are passing 5 arguments. Use method extend() instead.
Expand|Select|Wrap|Line Numbers
  1. >>> p1 = Pt3D(1,2,3)
  2. >>> p1
  3. Pt3D(1.000000, 2.000000, 3.000000)
  4. >>> p2 = Pt3D(4,5,6)
  5. >>> dd = {1:[1,2,3,4,5,6], 2:p1, 3:p2}
  6. >>> dd
  7. {1: [1, 2, 3, 4, 5, 6], 2: Pt3D(1.000000, 2.000000, 3.000000), 3: Pt3D(4.000000, 5.000000, 6.000000)}
  8. >>> dd[1].extend([7,8,9,10])
  9. >>> dd
  10. {1: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 2: Pt3D(1.000000, 2.000000, 3.000000), 3: Pt3D(4.000000, 5.000000, 6.000000)}
  11. >>> dd[2].cross(dd[3])
  12. Pt3D(-3.000000, 6.000000, -3.000000)
  13. >>>
Jan 23 '12 #2

Post your reply

Sign in to post your reply or Sign up for a free account.