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 Pieters♦Martijn 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]
49Xó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']
9removetable = str.maketrans['', '', '@#%']
out_list = [s.translate[removetable] for s in my_list]
0 ['on3', 'two', 'three']
2removetable = 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]
3removetable = str.maketrans['', '', '@#%']
out_list = [s.translate[removetable] for s in my_list]
4removetable = str.maketrans['', '', '@#%']
out_list = [s.translate[removetable] for s in my_list]
5Output:
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]
8removetable = 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']
4removetable = str.maketrans['', '', '@#%']
out_list = [s.translate[removetable] for s in my_list]
4l_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']
3l_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']
8removetable = 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
0l = ['@','#','%']
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
4removetable = str.maketrans['', '', '@#%']
out_list = [s.translate[removetable] for s in my_list]
4l_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']
3l = ['@','#','%']
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
9out_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]
8removetable = 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']
4removetable = str.maketrans['', '', '@#%']
out_list = [s.translate[removetable] for s in my_list]
4l_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']
3l_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']
8removetable = 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
0removetable = str.maketrans['', '', '@#%']
out_list = [s.translate[removetable] for s in my_list]
4l_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']
3l = ['@','#','%']
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
9out_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 : GeeksforGeeks4
['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']
8removetable = 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']
9removetable = str.maketrans['', '', '@#%']
out_list = [s.translate[removetable] for s in my_list]
2Xó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']
4removetable = str.maketrans['', '', '@#%']
out_list = [s.translate[removetable] for s in my_list]
4l_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']
3l_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']
8removetable = 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
0l = ['@','#','%']
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
4removetable = str.maketrans['', '', '@#%']
out_list = [s.translate[removetable] for s in my_list]
4l_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']
3l = ['@','#','%']
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
9out_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]
4l_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']
3l = ['@','#','%']
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
9out_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 : GeeksforGeeks4
['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']
8removetable = 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']
9removetable = str.maketrans['', '', '@#%']
out_list = [s.translate[removetable] for s in my_list]
2Python3
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]
4l_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']
3l_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']
8removetable = 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
0import re
my_list= ["on@3", "two#", "thre%e"]
print [re.sub['[^a-zA-Z0-9]+', '', _] for _ in my_list]
85import re
my_list= ["on@3", "two#", "thre%e"]
print [re.sub['[^a-zA-Z0-9]+', '', _] for _ in my_list]
86removetable = str.maketrans['', '', '@#%']
out_list = [s.translate[removetable] for s in my_list]
4l_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']
3l = ['@','#','%']
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
9out_list = [ x.replace[y,''] for x in my_list for y in l if y in x ]
0Output:
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 : GeeksforGeeks4
['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']
8removetable = 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']
9removetable = str.maketrans['', '', '@#%']
out_list = [s.translate[removetable] for s in my_list]
2Xó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]
11removetable = 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]
8removetable = 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']
8import 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]
35removetable = 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]
37import re
my_list= ["on@3", "two#", "thre%e"]
print [re.sub['[^a-zA-Z0-9]+', '', _] for _ in my_list]
38import re
my_list= ["on@3", "two#", "thre%e"]
print [re.sub['[^a-zA-Z0-9]+', '', _] for _ in my_list]
39import 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]
42import 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
9import re
my_list= ["on@3", "two#", "thre%e"]
print [re.sub['[^a-zA-Z0-9]+', '', _] for _ in my_list]
46Output:
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
>>> my_list = ["on@3", "two#", "thre%e"]
>>> removetable = str.maketrans['', '', '@#%']
>>> [s.translate[removetable] for s in my_list]
['on3', 'two', 'three']
['on3', 'two', 'three']
import re
my_list= ["on@3", "two#", "thre%e"]
print [re.sub['[^a-zA-Z0-9]+', '', _] for _ in my_list]
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]
8removetable = 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']
4removetable = str.maketrans['', '', '@#%']
out_list = [s.translate[removetable] for s in my_list]
4l_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']
3l_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']
8removetable = 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
0l = ['@','#','%']
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']
60l_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']
27removetable = str.maketrans['', '', '@#%']
out_list = [s.translate[removetable] for s in my_list]
4l_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']
3l = ['@','#','%']
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
9out_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