Hướng dẫn how do i remove unwanted characters from a list in python? - làm cách nào để xóa các ký tự không mong muốn khỏi danh sách trong python?

Tôi có một danh sách các yếu tố chứa các ký tự đặc biệt. Tôi muốn chuyển đổi danh sách thành các ký tự chữ và số. Không có ký tự đặc biệt. my_list = ["on@3", "hai#", "thre%e"]]

đầu ra mong đợi của tôi là,

out_list = ["one","two","three"]

Tôi không thể đơn giản áp dụng

import re
my_list= ["on@3", "two#", "thre%e"]
print [re.sub['[^a-zA-Z0-9]+', '', _] for _ in my_list]
5 cho các mặt hàng này, xin vui lòng giúp đỡ.

Đã hỏi ngày 15 tháng 11 năm 2017 lúc 7:36Nov 15, 2017 at 7:36

3

Đây là một giải pháp khác:

import re
my_list= ["on@3", "two#", "thre%e"]
print [re.sub['[^a-zA-Z0-9]+', '', _] for _ in my_list]

output:

['on3', 'two', 'three']

Đã trả lời ngày 15 tháng 11 năm 2017 lúc 7:46Nov 15, 2017 at 7:46

Mahesh Kariamahesh KariaMahesh Karia

2.0351 Huy hiệu vàng11 Huy hiệu bạc21 Huy hiệu đồng1 gold badge11 silver badges21 bronze badges

Sử dụng phương thức

import re
my_list= ["on@3", "two#", "thre%e"]
print [re.sub['[^a-zA-Z0-9]+', '', _] for _ in my_list]
6 để áp dụng cùng một bảng dịch cho tất cả các chuỗi:

removetable = str.maketrans['', '', '@#%']
out_list = [s.translate[removetable] for s in my_list]

Phương pháp tĩnh

import re
my_list= ["on@3", "two#", "thre%e"]
print [re.sub['[^a-zA-Z0-9]+', '', _] for _ in my_list]
7 là một công cụ hữu ích để tạo ra bản đồ dịch; Hai đối số đầu tiên là các chuỗi trống vì bạn không thay thế các ký tự, chỉ xóa. Chuỗi thứ ba chứa tất cả các ký tự bạn muốn xóa.

Demo:

>>> my_list = ["on@3", "two#", "thre%e"]
>>> removetable = str.maketrans['', '', '@#%']
>>> [s.translate[removetable] for s in my_list]
['on3', 'two', 'three']

Đã trả lời ngày 15 tháng 11 năm 2017 lúc 7:43Nov 15, 2017 at 7:43

Martijn Pieters ♦ Martijn PietersMartijn Pieters

994K277 Huy hiệu vàng3915 Huy hiệu bạc3258 Huy hiệu đồng277 gold badges3915 silver badges3258 bronze badges

3

thử cái này:

l_in = ["on@3", "two#", "thre%e"]
l_out = [''.join[e for e in string if e.isalnum[]] for string in l_in]
print l_out
>['on3', 'two', 'three']

Đã trả lời ngày 15 tháng 11 năm 2017 lúc 7:46Nov 15, 2017 at 7:46

Mahesh Kariamahesh Karia

l = ['@','#','%']
out_list = []
for x in my_list:
    for y in l:
        if y in x:
            x = x.replace[y,'']
            out_list.append[x]
            break

2.0351 Huy hiệu vàng11 Huy hiệu bạc21 Huy hiệu đồng

out_list = [ x.replace[y,'']  for x in my_list for y in l if y in x ]

Sử dụng phương thức

import re
my_list= ["on@3", "two#", "thre%e"]
print [re.sub['[^a-zA-Z0-9]+', '', _] for _ in my_list]
6 để áp dụng cùng một bảng dịch cho tất cả các chuỗi:

Phương pháp tĩnh

import re
my_list= ["on@3", "two#", "thre%e"]
print [re.sub['[^a-zA-Z0-9]+', '', _] for _ in my_list]
7 là một công cụ hữu ích để tạo ra bản đồ dịch; Hai đối số đầu tiên là các chuỗi trống vì bạn không thay thế các ký tự, chỉ xóa. Chuỗi thứ ba chứa tất cả các ký tự bạn muốn xóa.Nov 15, 2017 at 7:39

Đã trả lời ngày 15 tháng 11 năm 2017 lúc 7:43Van Peer

Martijn Pieters ♦ Martijn Pieters2 gold badges22 silver badges34 bronze badges

5

>>> my_list = ["on@3", "two#", "thre%e"]
>>> removetable = str.maketrans['', '', '@#%']
>>> [s.translate[removetable] for s in my_list]
['on3', 'two', 'three']
9
['on3', 'two', 'three']
3
import re
my_list= ["on@3", "two#", "thre%e"]
print [re.sub['[^a-zA-Z0-9]+', '', _] for _ in my_list]
49

Xóa ký hiệu khỏi chuỗi bằng str.alsalnum []

Phương thức Python String isalnum [] kiểm tra xem tất cả các ký tự trong một chuỗi nhất định có phải là chữ và số hay không. Nó trả về một boolean là đúng - nếu tất cả các ký tự là chữ và khác hoặc sai - nếu một hoặc nhiều ký tự không phải là chữ và số.

