Hướng dẫn how do i remove all words from a list in python? - làm cách nào để xóa tất cả các từ khỏi danh sách trong python?

Tôi đã thực hiện mã của mình cho đến nay nhưng nó không hoạt động bình thường với Remove () .. bất cứ ai có thể giúp tôi ..

'''
Created on Apr 21, 2015

@author: Pallavi
'''
from pip._vendor.distlib.compat import raw_input
print ("Enter Query")
str=raw_input()  

fo = open("stopwords.txt", "r+")
str1 = fo.read();
list=str1.split("\n");
fo.close()
words=str.split(" ");
for i in range(0,len(words)):
    for j in range(0,len(list)):
        if(list[j]==words[i]):
            print(words[i])
            words.remove(words(i))

Đây là lỗi:

Enter Query
let them cry try diesd
let them try
Traceback (most recent call last):
  File "C:\Users\Pallavi\workspace\py\src\parser.py", line 17, in <module>
    if(list[j]==words[i]):
IndexError: list index out of range

Hướng dẫn how do i remove all words from a list in python? - làm cách nào để xóa tất cả các từ khỏi danh sách trong python?

Hỏi ngày 21 tháng 4 năm 2015 lúc 11:38Apr 21, 2015 at 11:38

Hướng dẫn how do i remove all words from a list in python? - làm cách nào để xóa tất cả các từ khỏi danh sách trong python?

3

Các lỗi bạn có (ngoài các bình luận khác của tôi) là vì bạn đang sửa đổi danh sách trong khi lặp lại nó. Nhưng bạn có độ dài của danh sách khi bắt đầu, do đó, sau khi bạn đã xóa một số yếu tố, bạn không thể truy cập các vị trí cuối cùng.

Tôi sẽ làm theo cách này:

words = ['a', 'b', 'a', 'c', 'd']
stopwords = ['a', 'c']
for word in list(words):  # iterating on a copy since removing will mess things up
    if word in stopwords:
        words.remove(word)

Một cách thậm chí là Pythonic hơn khi sử dụng danh sách toàn diện:

new_words = [word for word in words if word not in stopwords]

Đã trả lời ngày 21 tháng 4 năm 2015 lúc 11:48Apr 21, 2015 at 11:48

Francis Colasfrancis ColasFrancis Colas

3.1512 Huy hiệu vàng24 Huy hiệu bạc31 Huy hiệu đồng2 gold badges24 silver badges31 bronze badges

2

Theo quan sát, đây có thể là một cách thanh lịch khác để làm điều đó:

new_words = list(filter(lambda w: w not in stop_words, initial_words))

Đã trả lời ngày 9 tháng 10 năm 2019 lúc 13:34Oct 9, 2019 at 13:34

''' call this script in a Bash Konsole like so:    python  reject.py
    purpose of this script: remove certain words from a list of words ,
    e.g. remove invalid packages in a request-list using 
    a list of rejected packages from the logfile, 
    say on https://fai-project.org/FAIme/#
    remove trailing spaces e.g. with KDE Kate in wordlist like so:

kate: remove-trailing-space on; BOM off;
'''
with open("rejects", "r+")       as fooo   :
    stwf    = fooo.read()
toreject    = stwf.split("\n")

with open("wordlist", "r+")      as bar    :
  woL       = bar.read()
words       = woL.split("\n")

new_words = [word for word in words if word not in toreject]
with open("cleaned", "w+")       as foobar :
    for ii in new_words:
        foobar.write("%s\n" % ii)

Một cách dễ dàng hơn để xóa các từ khỏi danh sách là chuyển đổi 2 danh sách thành tập hợp và thực hiện phép trừ btw danh sách.

words = ['a', 'b', 'a', 'c', 'd']
words = set(words)
stopwords = ['a', 'c']
stopwords = set(stopwords)
final_list = words - stopwords
final_list = list(final_list)

Đã trả lời ngày 22 tháng 4 năm 2020 lúc 13:08Apr 22, 2020 at 13:08

1

Xóa danh sách các từ khỏi chuỗi trong Python #

Để xóa danh sách các từ khỏi chuỗi:

  1. Chia chuỗi trên mỗi ký tự khoảng trắng.
  2. Sử dụng danh sách hiểu để lọc ra danh sách các từ.
  3. Sử dụng phương thức
    Enter Query
    let them cry try diesd
    let them try
    Traceback (most recent call last):
      File "C:\Users\Pallavi\workspace\py\src\parser.py", line 17, in <module>
        if(list[j]==words[i]):
    IndexError: list index out of range
    
    1 để tham gia danh sách được lọc vào một chuỗi.

