-2

I have a dictionary of items, where the list is formatted to be {(x,y): z, (x,y):z...} where x and y are coordinates and z is a probability. I need to perform operations using the probability. How can I access those?

I tried things like

    for item in lst:
        print item[1]

However, that only returned the y coordinate. Attempting to print item[2] returned the error "Need more than 2 values to unpack"

DMop
  • 463
  • 8
  • 23

5 Answers5

4

As you are using a dictionary, python can retrieve both names and values individually. For example for:

>>> a = {(1,2): 0.5, (2,3): 0.4}
>>> a.keys()
>>> [(1, 2), (2, 3)]

>>> a.values()
>>> [0.5, 0.4]

Therefore for you to perform calculations on the probabilities you need to do:

for item in a.values():
    print item

each item will output the values of your dictionary in sequence.

pacificgilly1992
  • 384
  • 3
  • 11
1

You can use this:

list = {(x1, y1): z1, (x2, y2): z2, ...} # actually a dict
for (x, y), z in list.items():
    print x, y, z

or this:

list = {(x1, y1): z1, (x2, y2): z2, ...} # actually a dict
for z in list.values():
    print z

But you may be better off using a true list instead of a dict. It's also best to avoid giving your variables names that match built-in Python components like list. So then you'd have something like this:

lst = [(x1, y1, z1), (x2, y2, z2), ...]
for x, y, z in lst:
    print x, y, z
Matthias Fripp
  • 17,670
  • 5
  • 28
  • 45
1

First, you do not have a lit, that is a dict, so you can access z by using coordinates as key:

my_dict = {(x,y): z, (x,y):z...}
my_dict[(x, y)]

In a for loop:

for probability in my_dict.values():
    print(probability)

I recommend you do not use the name list for this cause it is a built-in

lpozo
  • 598
  • 2
  • 9
1

For item in list is getting one item at a time. So how can you access second item. Here is an example I made for you. Let say here is your dictionary d where coordinates 10,20 has prob 0.1 and 20,30 has prob 0.2

d = {(10,20):0.1, (20,30):0.2}
d.items() # this will print- dict_items([((10, 20), 0.1), ((20, 30), 0.2)])
d[(10,20)] # this will print 0.1
d[(20,30)] # this will print 0.2
Pal
  • 920
  • 6
  • 6
0

If what you present us here is indeed your data structure {(x,y): z, (x,y):z...} then you should iterate and unpack it in another fashion than you do:

Python 3.6.3 (default, Oct  4 2017, 06:09:15)
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.37)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> l = {(1,2):0.5, (3,4):0.9}
>>> for coordinates, probability in l.items():
...     print(coordinates, probability)
...
(1, 2) 0.5
(3, 4) 0.9

See Documentation

If you want a list of tuples, you could transform it easily with

>>> [ (coordinates, probabilities) for coordinates, probabilities in l.items() ]
[((1, 2), 0.5), ((3, 4), 0.9)]

In your version, you unpack the keys of the dictionary, which are a tuple of an x and a y coordinate. print item[1] references the y coordinate (python has 0-based indexing), which is not, what you want.

Thomas Junk
  • 5,588
  • 2
  • 30
  • 43