How I can remove '' in items in nested list?
c = [['1', '1', '10', '92'], ['262', '56', '238', '142'], ['86', '84', '149', '30'], ['48', '362', '205', '237'], ['8', '33', '96', '336'], ['39', '82', '89', '140'], ['170', '296', '223', '210'], ['16', '40', '65', '50'], ['16', '40', '65', '50']]
>>> [ ','.join(i[0:][0:]) for i in c]
['1,1,10,92', '262,56,238,142', '86,84,149,30', '48,362,205,237', '8,33,96,336', '39,82,89,140', '170,296,223,210', '16,40,65,50', '16,40,65,50']
But, I will keep square bracket
[[1,1,10,92], [262,56,238,142], [86,84,149,30], [48,362,205,237], [8,33,96,336'] [39,82,89,140], [170,296,223,210], [16,40,65,50], [16,40,65,50]]
how I can accomplish this task?
This isn't really removing quotations marks. This uses the int function to convert strs into ints, and a list comprehension to build the result:
In [72]: [map(int, grp) for grp in c]
Out[72]:
[[1, 1, 10, 92],
[262, 56, 238, 142],
[86, 84, 149, 30],
[48, 362, 205, 237],
[8, 33, 96, 336],
[39, 82, 89, 140],
[170, 296, 223, 210],
[16, 40, 65, 50],
[16, 40, 65, 50]]
List comprehensions can nest indefinitely. So, you just need to make a nested one that will convert the strings into integers:
>>> c = [['1', '1', '10', '92'], ['262', '56', '238', '142'], ['86', '84', '149', '30'], ['48', '362', '205', '237'], ['8', '33', '96', '336'], ['39', '82', '89', '140'], ['170', '296', '223', '210'], ['16', '40', '65', '50'], ['16', '40', '65', '50']]
>>> [[int(y) for y in x] for x in c]
[[1, 1, 10, 92], [262, 56, 238, 142], [86, 84, 149, 30], [48, 362, 205, 237], [8, 33, 96, 336], [39, 82, 89, 140], [170, 296, 223, 210], [16, 40, 65, 50], [16, 40, 65, 50]]
>>>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With