Copied!

words_to_remove = ['one', 'two', 'three'] my_str = 'apple one banana two kiwi three' list_of_words = my_str.split() print(list_of_words) # 👉️ ['apple', 'one', 'banana', 'two', 'kiwi', 'three'] filtered_words = [word for word in list_of_words if word.lower() not in words_to_remove] print(filtered_words) # 👉️ ['apple', 'banana', 'kiwi'] final_string = ' '.join(filtered_words) print(final_string) # 👉️ 'apple banana kiwi'

Bước đầu tiên là sử dụng phương thức

Enter Query
let them cry try diesd
let them try
Traceback (most recent call last):
  File "C:\Users\Pallavi\workspace\py\src\parser.py", line 17, in <module>
    if(list[j]==words[i]):
IndexError: list index out of range
2 để chia chuỗi thành một danh sách các từ.

Copied!

my_str = 'apple one banana two kiwi three' list_of_words = my_str.split() print(list_of_words) # 👉️ ['apple', 'one', 'banana', 'two', 'kiwi', 'three']

Phương thức str.split () chia chuỗi thành một danh sách các chuỗi con bằng cách sử dụng dấu phân cách.

Phương thức lấy 2 tham số sau:

TênSự mô tả
máy tách biệtChia chuỗi thành chuỗi con trên mỗi lần xuất hiện
MaxSplitNhiều nhất
Enter Query
let them cry try diesd
let them try
Traceback (most recent call last):
  File "C:\Users\Pallavi\workspace\py\src\parser.py", line 17, in <module>
    if(list[j]==words[i]):
IndexError: list index out of range
3 chia tách được thực hiện (tùy chọn)

Khi phương thức

Enter Query
let them cry try diesd
let them try
Traceback (most recent call last):
  File "C:\Users\Pallavi\workspace\py\src\parser.py", line 17, in <module>
    if(list[j]==words[i]):
IndexError: list index out of range
2 được gọi mà không có dấu phân cách, nó coi các ký tự khoảng trắng liên tiếp là một dấu phân cách duy nhất.

Nếu các từ trong chuỗi của bạn được phân tách bằng một dấu phân cách khác, hãy đảm bảo chuyển nó trong cuộc gọi đến

Enter Query
let them cry try diesd
let them try
Traceback (most recent call last):
  File "C:\Users\Pallavi\workspace\py\src\parser.py", line 17, in <module>
    if(list[j]==words[i]):
IndexError: list index out of range
2, ví dụ:
Enter Query
let them cry try diesd
let them try
Traceback (most recent call last):
  File "C:\Users\Pallavi\workspace\py\src\parser.py", line 17, in <module>
    if(list[j]==words[i]):
IndexError: list index out of range
6.

Bước tiếp theo là sử dụng danh sách hiểu biết để lọc ra một số từ.

Copied!

words_to_remove = ['one', 'two', 'three'] my_str = 'apple one banana two kiwi three' list_of_words = my_str.split() print(list_of_words) # 👉️ ['apple', 'one', 'banana', 'two', 'kiwi', 'three'] filtered_words = [word for word in list_of_words if word.lower() not in words_to_remove] print(filtered_words) # 👉️ ['apple', 'banana', 'kiwi']

Danh sách các hệ thống được sử dụng để thực hiện một số hoạt động cho mọi yếu tố hoặc chọn một tập hợp con của các phần tử đáp ứng một điều kiện.

Trên mỗi lần lặp, chúng tôi kiểm tra xem từ không có trong danh sách

Enter Query
let them cry try diesd
let them try
Traceback (most recent call last):
  File "C:\Users\Pallavi\workspace\py\src\parser.py", line 17, in <module>
    if(list[j]==words[i]):
IndexError: list index out of range
7 trước khi trả lại.

Các thử nghiệm trong nhà điều hành để thành viên. Ví dụ,

Enter Query
let them cry try diesd
let them try
Traceback (most recent call last):
  File "C:\Users\Pallavi\workspace\py\src\parser.py", line 17, in <module>
    if(list[j]==words[i]):
IndexError: list index out of range
8 đánh giá thành
Enter Query
let them cry try diesd
let them try
Traceback (most recent call last):
  File "C:\Users\Pallavi\workspace\py\src\parser.py", line 17, in <module>
    if(list[j]==words[i]):
IndexError: list index out of range
9 nếu
words = ['a', 'b', 'a', 'c', 'd']
stopwords = ['a', 'c']
for word in list(words):  # iterating on a copy since removing will mess things up
    if word in stopwords:
        words.remove(word)