Python3

['on3', 'two', 'three']
2
['on3', 'two', 'three']
3
['on3', 'two', 'three']
4

['on3', 'two', 'three']
5
['on3', 'two', 'three']
3
['on3', 'two', 'three']
7
['on3', 'two', 'three']
8
['on3', 'two', 'three']
9
removetable = str.maketrans['', '', '@#%']
out_list = [s.translate[removetable] for s in my_list]
0
['on3', 'two', 'three']
2
removetable = str.maketrans['', '', '@#%']
out_list = [s.translate[removetable] for s in my_list]
2
removetable = str.maketrans['', '', '@#%']
out_list = [s.translate[removetable] for s in my_list]
3

removetable = str.maketrans['', '', '@#%']
out_list = [s.translate[removetable] for s in my_list]
4
removetable = str.maketrans['', '', '@#%']
out_list = [s.translate[removetable] for s in my_list]
5

Output:

GeeksforGeeks

Loại bỏ ký hiệu khỏi chuỗi bằng cách sử dụng thay thế [] & nbsp;

Người ta có thể sử dụng str.replace [] bên trong một vòng lặp để kiểm tra BAD_CHAR và sau đó thay thế nó bằng chuỗi trống do đó loại bỏ nó. Đây là cách tiếp cận cơ bản nhất và không hiệu quả trên quan điểm hiệu suất.

Python3

removetable = str.maketrans['', '', '@#%']
out_list = [s.translate[removetable] for s in my_list]
6
['on3', 'two', 'three']
3
removetable = str.maketrans['', '', '@#%']
out_list = [s.translate[removetable] for s in my_list]
8
removetable = str.maketrans['', '', '@#%']
out_list = [s.translate[removetable] for s in my_list]
9
>>> my_list = ["on@3", "two#", "thre%e"]
>>> removetable = str.maketrans['', '', '@#%']
>>> [s.translate[removetable] for s in my_list]
['on3', 'two', 'three']
0
>>> my_list = ["on@3", "two#", "thre%e"]
>>> removetable = str.maketrans['', '', '@#%']
>>> [s.translate[removetable] for s in my_list]
['on3', 'two', 'three']
1
>>> my_list = ["on@3", "two#", "thre%e"]
>>> removetable = str.maketrans['', '', '@#%']
>>> [s.translate[removetable] for s in my_list]
['on3', 'two', 'three']
0
>>> my_list = ["on@3", "two#", "thre%e"]
>>> removetable = str.maketrans['', '', '@#%']
>>> [s.translate[removetable] for s in my_list]
['on3', 'two', 'three']
3
>>> my_list = ["on@3", "two#", "thre%e"]
>>> removetable = str.maketrans['', '', '@#%']
>>> [s.translate[removetable] for s in my_list]
['on3', 'two', 'three']
0
>>> my_list = ["on@3", "two#", "thre%e"]
>>> removetable = str.maketrans['', '', '@#%']
>>> [s.translate[removetable] for s in my_list]
['on3', 'two', 'three']
5
>>> my_list = ["on@3", "two#", "thre%e"]
>>> removetable = str.maketrans['', '', '@#%']
>>> [s.translate[removetable] for s in my_list]
['on3', 'two', 'three']
0
>>> my_list = ["on@3", "two#", "thre%e"]
>>> removetable = str.maketrans['', '', '@#%']
>>> [s.translate[removetable] for s in my_list]
['on3', 'two', 'three']
7____48

>>> my_list = ["on@3", "two#", "thre%e"]
>>> removetable = str.maketrans['', '', '@#%']
>>> [s.translate[removetable] for s in my_list]
['on3', 'two', 'three']
9
['on3', 'two', 'three']
3
['on3', 'two', 'three']
4

removetable = str.maketrans['', '', '@#%']
out_list = [s.translate[removetable] for s in my_list]
4
l_in = ["on@3", "two#", "thre%e"]
l_out = [''.join[e for e in string if e.isalnum[]] for string in l_in]
print l_out
>['on3', 'two', 'three']
3
l_in = ["on@3", "two#", "thre%e"]
l_out = [''.join[e for e in string if e.isalnum[]] for string in l_in]
print l_out
>['on3', 'two', 'three']
4
l_in = ["on@3", "two#", "thre%e"]
l_out = [''.join[e for e in string if e.isalnum[]] for string in l_in]
print l_out
>['on3', 'two', 'three']
5
l_in = ["on@3", "two#", "thre%e"]
l_out = [''.join[e for e in string if e.isalnum[]] for string in l_in]
print l_out
>['on3', 'two', 'three']
6

['on3', 'two', 'three']
8
l_in = ["on@3", "two#", "thre%e"]
l_out = [''.join[e for e in string if e.isalnum[]] for string in l_in]
print l_out
>['on3', 'two', 'three']
8
removetable = str.maketrans['', '', '@#%']
out_list = [s.translate[removetable] for s in my_list]
0
l = ['@','#','%']
out_list = []
for x in my_list:
    for y in l:
        if y in x:
            x = x.replace[y,'']
            out_list.append[x]
            break
