Removing a character from a string in a list of lists

I am trying to format some data for analysis purposes. I am trying to remove '*'

from all lines that start with one. Here is a piece of data:

[['Version', 'age', 'language', 'Q1', 'Q2', 'Q3', 'Q4', 'Q5', 'Q6', 'Q7', 'Q8', 'Q9', 'Q10', 'Q11', 'Q12', 'Q13', 'Q14', 'Q15', 'Q16', 'Q17', 'Q18', 'Q19', 'Q20', 'Q21', 'Q22', 'Q23', 'Q24', 'Q25', 'Q26', 'Q27', 'Q28', 'Q29', 'Q30', 'Q31', 'Q32', 'Q33', 'Q34', 'Q35', 'Q36', 'Q37', 'Q38', 'Q39', 'Q40', 'Q41', 'Q42', 'Q43', 'Q44', 'Q45'], ['1', '18 to 40', 'English', '*distort', '*transfer', '*retain', 'constrict', '*secure', '*excite', '*cancel', '*hinder', '*overstate', 'channel', '*diminish', '*abolish', '*comprehend', '*tolerate', '*conduct', '*destroy', '*foster', 'direct', '*challenge', 'forego', '*cause', '*reduce', 'interrupt', '*enhance', '*misapply', '*exhaust', '*extinguish', '*assimilate', 'believe', 'harmonize', '*demolish', 'affirm', 'trouble', 'discuss', '*force', 'divide', '*remove', '*release', 'highlight', 'reinforce', 'stifle', '*compromise', '*experience', 'evaluate', 'replenish']]

      

It should be easy, but I haven't tried anything. For example:

for lst in testList:
    for item in lst:
        item.replace('*', '')

      

just returns the same lines to me. I have also tried inserting an if statement and indexing characters in strings. I know I can access the strings. For example, if I say if item[0] == '*': print item

, it prints the correct items.

+3


source to share


5 answers


string

are immutable and as such item.replace('*','')

returns a string with replaced characters, it does not replace them inplace (it cannot, since it is string

immutable). you can enumerate your lists and then put the returned string back into a list -

Example -

for lst in testList:
    for j, item in enumerate(lst):
        lst[j] = item.replace('*', '')

      




You can also easily deal with list comprehension -

testList = [[item.replace('*', '') for item in lst] for lst in testList]

      

+7


source


You can try using an enum to be able to access the index of the list item when the time comes and you need to change it:



 for lst in testList:
      for i, item in enumerate(lst):
          if item.startswith('*'):
               lst[i] = item[1:] # Or lst[i] = item.replace('*', '') for more

      

+2


source


You need to either create a new one list

(demonstrated below) or access the indexes of the old one.

new_list = [[item.replace('*','') if item[0]=='*' else item for item in l] for l in old_list]

      

0


source


y = []
for lst in testList:
    for a in lst:
        z = a.replace('*','')
        y.append(z)
testList = []
testList.append(y)
print testList

      

0


source


In your code, you are *

only replacing in a dummy variable and not writing to the list. Usage lstrip

will only accept the *

left side of the string.

for x in xrange(len(testList)):
    testList[x] = testList[x].lstrip('*')

      

0


source







All Articles