1
{
    "key1" : <list of strings>,
    "key2" : <list of integeres> }

I want to change type of 'key2' list to int. I have already tried looping over and using

v = int(v)

I have also tried mapping int to whole list.

map(int, list)

Any other way I can accomplish this task?

Current Code:

integer_columns = ["col1","col2","col3","col4"]
for col in integer_columns:
    col_list = config_data[col]
    col_list = list(map(int, col_list))
Avik Aggarwal
  • 499
  • 6
  • 23
  • are you suggesting the value that's keyed with 'key2' is a list of numpy.int64 at the moment? – stucash Dec 07 '17 at 12:20
  • Right. Elements in d['key2'] are of type numpy.int64 – Avik Aggarwal Dec 07 '17 at 12:51
  • to be fair, I think you should change your question to something similar to why my map function didn't work, at the moment your question suggests you are asking for alternatives. – stucash Dec 07 '17 at 13:57

2 Answers2

3

What's wrong with map?

d['key2'] = map(int, d['key2']) or d['key2'] = list(map(int, d['key2'])) on Python 3:

d = {'key2': ['1', '2', '3']}
print(d)
d['key2'] = list(map(int, d['key2']))
print(d)

Outputs

{'key2': ['1', '2', '3']}
{'key2': [1, 2, 3]}

Edit after OP updated the question

for col in integer_columns:
    col_list = config_data[col]          # col_list references to config_data[col]

    col_list = list(map(int, col_list))  # now col_list references to an entire
                                         # new list of ints, that has nothing to do
                                         # with config_data[col]

col_list is being modified, but this change is not reflected back to config_data[col]. Instead, do something similar to what I showed in my original answer above:

for col in integer_columns:
    config_data[col] = list(map(int, config_data[col]))
DeepSpace
  • 65,330
  • 8
  • 79
  • 117
  • I have already tried doing the same, but somehow it refuses to work. type of all elements remains numpy.int64 – Avik Aggarwal Dec 07 '17 at 12:50
  • 1
    @AvikAggarwal Either you are doing something wrong or you think that it "refuses" to work (since it's obvious that it's working as my answer shows). Either way you will need to update your question with your exact code. Keep in mind that `map` **returns a new list (map object in Python 3)** and does **not** modify the provided iterable in-place. – DeepSpace Dec 07 '17 at 12:52
  • Yes this did the trick. Didnt notice that map is returning new list everytime. Thanks – Avik Aggarwal Dec 07 '17 at 13:14
  • @DeepSpace if map returns a new list (map object in Python 3) then why you have written redundent list(map(int, d['key2'])) , there is no essential to write list before map – Argus Malware Dec 07 '17 at 13:40
  • @ArgusMalware because in Python 3 `map` doesn't return a list. Therefore, `list(map(...))` is **not** redundant in Python 3. A `map` object in Python 3 is an iterator that can be consumed only once. Consider: `a = map(int, ['1']) ; print(list(a)) ; print(list(a))`. The output will be `[1] ; [ ]`. – DeepSpace Dec 07 '17 at 13:41
  • @DeepSpace thanx for the insight – Argus Malware Dec 07 '17 at 13:44
0

bug fixed.

assuming you have a dict with each key mapping to a list of numpy.int64.

Setup

d = {'key2':[np.int64(v) for v in xrange(10)]}

Trials

%timeit -n 1000 d['key2'] = map(int, d['key2'])
1000 loops, best of 3: 1.5 µs per loop

%timeit -n 1000 d['key2'] = [int(v) for v in d['key2']]
1000 loops, best of 3: 2.0 µs per loop

%timeit -n 1000 d['key2'] = [np.asscalar(v) for v in np.array(d['key2'])]
1000 loops, best of 3: 11.6 µs per loop

updated with your current codes:

integer_columns = ["col1","col2","col3","col4"]  # assuming you have a list of list here

for col in integer_columns:
    x = np.array(col)
    config_data[col] = [np.asscalar(v) for v in x]

# >>> type(integer_columns[0][1])
# >>> int

numpy.asscalar is a function in numpy for converting numpy types to native python types. here's good answer explaining it.

So there's definitely other ways of doing it, it depends on your particular scenario for a certain solution.

stucash
  • 649
  • 8
  • 19