0

I have an array known as start. I am trying to use it as a key in a dictionary known as G.

Here is a sample of the array:

array([(1497315, 11965605), (1502535, 11967915), (1501785, 11968665),
       (1520325, 11972295), (1522905, 11972805), (1504545, 11972865),
       (1500465, 11973075), (1489695, 11975205), (1485855, 11978775),
       (1499535, 11978955), (1508205, 11981745), (1521315, 11982615),
       (1501215, 11983335), (1508595, 11985225), (1503045, 11986635),
       (1522425, 11987745), (1512705, 11988255), (1519035, 11989185)...

len (start) is 50

I want to convert start into an integer type so I can use it as a key in my dictionary.

I've tried type (start) is int to confirm that it is not an integer. Start was created from two columns of integers representing x and y coordinates.

Related to previous question:

Dictionary not recognizing floating point keys

4
  • 1
    What do you mean convert to integer? The elements of the array tuples are already integers. Commented Mar 6, 2014 at 14:42
  • How is start initialised? If it's an array I'm not sure why (or how) you'd convert it to int. Commented Mar 6, 2014 at 14:45
  • This post is related to OP's last post stackoverflow.com/questions/22226725/… he was using floats before Commented Mar 6, 2014 at 14:45
  • Array and list are two different thing in Python, yours look like Numpy's ndarray. Commented Mar 6, 2014 at 14:46

1 Answer 1

1

I believe it is with reference to your previous question

array = [(1497315.0, 11965605.0),(1502535.0, 11967915.0),(1501785.0, 11968665.0)]
print map(tuple, map(lambda x: map(int, x), array))
# [(1497315, 11965605), (1502535, 11967915), (1501785, 11968665)]

with list comprehension

print [[int(item) for item in items] for items in array]

If you want to convert the keys of your dictionary to int, you use dictionary comprehension

d = {(15035.0, 119915.0): 'b', (15085.0, 119665.0): 'c', (14975.0, 11965.0): 'a'}
print {(int(k[0]), int(k[1])):v for k, v in d.iteritems()}
# {(15085, 119665): 'c', (14975, 11965): 'a', (15035, 119915): 'b'}
Sign up to request clarification or add additional context in comments.

2 Comments

This prints the conversion, is there a way to change it permanently. When you typeprint array again the values are still floats.
array = {(int(k[0]), int(k[1])):v for k, v in d.iteritems()} :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.