0

l = ['@','#','%']
out_list = []
for x in my_list:
    for y in l:
        if y in x:
            x = x.replace[y,'']
            out_list.append[x]
            break
1
>>> my_list = ["on@3", "two#", "thre%e"]
>>> removetable = str.maketrans['', '', '@#%']
>>> [s.translate[removetable] for s in my_list]
['on3', 'two', 'three']
9
['on3', 'two', 'three']
3
l = ['@','#','%']
out_list = []
for x in my_list:
    for y in l:
        if y in x:
            x = x.replace[y,'']
            out_list.append[x]
            break
4

removetable = str.maketrans['', '', '@#%']
out_list = [s.translate[removetable] for s in my_list]
4
l_in = ["on@3", "two#", "thre%e"]
l_out = [''.join[e for e in string if e.isalnum[]] for string in l_in]
print l_out
>['on3', 'two', 'three']
3
l = ['@','#','%']
out_list = []
for x in my_list:
    for y in l:
        if y in x:
            x = x.replace[y,'']
            out_list.append[x]
            break
7
l_in = ["on@3", "two#", "thre%e"]
l_out = [''.join[e for e in string if e.isalnum[]] for string in l_in]
print l_out
>['on3', 'two', 'three']
5
l = ['@','#','%']
out_list = []
for x in my_list:
    for y in l:
        if y in x:
            x = x.replace[y,'']
            out_list.append[x]
            break
9
out_list = [ x.replace[y,'']  for x in my_list for y in l if y in x ]
0

Đầu ra: & nbsp; 

Original String : Ge;ek*s:fo!r;Ge*e*k:s!
Resultant list is : GeeksforGeeks

Xóa ký hiệu khỏi chuỗi bằng cách sử dụng Jop [] + Generator & nbsp; 

Bằng cách sử dụng python tham gia [], chúng tôi làm lại chuỗi. Trong hàm Trình tạo, chúng tôi chỉ định logic để bỏ qua các ký tự trong bad_chars và do đó xây dựng một chuỗi mới không có ký tự xấu.

Python3

removetable = str.maketrans['', '', '@#%']
out_list = [s.translate[removetable] for s in my_list]
6
['on3', 'two', 'three']
3
removetable = str.maketrans['', '', '@#%']
out_list = [s.translate[removetable] for s in my_list]
8
removetable = str.maketrans['', '', '@#%']
out_list = [s.translate[removetable] for s in my_list]
9
>>> my_list = ["on@3", "two#", "thre%e"]
>>> removetable = str.maketrans['', '', '@#%']
>>> [s.translate[removetable] for s in my_list]
['on3', 'two', 'three']
0
>>> my_list = ["on@3", "two#", "thre%e"]
>>> removetable = str.maketrans['', '', '@#%']
>>> [s.translate[removetable] for s in my_list]
['on3', 'two', 'three']
1
>>> my_list = ["on@3", "two#", "thre%e"]
>>> removetable = str.maketrans['', '', '@#%']
>>> [s.translate[removetable] for s in my_list]
['on3', 'two', 'three']
0
>>> my_list = ["on@3", "two#", "thre%e"]
>>> removetable = str.maketrans['', '', '@#%']
>>> [s.translate[removetable] for s in my_list]
['on3', 'two', 'three']
3
>>> my_list = ["on@3", "two#", "thre%e"]
>>> removetable = str.maketrans['', '', '@#%']
>>> [s.translate[removetable] for s in my_list]
['on3', 'two', 'three']
0
>>> my_list = ["on@3", "two#", "thre%e"]
>>> removetable = str.maketrans['', '', '@#%']
>>> [s.translate[removetable] for s in my_list]
['on3', 'two', 'three']
5
>>> my_list = ["on@3", "two#", "thre%e"]
>>> removetable = str.maketrans['', '', '@#%']
>>> [s.translate[removetable] for s in my_list]
['on3', 'two', 'three']
0
>>> my_list = ["on@3", "two#", "thre%e"]
>>> removetable = str.maketrans['', '', '@#%']
>>> [s.translate[removetable] for s in my_list]
['on3', 'two', 'three']
7____48

>>> my_list = ["on@3", "two#", "thre%e"]
>>> removetable = str.maketrans['', '', '@#%']
>>> [s.translate[removetable] for s in my_list]
['on3', 'two', 'three']
9
['on3', 'two', 'three']
3
['on3', 'two', 'three']
4

removetable = str.maketrans['', '', '@#%']
out_list = [s.translate[removetable] for s in my_list]
4
l_in = ["on@3", "two#", "thre%e"]
l_out = [''.join[e for e in string if e.isalnum[]] for string in l_in]
print l_out
>['on3', 'two', 'three']
3
l_in = ["on@3", "two#", "thre%e"]
l_out = [''.join[e for e in string if e.isalnum[]] for string in l_in]
print l_out
>['on3', 'two', 'three']
4
l_in = ["on@3", "two#", "thre%e"]
l_out = [''.join[e for e in string if e.isalnum[]] for string in l_in]
print l_out
>['on3', 'two', 'three']
5
l_in = ["on@3", "two#", "thre%e"]
l_out = [''.join[e for e in string if e.isalnum[]] for string in l_in]
print l_out
>['on3', 'two', 'three']
6

