Usuń wszystkie elementy listy łańcuchów

Muszę wziąć dużą listę słów w postaci:

['this\n', 'is\n', 'a\n', 'list\n', 'of\n', 'words\n']

A następnie za pomocą funkcji strip przekształć ją w:

['this', 'is', 'a', 'list', 'of', 'words']
Author: Aran-Fey, 2011-11-02

5 answers

>>> my_list = ['this\n', 'is\n', 'a\n', 'list\n', 'of\n', 'words\n']
>>> map(str.strip, my_list)
['this', 'is', 'a', 'list', 'of', 'words']
 155
Author: Sven Marnach,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-11-02 16:52:38

Zrozumienie listy? [x.strip() for x in lst]

 85
Author: yosukesabai,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-11-02 16:53:03

Możesz użyć list składanych :

strip_list = [item.strip() for item in lines]

Lub map Funkcja:

# with a lambda
strip_list = map(lambda it: it.strip(), lines)

# without a lambda
strip_list = map(str.strip, lines)
 39
Author: Schnouki,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-11-02 16:53:16

Można to zrobić za pomocą zestawień list zdefiniowanych w PEP 202

[w.strip() for w in  ['this\n', 'is\n', 'a\n', 'list\n', 'of\n', 'words\n']]
 8
Author: Casey,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-11-03 14:23:26

Wszystkie inne odpowiedzi, a głównie na temat rozumienia listy, są świetne. Ale żeby wyjaśnić swój błąd:

strip_list = []
for lengths in range(1,20):
    strip_list.append(0) #longest word in the text file is 20 characters long
for a in lines:
    strip_list.append(lines[a].strip())

a jest członkiem twojej listy, a nie indeksu. Co można napisać to:

[...]
for a in lines:
    strip_list.append(a.strip())

Kolejny ważny komentarz: możesz utworzyć pustą listę w ten sposób:

strip_list = [0] * 20

Ale to nie jest tak przydatne, jak .append dodaje rzeczy do twojej listy. W Twoim przypadku nie jest przydatne tworzenie listy z wartościami defaut, ponieważ podczas dodawania będziesz ją budować rozebrane struny.

Więc Twój kod powinien wyglądać tak:

strip_list = []
for a in lines:
    strip_list.append(a.strip())

Ale, na pewno, najlepszy jest ten, ponieważ to jest dokładnie to samo:

stripped = [line.strip() for line in lines]

Jeśli masz coś bardziej skomplikowanego niż tylko .strip, Umieść to w funkcji i zrób to samo. To najbardziej czytelny sposób pracy z listami.

 2
Author: Joël,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-11-03 13:46:40