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

11

Mới! Lưu câu hỏi hoặc câu trả lời và sắp xếp nội dung yêu thích của bạn. Tìm hiểu thêm.
Learn more.

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.0251 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

991K276 Huy hiệu vàng3904 Huy hiệu bạc3253 Huy hiệu đồng276 gold badges3904 silver badges3253 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.0251 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

Vấn đề chung mà các lập trình viên phải đối mặt là loại bỏ các ký tự không mong muốn khỏi một chuỗi bằng Python. Nhưng đôi khi yêu cầu ở trên và yêu cầu loại bỏ hơn 1 nhân vật, nhưng một danh sách các nhân vật độc hại như vậy. Đây có thể ở dạng ký tự đặc biệt để xây dựng lại mật khẩu hợp lệ và nhiều ứng dụng khác có thể. Vì vậy, nhiệm vụ của chúng tôi là loại bỏ các ký tự không mong muốn khỏi chuỗi.

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

Xóa 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

Xóa ký hiệu khỏi chuỗi bằng hàm re.sub []: & nbsp;re.sub[] function: 

Các biểu thức chính quy được sử dụng để xác định ký tự xấu trong hàm chuỗi và re.sub được sử dụng để thay thế bad_char từ chuỗi. & Nbsp;

Python3

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]
94

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

import re
my_list= ["on@3", "two#", "thre%e"]
print [re.sub['[^a-zA-Z0-9]+', '', _] for _ in my_list]
98
['on3', 'two', 'three']
3
removetable = str.maketrans['', '', '@#%']
out_list = [s.translate[removetable] for s in my_list]
8
['on3', 'two', 'three']
01
>>> my_list = ["on@3", "two#", "thre%e"]
>>> removetable = str.maketrans['', '', '@#%']
>>> [s.translate[removetable] for s in my_list]
['on3', 'two', 'three']
0
['on3', 'two', 'three']
03____40
>>> 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
['on3', 'two', 'three']
07404047474

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
['on3', 'two', 'three']
13
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']
15

['on3', 'two', 'three']
16
['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

['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
['on3', 'two', 'three']
22

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
['on3', 'two', 'three']
16
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']
55____23
['on3', 'two', 'three']
27

['on3', 'two', 'three']
28
['on3', 'two', 'three']
3
['on3', 'two', 'three']
30
['on3', 'two', 'three']
31
['on3', 'two', 'three']
32

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
['on3', 'two', 'three']
35
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
['on3', 'two', 'three']
38

Output:  

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

Xóa ký hiệu khỏi chuỗi bằng cách sử dụng trong, không phải trong các toán tử

Trong và không trong các toán tử được sử dụng để kiểm tra sự hiện diện của các ký tự không mong muốn trong chuỗi.

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
['on3', 'two', 'three']
66

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
removetable = str.maketrans['', '', '@#%']
out_list = [s.translate[removetable] for s in my_list]
2
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
import re
my_list= ["on@3", "two#", "thre%e"]
print [re.sub['[^a-zA-Z0-9]+', '', _] for _ in my_list]
00
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

['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
['on3', 'two', 'three']
83

Đầ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 cách nào để xóa các ký hiệu 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 ký tự không cần thiết khỏi Python?

Sử dụng 'str.Sử dụng str.Thay thế [], chúng ta có thể thay thế một ký tự cụ thể.Nếu chúng ta muốn xóa ký tự cụ thể đó, hãy thay thế ký tự đó bằng một chuỗi trống.replace that character with an empty string.

Làm cách nào để loại bỏ một biểu tượng trong Python?

Bạn có thể xóa một ký tự khỏi chuỗi python bằng cách sử dụng thay thế [] hoặc dịch [].Cả hai phương thức này thay thế một ký tự hoặc chuỗi bằng một giá trị đã cho.using replace[] or translate[]. Both these methods replace a character or string with a given value.

Làm cách nào để loại bỏ các giá trị không mong muốn khỏi danh sách trong Python?

Bạn có thể sử dụng các phương thức danh sách như Remove [], pop [] để xóa phần tử đầu tiên khỏi danh sách.Trong trường hợp của phương thức Remove [], bạn sẽ phải vượt qua phần tử đầu tiên để xóa và để bật chỉ mục, tức là, 0. Bạn cũng có thể sử dụng từ khóa DEL để xóa phần tử đầu tiên khỏi danh sách.make use of list methods like remove[], pop[] to remove the first element from the list. In the case of remove[] method, you will have to pass the first element to be removed and for pop the index, i.e., 0. You may also use the del keyword to remove the first element from the list.

Bài Viết Liên Quan

Chủ Đề