Question

My List is like this:

[('void ', 'treeInit', 'tSymbolTree *T'),('tTreeItemPtr ', 'nodeInsert', 'tTreeItemPtr *T')]

and if I call:

>>>list[0][0]
void

and now it's the problem I using re.sub which returns strign. and if I call

>>>list[0][0] = re.sub('(\s+$),'',newItem)
TypeError: 'tuple' object does not support item assignment

Can somebody help me ? Thanks

Was it helpful?

Solution

tuples are immutable objects, you cannot assign values to them. You should convert your list of tuples to a list of lists. It would like as follows:

[['void ', 'treeInit', 'tSymbolTree *T'], ['tTreeItemPtr ', 'nodeInsert', 'tTreeItemPtr *T]]

To convert your list of tuples to a list of strings, you can do:

>>> a = [('void ', 'treeInit', 'tSymbolTree *T'),('tTreeItemPtr ', 'nodeInsert', 'tTreeItemPtr *T')]
>>> b = [list(i) for i in a]   #or map(list, a)
>>> print b
[['void ', 'treeInit', 'tSymbolTree *T'], ['tTreeItemPtr ', 'nodeInsert', 'tTreeItemPtr *T]]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top