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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Jory R Ferrell
    New Member
    • Jul 2011
    • 62

    #1

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

    "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?

    Code:
    dict = {1:['a','b','c','d'], 2:classObj1, 3:classObj2}
    
    dict[1].append('e','f','g','h','i')
    dict[2].classObj2Method()
    dict[3].classObj3Method(2, myData)
    print(dict[3].name)
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Your problem is list method append() accepts one argument. You are passing 5 arguments. Use method extend() instead.
    Code:
    >>> p1 = Pt3D(1,2,3)
    >>> p1
    Pt3D(1.000000, 2.000000, 3.000000)
    >>> p2 = Pt3D(4,5,6)
    >>> dd = {1:[1,2,3,4,5,6], 2:p1, 3:p2}
    >>> dd
    {1: [1, 2, 3, 4, 5, 6], 2: Pt3D(1.000000, 2.000000, 3.000000), 3: Pt3D(4.000000, 5.000000, 6.000000)}
    >>> dd[1].extend([7,8,9,10])
    >>> dd
    {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)}
    >>> dd[2].cross(dd[3])
    Pt3D(-3.000000, 6.000000, -3.000000)
    >>>

    Comment

    Working...