0 là thành viên của
words = ['a', 'b', 'a', 'c', 'd']
stopwords = ['a', 'c']
for word in list(words):  # iterating on a copy since removing will mess things up
    if word in stopwords:
        words.remove(word)
1, nếu không nó sẽ đánh giá thành
words = ['a', 'b', 'a', 'c', 'd']
stopwords = ['a', 'c']
for word in list(words):  # iterating on a copy since removing will mess things up
    if word in stopwords:
        words.remove(word)
2.

words = ['a', 'b', 'a', 'c', 'd']
stopwords = ['a', 'c']
for word in list(words):  # iterating on a copy since removing will mess things up
    if word in stopwords:
        words.remove(word)
3 trả về sự phủ định của
Enter Query
let them cry try diesd
let them try
Traceback (most recent call last):
  File "C:\Users\Pallavi\workspace\py\src\parser.py", line 17, in <module>
    if(list[j]==words[i]):
IndexError: list index out of range
8.

Chúng tôi cũng đã sử dụng phương thức

words = ['a', 'b', 'a', 'c', 'd']
stopwords = ['a', 'c']
for word in list(words):  # iterating on a copy since removing will mess things up
    if word in stopwords:
        words.remove(word)
5 để chuyển đổi từng từ thành chữ thường, đó là tùy chọn.

Khi chúng tôi có một danh sách chỉ chứa các từ chúng tôi cần, chúng tôi có thể sử dụng phương thức

Enter Query
let them cry try diesd
let them try
Traceback (most recent call last):
  File "C:\Users\Pallavi\workspace\py\src\parser.py", line 17, in <module>
    if(list[j]==words[i]):
IndexError: list index out of range
1 để tham gia danh sách các từ vào một chuỗi.

Enter Query
let them cry try diesd
let them try
Traceback (most recent call last):
  File "C:\Users\Pallavi\workspace\py\src\parser.py", line 17, in <module>
    if(list[j]==words[i]):
IndexError: list index out of range
0

Phương thức str.join lấy một điều đáng tin cậy như một đối số và trả về một chuỗi là sự kết hợp của các chuỗi trong điều kiện có thể sử dụng được.

Chuỗi phương thức được gọi là bật được sử dụng làm phân tách giữa các phần tử.

Chúng tôi đã tham gia các chuỗi trong danh sách với bộ phân cách không gian trong ví dụ, nhưng bạn có thể sử dụng bất kỳ dấu phân cách nào khác phù hợp với trường hợp sử dụng của bạn.

Làm cách nào để xóa tất cả nội dung từ một danh sách trong Python?

Phương pháp số 3: Sử dụng các trò chơi*= 0, đây là một phương thức ít được biết đến, nhưng phương pháp này sẽ loại bỏ tất cả các yếu tố của danh sách và làm cho nó trống rỗng.Phương pháp số 4: Sử dụng DEL: DEL có thể được sử dụng để xóa các yếu tố danh sách trong một phạm vi, nếu chúng ta không đưa ra một phạm vi, tất cả các yếu tố sẽ bị xóa.Using “*= 0” : This is a lesser known method, but this method removes all elements of the list and makes it empty. Method #4 : Using del : del can be used to clear the list elements in a range, if we don't give a range, all the elements are deleted.

Làm cách nào để loại bỏ tất cả các chuỗi trong danh sách Python?

Làm thế nào để bạn tước dữ liệu trong Python?Phương thức Dải () Chức năng được xây dựng của Python được sử dụng để loại bỏ tất cả các không gian dẫn đầu và dấu ngoặc ra khỏi một chuỗi.Tham số: chars (tùy chọn): ký tự hoặc một tập hợp các ký tự, cần được xóa khỏi chuỗi.The strip() method in-built function of Python is used to remove all the leading and trailing spaces from a string. Parameter: chars(optional): Character or a set of characters, that needs to be removed from the string.

Làm thế nào để bạn làm trống một danh sách trong Python?

Để xóa toàn bộ danh sách Python, hãy sử dụng phương thức rõ ràng () của một danh sách.Để xóa một phần tử tại một chỉ mục cụ thể, hãy sử dụng phương thức pop () hoặc câu lệnh DEL.Để loại bỏ sự xuất hiện đầu tiên của một phần tử trong danh sách, hãy sử dụng phương thức Remove ().use the clear() method of a list. To remove an element at a specific index, use the pop() method or the del statement. To remove the first occurrence of an element in a list, use the remove() method.