Usuń końcowy znak nowej linii z elementów listy łańcuchowej
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']
Myślałem, że to, co napisałem, zadziała, ale ciągle dostaję błąd mówiąc:
"obiekt'list' nie posiada atrybutu 'strip '"
Oto kod, który wypróbowałem:
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())
7 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']
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]
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)
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']]
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.
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
Jeśli chcesz usunąć tylkokońcowe białe znaki, możesz użyć str.rstrip()
, który powinien być nieco bardziej wydajny niż str.strip()
:
>>> lst = ['this\n', 'is\n', 'a\n', 'list\n', 'of\n', 'words\n']
>>> [x.rstrip() for x in lst]
['this', 'is', 'a', 'list', 'of', 'words']
>>> list(map(str.rstrip, lst))
['this', 'is', 'a', 'list', 'of', 'words']
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
2018-10-14 16:48:33
my_list = ['this\n', 'is\n', 'a\n', 'list\n', 'of\n', 'words\n']
print([l.strip() for l in my_list])
Wyjście:
['this', 'is', 'a', 'list', 'of', 'words']
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
2019-12-28 11:03:34