Python Forum
Change type of elements in a list by column - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Change type of elements in a list by column (/thread-1362.html)



Change type of elements in a list by column - tkj80 - Dec-28-2016

Hi,

say I have the following list:

myList = [['john', '1', '2:00pm'], ['becky', '2', '3:15pm'], ['tom', '3', '3:20pm']]

the type of every elements in the inner list is string. How do I change (or what is the pythonic way) to change
the type of the 2nd element in the inner list to integer and the 3rd element to date.time?

Thank you!


RE: Change type of elements in a list by column - micseydel - Dec-28-2016

What have you tried?


RE: Change type of elements in a list by column - tkj80 - Dec-28-2016

I tried the following codes but didn't get the result that I wanted.

myList_chg = [int(x[1]) for x in myList]
>>>myList_chg
Output:
[1, 2, 3]
myList_chg_2 = [myList[0], int(x[1]) for x in myList, myList[2]]
Error:
SyntaxError: invalid syntax
I wanted to access the 2nd elements of the inner list using the following code but it retrieved the following instead:

>>> myList[:][0]
Output:
['john', '1', '2:00pm']



RE: Change type of elements in a list by column - wavic - Dec-28-2016

>>> for i in myList:
...     print i[1]
... 
1
2
3
>>> 



RE: Change type of elements in a list by column - Larz60+ - Dec-28-2016

import sys

myList = [['john', '1', '2:00pm'], ['becky', '2', '3:15pm'], ['tom', '3', '3:20pm']]

def get_myList_item(row=0, column=0):
    try:
        # Python 3.6.0
        print(f'row: {row}, column: {column} contains {myList[row][column]}')
        # other python:
        print('row: {}, column: {} contains {}'.format(row, column, myList[row][column]))
    except:
        print("Unexpected exception:", sys.exc_info()[0])

if __name__ == '__main__':
    get_myList_item(row=1,column=0)
result:
Output:
row: 1, column: 0 contains becky



RE: Change type of elements in a list by column - snippsat - Dec-28-2016

Here a hint for integer convert.
>>> my_list = [['john', '1', '2:00pm'], ['becky', '2', '3:15pm'], ['tom', '3', '3:20pm']]
>>> [[int(x) for x in lst] for lst in my_list]
Error:
Traceback (most recent call last):  File "<string>", line 301, in runcode  File "<interactive input>", line 1, in <module>  File "<interactive input>", line 1, in <listcomp>  File "<interactive input>", line 1, in <listcomp> ValueError: invalid literal for int() with base 10: 'john
Now it try to convert all element in the nested list.
Can wirte a helper function that catch ValueError
def try_int(x):
    try:
        return int(x)
    except ValueError:
        return x
Now try again:
>>> [[try_int(x) for x in lst] for lst in my_list]
[['john', 1, '2:00pm'], ['becky', 2, '3:15pm'], ['tom', 3, '3:20pm']]
Quote:3rd element to date.time?
Do you know how to this,like with only 1 value '3:20pm'?


RE: Change type of elements in a list by column - micseydel - Dec-28-2016

Apologies, I tried to reply to this last night but my internet was having issues.

Here's something close to what you want
newList = [(name, int(num), f(time)) for (name, num, time) in myList]
where f() is whatever conversion you want for time.

I believe this is preferable over mutating things in-place, as a general habit, but if memory is a concern, you could write a loop over myList in which for each sublist, you use indexes to replace the values with their relative modifications.


RE: Change type of elements in a list by column - tkj80 - Jan-04-2017

(Dec-28-2016, 07:54 PM)micseydel Wrote: Apologies, I tried to reply to this last night but my internet was having issues.

Here's something close to what you want
newList = [(name, int(num), f(time)) for (name, num, time) in myList]
where f() is whatever conversion you want for time.

I believe this is preferable over mutating things in-place, as a general habit, but if memory is a concern, you could write a loop over myList in which for each sublist, you use indexes to replace the values with their relative modifications.

Thank you!
it works