['on3', 'two', 'three']
8
l_in = ["on@3", "two#", "thre%e"]
l_out = [''.join[e for e in string if e.isalnum[]] for string in l_in]
print l_out
>['on3', 'two', 'three']
8
removetable = str.maketrans['', '', '@#%']
out_list = [s.translate[removetable] for s in my_list]
0
l = ['@','#','%']
out_list = []
for x in my_list:
    for y in l:
        if y in x:
            x = x.replace[y,'']
            out_list.append[x]
            break
0

removetable = str.maketrans['', '', '@#%']
out_list = [s.translate[removetable] for s in my_list]
4
l_in = ["on@3", "two#", "thre%e"]
l_out = [''.join[e for e in string if e.isalnum[]] for string in l_in]
print l_out
>['on3', 'two', 'three']
3
l = ['@','#','%']
out_list = []
for x in my_list:
    for y in l:
        if y in x:
            x = x.replace[y,'']
            out_list.append[x]
            break
7
l_in = ["on@3", "two#", "thre%e"]
l_out = [''.join[e for e in string if e.isalnum[]] for string in l_in]
print l_out
>['on3', 'two', 'three']
5
l = ['@','#','%']
out_list = []
for x in my_list:
    for y in l:
        if y in x:
            x = x.replace[y,'']
            out_list.append[x]
            break
9
out_list = [ x.replace[y,'']  for x in my_list for y in l if y in x ]
0

Đầu ra: & nbsp; 

Original String : Ge;ek*s:fo!r;Ge*e*k:s!
Resultant list is : GeeksforGeeks

Xóa ký hiệu khỏi chuỗi bằng cách sử dụng Jop [] + Generator & nbsp;translate[] 

Bằng cách sử dụng python tham gia [], chúng tôi làm lại chuỗi. Trong hàm Trình tạo, chúng tôi chỉ định logic để bỏ qua các ký tự trong bad_chars và do đó xây dựng một chuỗi mới không có ký tự xấu.

Python3

>>> my_list = ["on@3", "two#", "thre%e"]
>>> removetable = str.maketrans['', '', '@#%']
>>> [s.translate[removetable] for s in my_list]
['on3', 'two', 'three']
9
['on3', 'two', 'three']
3
Original String : Ge;ek*s:fo!r;Ge*e*k:s!
Resultant list is : GeeksforGeeks
4
['on3', 'two', 'three']
8
l_in = ["on@3", "two#", "thre%e"]
l_out = [''.join[e for e in string if e.isalnum[]] for string in l_in]
print l_out
>['on3', 'two', 'three']
8
removetable = str.maketrans['', '', '@#%']
out_list = [s.translate[removetable] for s in my_list]
0
>>> my_list = ["on@3", "two#", "thre%e"]
>>> removetable = str.maketrans['', '', '@#%']
>>> [s.translate[removetable] for s in my_list]
['on3', 'two', 'three']
9
removetable = str.maketrans['', '', '@#%']
out_list = [s.translate[removetable] for s in my_list]
2

Xóa ký hiệu khỏi chuỗi bằng cách sử dụng dịch [] & nbsp;

>>> my_list = ["on@3", "two#", "thre%e"]
>>> removetable = str.maketrans['', '', '@#%']
>>> [s.translate[removetable] for s in my_list]
['on3', 'two', 'three']
9
['on3', 'two', 'three']
3
['on3', 'two', 'three']
4

removetable = str.maketrans['', '', '@#%']
out_list = [s.translate[removetable] for s in my_list]
4
l_in = ["on@3", "two#", "thre%e"]
l_out = [''.join[e for e in string if e.isalnum[]] for string in l_in]
print l_out
>['on3', 'two', 'three']
3
l_in = ["on@3", "two#", "thre%e"]
l_out = [''.join[e for e in string if e.isalnum[]] for string in l_in]
print l_out
>['on3', 'two', 'three']
4
l_in = ["on@3", "two#", "thre%e"]
l_out = [''.join[e for e in string if e.isalnum[]] for string in l_in]
print l_out
>['on3', 'two', 'three']
5
l_in = ["on@3", "two#", "thre%e"]
l_out = [''.join[e for e in string if e.isalnum[]] for string in l_in]
print l_out
>['on3', 'two', 'three']
6

['on3', 'two', 'three']
8
l_in = ["on@3", "two#", "thre%e"]
l_out = [''.join[e for e in string if e.isalnum[]] for string in l_in]
print l_out
>['on3', 'two', 'three']
8
removetable = str.maketrans['', '', '@#%']
out_list = [s.translate[removetable] for s in my_list]
0
l = ['@','#','%']
out_list = []
for x in my_list:
    for y in l:
        if y in x:
            x = x.replace[y,'']
            out_list.append[x]
            break
0

l = ['@','#','%']
out_list = []
for x in my_list:
    for y in l:
        if y in x:
            x = x.replace[y,'']
            out_list.append[x]
            break
