Trong Python, tôi chỉ muốn trích xuất các ký tự từ một chuỗi.
Xem xét tôi có chuỗi sau,
input = "{['players',]: 24, ['year',]: 28, ['money',]: 19, ['ipod',]: 36, ['case',]: 23, ['mini',]: 46}"
Tôi muốn kết quả như,
output = "players year money ipod case mini"
Tôi đã cố gắng phân chia chỉ xem xét các bảng chữ cái,
word1 = st.split["[a-zA-Z]+"]
Nhưng sự chia rẽ không xảy ra.
& nbsp; Phương pháp #3: Sử dụng
output = "players year money ipod case mini"
2word1 = st.split["[a-zA-Z]+"]
7 word1 = st.split["[a-zA-Z]+"]
8word1 = st.split["[a-zA-Z]+"]
9output = "players year money ipod case mini"
01
word1 = st.split["[a-zA-Z]+"]
word1 = st.split["[a-zA-Z]+"]
word1 = st.split["[a-zA-Z]+"]
output = "players year money ipod case mini"
Bạn có thể sử dụng một biểu thức thông thường để chỉ trích xuất các chữ cái [bảng chữ cái] từ một chuỗi trong Python. Bạn cũng có thể lặp lại các ký tự trong một chuỗi và sử dụng hàm chuỗi
output = "players year money ipod case mini"
2 để chỉ giữ các chữ cái trong một chuỗi.
Hãy cùng xem xét cả hai phương pháp với sự trợ giúp của các ví dụ -
Trích xuất bảng chữ cái từ một chuỗi bằng regex
Bạn có thể sử dụng biểu thức thông thường
output = "players year money ipod case mini"
3 để khớp với các ký tự không phải alphabet trong chuỗi và thay thế chúng bằng một chuỗi trống bằng hàm output = "players year money ipod case mini"
4. Chuỗi kết quả sẽ chỉ chứa các chữ cái.Hãy cùng nhìn vào một ví dụ.
import re # string with letters, numbers, and special characters s = "[email protected]" # keep only letters res = re.sub[r'[^a-zA-Z]', '', s] print[res]
Output:
BuckyBarnes
Bạn có thể thấy rằng chuỗi kết quả chỉ chứa các chữ cái.
Sử dụng chức năng chuỗi output = "players year money ipod case mini"
2
output = "players year money ipod case mini"
Ngoài ra, bạn có thể sử dụng hàm chuỗi
output = "players year money ipod case mini"
2 để xóa các ký tự không alphabet khỏi chuỗi. Sử dụng các bước sau -- Tạo một chuỗi trống để lưu trữ chuỗi kết quả của chúng tôi chỉ bằng các chữ cái.
- Lặp qua mỗi ký tự trong chuỗi đã cho của chúng tôi.
- Đối với mỗi ký tự, hãy kiểm tra xem nó có bảng chữ cái sử dụng chức năng chuỗi
2 không. Nếu là, sau đó thêm ký tự vào chuỗi kết quả của chúng tôi.output = "players year money ipod case mini"
Hãy cùng nhìn vào một ví dụ.
# string with letters, numbers, and special characters s = "[email protected]" # keep only letters res = "" for ch in s: if ch.isalpha[]: res += ch print[res]
Output:
BuckyBarnes
Bạn có thể thấy rằng chuỗi kết quả chỉ chứa các chữ cái.
Sử dụng chức năng chuỗi
output = "players year money ipod case mini"
2# string with letters, numbers, and special characters s = "[email protected]" # keep only letters res = "".join[[ch for ch in s if ch.isalpha[]]] print[res]
Output:
BuckyBarnes
Ngoài ra, bạn có thể sử dụng hàm chuỗi
output = "players year money ipod case mini"
2 để xóa các ký tự không alphabet khỏi chuỗi. Sử dụng các bước sau -Tạo một chuỗi trống để lưu trữ chuỗi kết quả của chúng tôi chỉ bằng các chữ cái.
- Lặp qua mỗi ký tự trong chuỗi đã cho của chúng tôi.
- Đối với mỗi ký tự, hãy kiểm tra xem nó có bảng chữ cái sử dụng chức năng chuỗi
2 không. Nếu là, sau đó thêm ký tự vào chuỗi kết quả của chúng tôi.output = "players year money ipod case mini"
- Chuỗi kết quả chỉ chứa các chữ cái từ chuỗi gốc.
Mã trên có thể được giảm xuống ít dòng hơn bằng cách sử dụng danh sách hiểu.
We do not spam and you can opt out any
time.
Chúng tôi nhận được kết quả tương tự như trên.
Bạn cũng có thể quan tâm đến -
Làm thế nào để bạn chỉ chấp nhận bảng chữ cái trong Python?
Chỉ cho phép các chữ cái khi lấy đầu vào của người dùng trong Python #.
Sử dụng vòng lặp thời gian để lặp lại cho đến khi người dùng nhập các chữ cái ..
Làm thế nào để bạn chỉ chấp nhận bảng chữ cái trong Python?
Chỉ cho phép các chữ cái khi lấy đầu vào của người dùng trong Python #.
Sử dụng vòng lặp thời gian để lặp lại cho đến khi người dùng nhập các chữ cái ..
Sử dụng str. phương thức isalpha [] để kiểm tra xem người dùng chỉ nhập chữ cái chưa ..
Nếu điều kiện được đáp ứng, thoát ra khỏi vòng lặp ..
output = "players year money ipod case mini"
9 word1 = st.split["[a-zA-Z]+"]
0word1 = st.split["[a-zA-Z]+"]
1word1 = st.split["[a-zA-Z]+"]
2 word1 = st.split["[a-zA-Z]+"]
3word1 = st.split["[a-zA-Z]+"]
4word1 = st.split["[a-zA-Z]+"]
2 word1 = st.split["[a-zA-Z]+"]
6Làm thế nào để tôi có được bảng chữ cái trong Python?
Cách dễ nhất để tải một danh sách tất cả các chữ cái của bảng chữ cái là sử dụng chuỗi.ascii_letters, String.ascii_lowercase và String.ascii_uppercase.
Xem thảo luận
word1 = st.split["[a-zA-Z]+"]
7 word1 = st.split["[a-zA-Z]+"]
8BuckyBarnes5
BuckyBarnes6
BuckyBarnes7
BuckyBarnes8
word1 = st.split["[a-zA-Z]+"]
7 word1 = st.split["[a-zA-Z]+"]
8# string with letters, numbers, and special characters s = "[email protected]" # keep only letters res = "" for ch in s: if ch.isalpha[]: res += ch print[res]1
BuckyBarnes6
BuckyBarnes7
# string with letters, numbers, and special characters s = "[email protected]" # keep only letters res = "" for ch in s: if ch.isalpha[]: res += ch print[res]4
Output:
initial string : 123[]#$ABGFDabcjw abceddfgh first string result: ABGFDabcjw second string result: abceddfgh
& nbsp; Phương pháp #2: Sử dụng
# string with letters, numbers, and special characters s = "[email protected]" # keep only letters res = "" for ch in s: if ch.isalpha[]: res += ch print[res]5
Method #2: Using
# string with letters, numbers, and special characters s = "[email protected]" # keep only letters res = "" for ch in s: if ch.isalpha[]: res += ch print[res]5
output = "players year money ipod case mini"
9 word1 = st.split["[a-zA-Z]+"]
0word1 = st.split["[a-zA-Z]+"]
1word1 = st.split["[a-zA-Z]+"]
2 word1 = st.split["[a-zA-Z]+"]
3word1 = st.split["[a-zA-Z]+"]
4word1 = st.split["[a-zA-Z]+"]
2 word1 = st.split["[a-zA-Z]+"]
6word1 = st.split["[a-zA-Z]+"]
7 word1 = st.split["[a-zA-Z]+"]
8BuckyBarnes6
BuckyBarnes7
BuckyBarnes8
BuckyBarnes9
import re # string with letters, numbers, and special characters s = "[email protected]" # keep only letters res = re.sub[r'[^a-zA-Z]', '', s] print[res]1
word1 = st.split["[a-zA-Z]+"]
2 import re # string with letters, numbers, and special characters s = "[email protected]" # keep only letters res = re.sub[r'[^a-zA-Z]', '', s] print[res]3
# string with letters, numbers, and special characters s = "[email protected]" # keep only letters res = "".join[[ch for ch in s if ch.isalpha[]]] print[res]3
# string with letters, numbers, and special characters s = "[email protected]" # keep only letters res = "".join[[ch for ch in s if ch.isalpha[]]] print[res]4
import re # string with letters, numbers, and special characters s = "[email protected]" # keep only letters res = re.sub[r'[^a-zA-Z]', '', s] print[res]6
import re # string with letters, numbers, and special characters s = "[email protected]" # keep only letters res = re.sub[r'[^a-zA-Z]', '', s] print[res]7
word1 = st.split["[a-zA-Z]+"]
2 import re # string with letters, numbers, and special characters s = "[email protected]" # keep only letters res = re.sub[r'[^a-zA-Z]', '', s] print[res]3
# string with letters, numbers, and special characters s = "[email protected]" # keep only letters res = "".join[[ch for ch in s if ch.isalpha[]]] print[res]3
# string with letters, numbers, and special characters s = "[email protected]" # keep only letters res = "".join[[ch for ch in s if ch.isalpha[]]] print[res]4
BuckyBarnes2
word1 = st.split["[a-zA-Z]+"]
7 word1 = st.split["[a-zA-Z]+"]
8BuckyBarnes5
BuckyBarnes6
BuckyBarnes7
BuckyBarnes8
word1 = st.split["[a-zA-Z]+"]
7 word1 = st.split["[a-zA-Z]+"]
8# string with letters, numbers, and special characters s = "[email protected]" # keep only letters res = "" for ch in s: if ch.isalpha[]: res += ch print[res]1
BuckyBarnes6
BuckyBarnes7
# string with letters, numbers, and special characters s = "[email protected]" # keep only letters res = "" for ch in s: if ch.isalpha[]: res += ch print[res]4
Output:
output = "players year money ipod case mini"
0& nbsp; Phương pháp #3: Sử dụng
output = "players year money ipod case mini"
2Method #3: Using
output = "players year money ipod case mini"
2word1 = st.split["[a-zA-Z]+"]
1word1 = st.split["[a-zA-Z]+"]
2 word1 = st.split["[a-zA-Z]+"]
3word1 = st.split["[a-zA-Z]+"]
4word1 = st.split["[a-zA-Z]+"]
2 word1 = st.split["[a-zA-Z]+"]
6word1 = st.split["[a-zA-Z]+"]
7 word1 = st.split["[a-zA-Z]+"]
8BuckyBarnes6
BuckyBarnes7
BuckyBarnes8
BuckyBarnes9
import re # string with letters, numbers, and special characters s = "[email protected]" # keep only letters res = re.sub[r'[^a-zA-Z]', '', s] print[res]1
word1 = st.split["[a-zA-Z]+"]
2 import re # string with letters, numbers, and special characters s = "[email protected]" # keep only letters res = re.sub[r'[^a-zA-Z]', '', s] print[res]3
# string with letters, numbers, and special characters s = "[email protected]" # keep only letters res = "".join[[ch for ch in s if ch.isalpha[]]] print[res]3
# string with letters, numbers, and special characters s = "[email protected]" # keep only letters res = "".join[[ch for ch in s if ch.isalpha[]]] print[res]4
import re # string with letters, numbers, and special characters s = "[email protected]" # keep only letters res = re.sub[r'[^a-zA-Z]', '', s] print[res]6
import re # string with letters, numbers, and special characters s = "[email protected]" # keep only letters res = re.sub[r'[^a-zA-Z]', '', s] print[res]7
word1 = st.split["[a-zA-Z]+"]
2 import re # string with letters, numbers, and special characters s = "[email protected]" # keep only letters res = re.sub[r'[^a-zA-Z]', '', s] print[res]3
# string with letters, numbers, and special characters s = "[email protected]" # keep only letters res = "".join[[ch for ch in s if ch.isalpha[]]] print[res]3
# string with letters, numbers, and special characters s = "[email protected]" # keep only letters res = "".join[[ch for ch in s if ch.isalpha[]]] print[res]4
BuckyBarnes2
word1 = st.split["[a-zA-Z]+"]
7 word1 = st.split["[a-zA-Z]+"]
8BuckyBarnes5
BuckyBarnes6
BuckyBarnes7
BuckyBarnes8
word1 = st.split["[a-zA-Z]+"]
7 word1 = st.split["[a-zA-Z]+"]
8# string with letters, numbers, and special characters s = "[email protected]" # keep only letters res = "" for ch in s: if ch.isalpha[]: res += ch print[res]1
BuckyBarnes6
BuckyBarnes7
# string with letters, numbers, and special characters s = "[email protected]" # keep only letters res = "" for ch in s: if ch.isalpha[]: res += ch print[res]4
Output:
output = "players year money ipod case mini"
1