How can I convert a dict_keys list to integers
<p>I am trying to find a way of converting a list within dict_keys() to an integer so I can use it as a trigger to send to another system. My code (below) imports a list of 100 words (a txt file with words each on a new line) which belong to 10 categories (e.g. the first 10 words belong to category 1, second 10 words belong to category 2 etc...). </p>

<p><strong>Code:</strong></p>

<pre><code>from numpy.random import choice
from collections import defaultdict

number_of_elements = 10

Words = open('file_location').read().split()

categories = defaultdict(list)
for i in range(len(words)):
    categories[i/number_of_elements].append(words[i])

category_labels = categories.keys()

category_labels
</code></pre>

<p><strong>Output</strong></p>

<pre><code>dict_keys([0.0, 1.1, 2.0, 3.0, 4.9, 5.0, 0.5, 1.9, 8.0, 9.0, 1.3, 2.7, 3.9, 9.2, 9.4, 7.2, 4.2, 8.6, 5.1, 5.4, 3.3, 1.0, 6.6, 7.4, 7.7, 8.4, 5.8, 9.8, 0.7, 8.8, 2.1, 7.0, 6.4, 4.3, 0.1, 2.5, 3.8, 1.2, 6.9, 7.1, 5.6, 0.4, 5.3, 2.9, 7.3, 3.5, 9.5, 8.2, 2.8, 3.1, 0.9, 2.3, 8.1, 4.0, 6.3, 6.7, 4.5, 0.2, 1.7, 2.2, 8.9, 1.4, 7.6, 9.1, 7.8, 5.5, 4.8, 0.6, 3.2, 2.4, 6.5, 9.9, 9.6, 1.5, 6.0, 3.7, 4.7, 3.4, 5.9, 4.1, 1.6, 6.8, 9.3, 3.6, 8.5, 8.7, 0.3, 0.8, 7.5, 5.2, 2.6, 4.6, 5.7, 7.9, 6.1, 1.8, 8.3, 6.2, 9.7, 4.4])
</code></pre>

<p><strong>What I need:</strong></p>

<p>I would like the first number before the point (e.g. if it was 6.7, I just want the 6 as an int). </p>

<p>Thank you in advance for any help and/or advice!</p>

<p>Just convert your keys to integers using a list comprehension; note that there is no need to call <code>.keys()</code> here as iteration over the dictionary directly suffices:</p>

<pre><code>[int(k) for k in categories]
</code></pre>

<p>You may want to bucket your values directly into integer categories rather than by floating point values:</p>

<pre><code>categories = defaultdict(list)
for i, word in enumerate(words):
    categories[int(i / number_of_elements)].append(word)
</code></pre>

<p>I used <code>enumerate()</code> to pair words up with their index, rather than use <code>range()</code> plus indexing back into <code>words</code>.</p>

python dictionary defaultdict