1
>>> my_list = ["on@3", "two#", "thre%e"]
>>> removetable = str.maketrans['', '', '@#%']
>>> [s.translate[removetable] for s in my_list]
['on3', 'two', 'three']
9
['on3', 'two', 'three']
3
l = ['@','#','%']
out_list = []
for x in my_list:
    for y in l:
        if y in x:
            x = x.replace[y,'']
            out_list.append[x]
            break
4

removetable = str.maketrans['', '', '@#%']
out_list = [s.translate[removetable] for s in my_list]
4
l_in = ["on@3", "two#", "thre%e"]
l_out = [''.join[e for e in string if e.isalnum[]] for string in l_in]
print l_out
>['on3', 'two', 'three']
3
l = ['@','#','%']
out_list = []
for x in my_list:
    for y in l:
        if y in x:
            x = x.replace[y,'']
            out_list.append[x]
            break
7
l_in = ["on@3", "two#", "thre%e"]
l_out = [''.join[e for e in string if e.isalnum[]] for string in l_in]
print l_out
>['on3', 'two', 'three']
5
l = ['@','#','%']
out_list = []
for x in my_list:
    for y in l:
        if y in x:
            x = x.replace[y,'']
            out_list.append[x]
            break
9
out_list = [ x.replace[y,'']  for x in my_list for y in l if y in x ]
0

Đầu ra: & nbsp;

removetable = str.maketrans['', '', '@#%']
out_list = [s.translate[removetable] for s in my_list]
4
l_in = ["on@3", "two#", "thre%e"]
l_out = [''.join[e for e in string if e.isalnum[]] for string in l_in]
print l_out
>['on3', 'two', 'three']
3
l = ['@','#','%']
out_list = []
for x in my_list:
    for y in l:
        if y in x:
            x = x.replace[y,'']
            out_list.append[x]
            break
7
l_in = ["on@3", "two#", "thre%e"]
l_out = [''.join[e for e in string if e.isalnum[]] for string in l_in]
print l_out
>['on3', 'two', 'three']
5
l = ['@','#','%']
out_list = []
for x in my_list:
    for y in l:
        if y in x:
            x = x.replace[y,'']
            out_list.append[x]
            break
9
out_list = [ x.replace[y,'']  for x in my_list for y in l if y in x ]
0

Đầu ra: & nbsp; 

Xóa ký hiệu khỏi chuỗi bằng cách sử dụng Jop [] + Generator & nbsp;

Bằng cách sử dụng python tham gia [], chúng tôi làm lại chuỗi. Trong hàm Trình tạo, chúng tôi chỉ định logic để bỏ qua các ký tự trong bad_chars và do đó xây dựng một chuỗi mới không có ký tự xấu. filter[] 

>>> my_list = ["on@3", "two#", "thre%e"]
>>> removetable = str.maketrans['', '', '@#%']
>>> [s.translate[removetable] for s in my_list]
['on3', 'two', 'three']
9
['on3', 'two', 'three']
3
Original String : Ge;ek*s:fo!r;Ge*e*k:s!
Resultant list is : GeeksforGeeks
4
['on3', 'two', 'three']
8
l_in = ["on@3", "two#", "thre%e"]
l_out = [''.join[e for e in string if e.isalnum[]] for string in l_in]
print l_out
>['on3', 'two', 'three']
8
removetable = str.maketrans['', '', '@#%']
out_list = [s.translate[removetable] for s in my_list]
0
>>> my_list = ["on@3", "two#", "thre%e"]
>>> removetable = str.maketrans['', '', '@#%']
>>> [s.translate[removetable] for s in my_list]
['on3', 'two', 'three']
9
removetable = str.maketrans['', '', '@#%']
out_list = [s.translate[removetable] for s in my_list]
2

Python3

Xóa ký hiệu khỏi chuỗi bằng cách sử dụng dịch [] & nbsp;

Cách thanh lịch nhất để thực hiện nhiệm vụ cụ thể này, phương pháp này về cơ bản được sử dụng để đạt được giải pháp cho loại vấn đề này, chúng ta có thể dịch từng BAD_CHAR thành một chuỗi trống và lấy chuỗi được lọc.

removetable = str.maketrans['', '', '@#%']
out_list = [s.translate[removetable] for s in my_list]
4
l_in = ["on@3", "two#", "thre%e"]
l_out = [''.join[e for e in string if e.isalnum[]] for string in l_in]
print l_out
>['on3', 'two', 'three']
3
l_in = ["on@3", "two#", "thre%e"]
l_out = [''.join[e for e in string if e.isalnum[]] for string in l_in]
print l_out
>['on3', 'two', 'three']
4
l_in = ["on@3", "two#", "thre%e"]
l_out = [''.join[e for e in string if e.isalnum[]] for string in l_in]
print l_out
>['on3', 'two', 'three']
5
l_in = ["on@3", "two#", "thre%e"]
l_out = [''.join[e for e in string if e.isalnum[]] for string in l_in]
print l_out
>['on3', 'two', 'three']
6

