Hướng dẫn how do you separate upper and lower cases in python? - làm thế nào để bạn phân tách chữ hoa và chữ thường trong python?

-1

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ần tách các từ có ít nhất một chữ cái viết hoa bằng chữ thường. Tôi cần lấy đầu vào và tách các từ chữ hoa và các từ viết thường và in cả hai. Đây là mã của tôi:

text = input["Input your text: "]
words0 = text.strip[].split[]
words1 = []
words2 = []
wordslen= len[words0]
for word in words0:
    counter = 0
    for x in word:
        while counter != wordslen:
            if x.isupper[]:
                words1.append[word]
            else:
                words2.append[word]
            counter += 1
wordsupper = list[set[words1]]
wordslower = list[set[words2]]
allwords = wordsupper + wordslower

for word in allwords:
    print[word]

HALFER

19.7K17 Huy hiệu vàng92 Huy hiệu bạc177 Huy hiệu đồng17 gold badges92 silver badges177 bronze badges

Hỏi ngày 8 tháng 4 năm 2014 lúc 22:08Apr 8, 2014 at 22:08

7

words = input["Input your text: "].strip[].split[]

lower, mixed = set[], set[]
for word in words:
    if word == word.lower[]:
        lower.add[word]
    else:
        mixed.add[word]

print["Lowercase words: " + ", ".join[lower]]
print["Mixed- and uppercase words: " + ", ".join[mixed]]

mà chạy như:

Input your text: This is a Perl and Python party.
Lowercase words: a, and, is, party.
Mixed- and uppercase words: This, Python, Perl

Đã trả lời ngày 8 tháng 4 năm 2014 lúc 22:15Apr 8, 2014 at 22:15

Hugh Bothwellhugh BothwellHugh Bothwell

53,9K7 Huy hiệu vàng81 Huy hiệu bạc98 Huy hiệu Đồng7 gold badges81 silver badges98 bronze badges

0

Để tách các từ chỉ có ký tự chữ thường với phần còn lại:

text = raw_input['Input text: ']

lower, rest = set[], set[]
for word in text.split[]:
    [lower if word == word.lower[] else rest].add[word]
print[lower]
print[rest]

Đã trả lời ngày 8 tháng 4 năm 2014 lúc 23:09Apr 8, 2014 at 23:09

JFSJFSjfs

384K183 Huy hiệu vàng949 Huy hiệu bạc1619 Huy hiệu Đồng183 gold badges949 silver badges1619 bronze badges

Điều này tương tự như các bài viết trước, nhưng sử dụng map[] thay vì for word in words:

text = raw_input["Input your text: "]
words = text.strip[].split[]

upper = []
lower = []
def sort_word[word]:
    if word.lower[] == word:
        lower.append[word]
    else:
        upper.append[word]

map[sort_word, words]

Đã trả lời ngày 8 tháng 4 năm 2014 lúc 22:23Apr 8, 2014 at 22:23

Rickcnagyrickcnagyrickcnagy

1.74418 Huy hiệu bạc23 Huy hiệu đồng18 silver badges23 bronze badges

1

Có lẽ một cái gì đó như thế này:

text = input["Input your text: "]
words = text.strip[].split[]
wordslower = []
wordsupper = []

for word in words:
    # if the word is the same as word.lower[] that means all the
    # characters are lower case.  Also, don't add duplicates to
    # the list.
    if word == word.lower[]:
        if word not in wordslower:
            wordslower.append[word]
    else: # The word has at least one capital letter
        if word not in wordsupper:
            wordsupper.append[word]

print[wordslower]
print[wordsupper]

Đã trả lời ngày 8 tháng 4 năm 2014 lúc 22:16Apr 8, 2014 at 22:16

JGRITTYJGRITTYjgritty

11.4K3 Huy hiệu vàng37 Huy hiệu bạc59 Huy hiệu đồng3 gold badges37 silver badges59 bronze badges

2

Thử cái này:

def check[word]:
    for k in word:
        if k != k.lower[]: #If the letter is capitalized
            return True
    return False

text = input['Text: ']
text = text.strip[].split[]

wordsupper = []
wordslower = []

for k in text:
    if check[k] == True:
        wordsupper.append[k]
    else:
        wordslower.append[k]

print[wordslower]
print[wordsupper]

Đã trả lời ngày 8 tháng 4 năm 2014 lúc 23:21Apr 8, 2014 at 23:21

A.J. Uppala.J. UppalA.J. Uppal

Phim thương hiệu vàng 18,7K66 gold badges41 silver badges74 bronze badges

Làm thế nào để bạn phân tách chữ thường và chữ hoa trong Python?

Sử dụng Re.Phương thức findall [] Để phân chia một chuỗi trên các chữ cái chữ hoa, ví dụ:lại.

Làm thế nào để bạn trích xuất chữ hoa trong Python?

Danh sách hiểu và chức năng isupper có thể được sử dụng để thực hiện nhiệm vụ cụ thể này.Độ hiểu hiểu danh sách chủ yếu được sử dụng để lặp lại trong danh sách và chức năng isupper kiểm tra các ký tự chữ hoa. can be used to perform this particular task. The list comprehension is primarily used to iterate over the list and isupper function checks for the uppercase characters.

Sự khác biệt giữa trên [] và isupper [] trong Python là gì?

Isupper [] & isLower [] trả về các giá trị boolean trong khi hàm trên [] & thấp hơn [] trả về các chuỗi trong chữ hoa hoặc chữ thường..

Bài Viết Liên Quan

Chủ Đề