3

I seem to have a very silly problem,

>>> a = ['[','1','2',']','[','2','3',']']
>>> b = str(a)
>>> print b
['[', '1', '2', ']', '[', '2', '3', ']']
>>> b.strip('[')
"'[', '1', '2', ']', '[', '2', '3', ']']"
>>> b.strip('[').strip(']')
"'[', '1', '2', ']', '[', '2', '3', ']'"
>>> 

I can't remove the square brackets from my list, I thought maybe converting it to a string, and strip the [ ] characters would help, but doesn't seem to be the solution.

output_test_post.txt contains this:

1_0_0_0_0_0_0_0 [ 1 1 ] [ 1 1 ] [ 1 1 ] [ 1 1 ] [ 1 1 ] [ 1 1 ] [ 1 1 ] [ 1 1 ] [ 1 1 ] [ 1 1 ] [ 1 1 ] [ 1 1 ] [ 1 1 ] [ 1 1 ] [ 1 1 ] [ 1 1 ] [ 1 1 ] [ 1 1 ] [ 1 1 ] [ 1 1 ] [ 1 1 ] [ 1 1 ] [ 1 1 ] [ 1 1 ] [ 1 1 ] [ 1 1 ] [ 1 1 ] [ 1 1 ] [ 1 1 ] [ 1 1 ] [ 1 1 ] [ 1 1 ] [ 1 1 ] [ 1 1 ] [ 1 1 ] [ 1 1 ] [ 1 1 ] [ 1 1 ] [ 1 1 ] [ 1 1 ] [ 1 1 ] [ 1 1 ] [ 1 1 ] [ 1 1 ] [ 1 1 ] [ 1 1 ] [ 1 1 ] [ 1 1 ] [ 1 1 ] [ 1 1 ] [ 1 1 ] [ 1 1 ] [ 1 1 ] [ 1 1 ] [ 1 1 ] [ 1 1 ] [ 1 1 ] [ 1 1 ] [ 1 1 ] [ 1 1 ] [ 1 1 ] [ 1 1 ] [ 1 1 ] [ 1 1 ] [ 1 1 ] [ 1 1 ] [ 1 1 ] [ 1 1 ] [ 1 1 ] [ 1 1 ] [ 1 1 ] [ 1 1 ] [ 1 1 ] [ 1 1 ] [ 1 1 ] [ 1 1 ] [ 1 1 ] [ 1 1 ] [ 1 1 ] [ 1 1 ] [ 1 1 ] [ 1 1 ] [ 1 1 ] [ 1 1 ] [ 1 1 ] [ 1 1 ] [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[ 4 1 ] [ 4 1 ] [ 4 1 ] [ 4 1 ] [ 4 1 ] [ 4 1 ] [ 4 1 ] [ 4 1 ] [ 4 1 ] [ 4 1 ] [ 4 1 ] [ 4 1 ] [ 4 1 ] 

Current processing:

map_test = {}
os.chdir(output_test)
with open("output_test_post.txt",'rb') as csvfile:
    reader = csv.reader(csvfile, delimiter = ' ')
    for row in reader:
        name = row[0]
        map_test[name] = str(row[1:])
        print map_test[name]
        raw_input("Somethign")
map_train = {}
cs95
  • 379,657
  • 97
  • 704
  • 746
I am not Fat
  • 283
  • 11
  • 36
  • 4
    I think you should fix the *source* of this silliness. – juanpa.arrivillaga Jun 13 '17 at 17:37
  • `replace()` is your friend. – Vincent Ramdhanie Jun 13 '17 at 17:37
  • 2
    how did you end up `a`? casting a list to string then back to a list is rarely helpful... – Tadhg McDonald-Jensen Jun 13 '17 at 17:37
  • use `replace`. replace all the brackets with empty strings. – Sufian Latif Jun 13 '17 at 17:37
  • 1
    Do you want to remove all occurences of an item in a list? See https://stackoverflow.com/q/1157106/5754656 – Artyer Jun 13 '17 at 17:37
  • 1
    Possible duplicate of [Remove all occurrences of a value from a Python list](https://stackoverflow.com/questions/1157106/remove-all-occurrences-of-a-value-from-a-python-list) – Dmiters Jun 13 '17 at 17:38
  • "I'm wearing pants on my head. How do I take them off?" *Don't put your pants on your head*, and *don't do whatever got you this `a` list*. – user2357112 Jun 13 '17 at 17:39
  • @juanpa.arrivillaga Ha... that would indeed help... Why I didn't i think of that. – I am not Fat Jun 13 '17 at 17:43
  • @TadhgMcDonald-Jensen I am currently extracting statics [µ,sigma] They are stored in a csv file, so `csvreader` puts them into a list... reader = csv.reader(csvfile, delimiter = ' ') for row in reader: name = row[0] map_test[name] = str(row[1:]) – I am not Fat Jun 13 '17 at 17:47
  • yeah so `str(row[1:])` would be converting the list `row[1:]` to a string, which gives you `[]` characters then I'm guessing you are using `.split(", ")` to get it back into a list? – Tadhg McDonald-Jensen Jun 13 '17 at 17:50
  • @TadhgMcDonald-Jensen `str(a)` is something I recently added, as an atempt of trying to remove the extra character. – I am not Fat Jun 13 '17 at 17:58
  • the newly posted code still doesn't show how/when you got `['[','1','2',']','[','2','3',']']` as a value, where did it come from? Does the csv have square bracket characters in it? – Tadhg McDonald-Jensen Jun 13 '17 at 18:02
  • its how the file is stored @TadhgMcDonald-Jensen – I am not Fat Jun 13 '17 at 18:05
  • oh huh, thats odd. sometimes people convert between list and string and then ask how they can clean up the result. (hence [comments like this](https://stackoverflow.com/questions/44528081/remove-square-bracket-character-from-a-list-of-character?noredirect=1#comment76048000_44528081)) so I'm glad this wasn't one of those cases :) – Tadhg McDonald-Jensen Jun 13 '17 at 18:09

4 Answers4

3
[x for x in a if x not in "[]"]
DYZ
  • 55,249
  • 10
  • 64
  • 93
1

You can use a list comprehension to filter the brackets out.

>>> a = ['[','1','2',']','[','2','3',']']
>>> b = [i for i in a if i not in ['[', ']']]
>>> b
['1', '2', '2', '3']
cs95
  • 379,657
  • 97
  • 704
  • 746
1

Use regexp for it

re.sub(r'[\[\]]', '', b)
vZ10
  • 2,468
  • 2
  • 23
  • 33
0
b=list(filter(lambda a: a!='[' and a!=']' and a!=',' and a!=' ' and a!="'", b))

note: b is your input string