['on3', 'two', 'three']
8
l_in = ["on@3", "two#", "thre%e"]
l_out = [''.join[e for e in string if e.isalnum[]] for string in l_in]
print l_out
>['on3', 'two', 'three']
8
removetable = str.maketrans['', '', '@#%']
out_list = [s.translate[removetable] for s in my_list]
0
l = ['@','#','%']
out_list = []
for x in my_list:
    for y in l:
        if y in x:
            x = x.replace[y,'']
            out_list.append[x]
            break
0

import re
my_list= ["on@3", "two#", "thre%e"]
print [re.sub['[^a-zA-Z0-9]+', '', _] for _ in my_list]
85
import re
my_list= ["on@3", "two#", "thre%e"]
print [re.sub['[^a-zA-Z0-9]+', '', _] for _ in my_list]
86

removetable = str.maketrans['', '', '@#%']
out_list = [s.translate[removetable] for s in my_list]
4
l_in = ["on@3", "two#", "thre%e"]
l_out = [''.join[e for e in string if e.isalnum[]] for string in l_in]
print l_out
>['on3', 'two', 'three']
3
l = ['@','#','%']
out_list = []
for x in my_list:
    for y in l:
        if y in x:
            x = x.replace[y,'']
            out_list.append[x]
            break
7
l_in = ["on@3", "two#", "thre%e"]
l_out = [''.join[e for e in string if e.isalnum[]] for string in l_in]
print l_out
>['on3', 'two', 'three']
5
l = ['@','#','%']
out_list = []
for x in my_list:
    for y in l:
        if y in x:
            x = x.replace[y,'']
            out_list.append[x]
            break
9
out_list = [ x.replace[y,'']  for x in my_list for y in l if y in x ]
0

Output:  

Original String : Ge;ek*s:fo!r;Ge*e*k:s!
Resultant list is : GeeksforGeeks

Đầu ra: & nbsp;re.sub[] function: 

Xóa ký hiệu khỏi chuỗi bằng cách sử dụng Jop [] + Generator & nbsp;

Python3

Bằng cách sử dụng python tham gia [], chúng tôi làm lại chuỗi. Trong hàm Trình tạo, chúng tôi chỉ định logic để bỏ qua các ký tự trong bad_chars và do đó xây dựng một chuỗi mới không có ký tự xấu.

>>> my_list = ["on@3", "two#", "thre%e"]
>>> removetable = str.maketrans['', '', '@#%']
>>> [s.translate[removetable] for s in my_list]
['on3', 'two', 'three']
9
['on3', 'two', 'three']
3
Original String : Ge;ek*s:fo!r;Ge*e*k:s!
Resultant list is : GeeksforGeeks
4
['on3', 'two', 'three']
8
l_in = ["on@3", "two#", "thre%e"]
l_out = [''.join[e for e in string if e.isalnum[]] for string in l_in]
print l_out
>['on3', 'two', 'three']
8
removetable = str.maketrans['', '', '@#%']
out_list = [s.translate[removetable] for s in my_list]
0
>>> my_list = ["on@3", "two#", "thre%e"]
>>> removetable = str.maketrans['', '', '@#%']
>>> [s.translate[removetable] for s in my_list]
['on3', 'two', 'three']
9
removetable = str.maketrans['', '', '@#%']
out_list = [s.translate[removetable] for s in my_list]
2

Xóa ký hiệu khỏi chuỗi bằng cách sử dụng dịch [] & nbsp;

Cách thanh lịch nhất để thực hiện nhiệm vụ cụ thể này, phương pháp này về cơ bản được sử dụng để đạt được giải pháp cho loại vấn đề này, chúng ta có thể dịch từng BAD_CHAR thành một chuỗi trống và lấy chuỗi được lọc.

import re
my_list= ["on@3", "two#", "thre%e"]
print [re.sub['[^a-zA-Z0-9]+', '', _] for _ in my_list]
10
import re
my_list= ["on@3", "two#", "thre%e"]
print [re.sub['[^a-zA-Z0-9]+', '', _] for _ in my_list]
11

removetable = str.maketrans['', '', '@#%']
out_list = [s.translate[removetable] for s in my_list]
6
['on3', 'two', 'three']
3
removetable = str.maketrans['', '', '@#%']
out_list = [s.translate[removetable] for s in my_list]
8
removetable = str.maketrans['', '', '@#%']
out_list = [s.translate[removetable] for s in my_list]
9
>>> my_list = ["on@3", "two#", "thre%e"]
>>> removetable = str.maketrans['', '', '@#%']
>>> [s.translate[removetable] for s in my_list]
['on3', 'two', 'three']
0
>>> my_list = ["on@3", "two#", "thre%e"]
>>> removetable = str.maketrans['', '', '@#%']
>>> [s.translate[removetable] for s in my_list]
['on3', 'two', 'three']
1
>>> my_list = ["on@3", "two#", "thre%e"]
>>> removetable = str.maketrans['', '', '@#%']
>>> [s.translate[removetable] for s in my_list]
['on3', 'two', 'three']
0
>>> my_list = ["on@3", "two#", "thre%e"]
>>> removetable = str.maketrans['', '', '@#%']
>>> [s.translate[removetable] for s in my_list]
['on3', 'two', 'three']
3
>>> my_list = ["on@3", "two#", "thre%e"]
>>> removetable = str.maketrans['', '', '@#%']
>>> [s.translate[removetable] for s in my_list]
['on3', 'two', 'three']
0
>>> my_list = ["on@3", "two#", "thre%e"]
>>> removetable = str.maketrans['', '', '@#%']
>>> [s.translate[removetable] for s in my_list]
['on3', 'two', 'three']
5
>>> my_list = ["on@3", "two#", "thre%e"]
>>> removetable = str.maketrans['', '', '@#%']
>>> [s.translate[removetable] for s in my_list]
['on3', 'two', 'three']
8

import re
my_list= ["on@3", "two#", "thre%e"]
print [re.sub['[^a-zA-Z0-9]+', '', _] for _ in my_list]
31
['on3', 'two', 'three']
3
import re
my_list= ["on@3", "two#", "thre%e"]
print [re.sub['[^a-zA-Z0-9]+', '', _] for _ in my_list]
33
['on3', 'two', 'three']
8
import re
my_list= ["on@3", "two#", "thre%e"]
print [re.sub['[^a-zA-Z0-9]+', '', _] for _ in my_list]
35
removetable = str.maketrans['', '', '@#%']
out_list = [s.translate[removetable] for s in my_list]
0
import re
my_list= ["on@3", "two#", "thre%e"]
print [re.sub['[^a-zA-Z0-9]+', '', _] for _ in my_list]
37

import re
my_list= ["on@3", "two#", "thre%e"]
print [re.sub['[^a-zA-Z0-9]+', '', _] for _ in my_list]
38
import re
my_list= ["on@3", "two#", "thre%e"]
print [re.sub['[^a-zA-Z0-9]+', '', _] for _ in my_list]
39
import re
my_list= ["on@3", "two#", "thre%e"]
print [re.sub['[^a-zA-Z0-9]+', '', _] for _ in my_list]
40
['on3', 'two', 'three']
3
import re
my_list= ["on@3", "two#", "thre%e"]
print [re.sub['[^a-zA-Z0-9]+', '', _] for _ in my_list]
42

import re
my_list= ["on@3", "two#", "thre%e"]
print [re.sub['[^a-zA-Z0-9]+', '', _] for _ in my_list]
43
['on3', 'two', 'three']
3
l = ['@','#','%']
out_list = []
for x in my_list:
    for y in l:
        if y in x:
            x = x.replace[y,'']
            out_list.append[x]
            break
9
import re
my_list= ["on@3", "two#", "thre%e"]
print [re.sub['[^a-zA-Z0-9]+', '', _] for _ in my_list]
46

Output:  

import re
my_list= ["on@3", "two#", "thre%e"]
print [re.sub['[^a-zA-Z0-9]+', '', _] for _ in my_list]
3

>>> my_list = ["on@3", "two#", "thre%e"]
>>> removetable = str.maketrans['', '', '@#%']
>>> [s.translate[removetable] for s in my_list]
['on3', 'two', 'three']
9
['on3', 'two', 'three']
3
import re
my_list= ["on@3", "two#", "thre%e"]
print [re.sub['[^a-zA-Z0-9]+', '', _] for _ in my_list]
49

Original String : Ge;ek*s:fo!r;Ge*e*k:s!
Resultant list is : GeeksforGeeks

Python3

removetable = str.maketrans['', '', '@#%']
out_list = [s.translate[removetable] for s in my_list]
6
['on3', 'two', 'three']
3
removetable = str.maketrans['', '', '@#%']
out_list = [s.translate[removetable] for s in my_list]
8
removetable = str.maketrans['', '', '@#%']
out_list = [s.translate[removetable] for s in my_list]
9
>>> my_list = ["on@3", "two#", "thre%e"]
>>> removetable = str.maketrans['', '', '@#%']
>>> [s.translate[removetable] for s in my_list]
['on3', 'two', 'three']
0
>>> my_list = ["on@3", "two#", "thre%e"]
>>> removetable = str.maketrans['', '', '@#%']
>>> [s.translate[removetable] for s in my_list]
['on3', 'two', 'three']
1
>>> my_list = ["on@3", "two#", "thre%e"]
>>> removetable = str.maketrans['', '', '@#%']
>>> [s.translate[removetable] for s in my_list]
['on3', 'two', 'three']
0
>>> my_list = ["on@3", "two#", "thre%e"]
>>> removetable = str.maketrans['', '', '@#%']
>>> [s.translate[removetable] for s in my_list]
['on3', 'two', 'three']
3
>>> my_list = ["on@3", "two#", "thre%e"]
>>> removetable = str.maketrans['', '', '@#%']
>>> [s.translate[removetable] for s in my_list]
['on3', 'two', 'three']
0
>>> my_list = ["on@3", "two#", "thre%e"]
>>> removetable = str.maketrans['', '', '@#%']
>>> [s.translate[removetable] for s in my_list]
['on3', 'two', 'three']
5
>>> my_list = ["on@3", "two#", "thre%e"]
>>> removetable = str.maketrans['', '', '@#%']
>>> [s.translate[removetable] for s in my_list]
['on3', 'two', 'three']
0
>>> my_list = ["on@3", "two#", "thre%e"]
>>> removetable = str.maketrans['', '', '@#%']
>>> [s.translate[removetable] for s in my_list]
['on3', 'two', 'three']
7____48

>>> my_list = ["on@3", "two#", "thre%e"]
>>> removetable = str.maketrans['', '', '@#%']
>>> [s.translate[removetable] for s in my_list]
['on3', 'two', 'three']
9
['on3', 'two', 'three']
3
['on3', 'two', 'three']
4

removetable = str.maketrans['', '', '@#%']
out_list = [s.translate[removetable] for s in my_list]
4
l_in = ["on@3", "two#", "thre%e"]
l_out = [''.join[e for e in string if e.isalnum[]] for string in l_in]
print l_out
>['on3', 'two', 'three']
3
l_in = ["on@3", "two#", "thre%e"]
l_out = [''.join[e for e in string if e.isalnum[]] for string in l_in]
print l_out
>['on3', 'two', 'three']
4
l_in = ["on@3", "two#", "thre%e"]
l_out = [''.join[e for e in string if e.isalnum[]] for string in l_in]
print l_out
>['on3', 'two', 'three']
5
l_in = ["on@3", "two#", "thre%e"]
l_out = [''.join[e for e in string if e.isalnum[]] for string in l_in]
print l_out
>['on3', 'two', 'three']
6

['on3', 'two', 'three']
60
['on3', 'two', 'three']
3
['on3', 'two', 'three']
62

['on3', 'two', 'three']
8
l_in = ["on@3", "two#", "thre%e"]
l_out = [''.join[e for e in string if e.isalnum[]] for string in l_in]
print l_out
>['on3', 'two', 'three']
8
removetable = str.maketrans['', '', '@#%']
out_list = [s.translate[removetable] for s in my_list]
0
l = ['@','#','%']
out_list = []
for x in my_list:
    for y in l:
        if y in x:
            x = x.replace[y,'']
            out_list.append[x]
            break
0

l = ['@','#','%']
out_list = []
for x in my_list:
    for y in l:
        if y in x:
            x = x.replace[y,'']
            out_list.append[x]
            break
1
>>> my_list = ["on@3", "two#", "thre%e"]
>>> removetable = str.maketrans['', '', '@#%']
>>> [s.translate[removetable] for s in my_list]
['on3', 'two', 'three']
9
['on3', 'two', 'three']
3
l = ['@','#','%']
out_list = []
for x in my_list:
    for y in l:
        if y in x:
            x = x.replace[y,'']
            out_list.append[x]
            break
4

['on3', 'two', 'three']
73
['on3', 'two', 'three']
60
l_in = ["on@3", "two#", "thre%e"]
l_out = [''.join[e for e in string if e.isalnum[]] for string in l_in]
print l_out
>['on3', 'two', 'three']
5
['on3', 'two', 'three']
3
['on3', 'two', 'three']
27

removetable = str.maketrans['', '', '@#%']
out_list = [s.translate[removetable] for s in my_list]
4
l_in = ["on@3", "two#", "thre%e"]
l_out = [''.join[e for e in string if e.isalnum[]] for string in l_in]
print l_out
>['on3', 'two', 'three']
3
l = ['@','#','%']
out_list = []
for x in my_list:
    for y in l:
        if y in x:
            x = x.replace[y,'']
            out_list.append[x]
            break
7
l_in = ["on@3", "two#", "thre%e"]
l_out = [''.join[e for e in string if e.isalnum[]] for string in l_in]
print l_out
>['on3', 'two', 'three']
5
l = ['@','#','%']
out_list = []
for x in my_list:
    for y in l:
        if y in x:
            x = x.replace[y,'']
            out_list.append[x]
            break
9
out_list = [ x.replace[y,'']  for x in my_list for y in l if y in x ]
0

Đầu ra

import re
my_list= ["on@3", "two#", "thre%e"]
print [re.sub['[^a-zA-Z0-9]+', '', _] for _ in my_list]
4


Làm thế nào để bạn xóa các ký tự khỏi danh sách trong Python?

Phương thức Remove [] xóa phần tử khớp đầu tiên [được truyền dưới dạng đối số] khỏi danh sách.Phương thức pop [] loại bỏ một phần tử tại một chỉ mục nhất định và cũng sẽ trả về mục đã xóa.Bạn cũng có thể sử dụng từ khóa DEL trong Python để xóa một phần tử hoặc lát khỏi danh sách.. The pop[] method removes an element at a given index, and will also return the removed item. You can also use the del keyword in Python to remove an element or slice from a list.

Làm thế nào để bạn loại bỏ các yếu tố không mong muốn khỏi danh sách trong Python?

Phương thức Remove [] là một trong những cách bạn có thể xóa các phần tử khỏi danh sách trong Python.Phương thức Remove [] xóa một mục khỏi danh sách theo giá trị của nó chứ không phải bằng số chỉ mục của nó. is one of the ways you can remove elements from a list in Python. The remove[] method removes an item from a list by its value and not by its index number.

Bài Viết Liên Quan

Chủ Đề