01/10/2018, 13:58

Hỏi về nguyên tắc trả về kết quả sorted trong python!

Chào các bạn mình có một câu hỏi về nguyên tắc sort trong python. Mình đang vọc bài tập 25 trong Learn Python the hard way thì để ý thấy khi kết quả trả ra khi sort từ trong 1 câu, Python sẽ đưa về từ viết hoa đứng trước tiên chứ không phải thứ tự alphbet. Mình có thử chỉnh lại bỏ viết hoa từ đầu tiên trong câu thì ra đúng thứ tự alphabet. Cho mình hỏi đó là mặc định của Python phải không, trong trường hợp mình cần phải đưa đúng thứ tự alphabet, mặc kệ là có viết hoa hay không thì phải làm sao, mong các cao nhân chỉ giúp.

Cám ơn các bạn.

Công

Đây là thao tác của mình trên Python:

>>> words
['I', 'love', 'the', 'way', 'you', 'love', 'me.']
>>> sorted_words = sort_word(words)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'sort_word' is not defined
>>> sorted_words = sort_words(words)
>>> sorted_words
['I', 'love', 'love', 'me.', 'the', 'way', 'you']
>>> sentence = "Seven wonders of the world i may know two only."
>>>
>>> words = break_words(sentence)
>>>
>>> words
['Seven', 'wonders', 'of', 'the', 'world', 'i', 'may', 'know', 'two', 'only.']
>>> sorted_words = sort_words(words)
>>>
>>> sorted_words
['Seven', 'i', 'know', 'may', 'of', 'only.', 'the', 'two', 'wonders', 'world']
>>>
>>> sentence = "seven wonders of the world i may know two."
>>>
>>> words = break_words(sentence)
>>>
>>> words
['seven', 'wonders', 'of', 'the', 'world', 'i', 'may', 'know', 'two.']
>>> sorted_words = sort_words(words)
>>>
>>> sorted_wrods
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'sorted_wrods' is not defined
>>> sorted_words
['i', 'know', 'may', 'of', 'seven', 'the', 'two.', 'wonders', 'world']
>>>
./.
Bên dưới là đoạn source code trong bài tập 25:

def break_words(stuff):
	"""This function will break up words for us."""
	words = stuff.split(' ')
	return words

def sort_words(words):
	"""Sorts the words."""
	return sorted(words)

def print_first_word(words):
	"""Prints the first word after popping it off."""
	word = words.pop(0)
	print word

def print_last_word(words):
	"""Prints the last word after popping it off."""
	word = words.pop(-1)
	print word

def sort_sentence(sentence):
	"""Takes in a full sentence and returns the sorted words."""
	words = break_words(sentence)
	return sort_words(words)

def print_first_and_last(sentence):
	"""Prints the first and last words of the sentence."""
	words = break_words(sentence)
	print_first_word(words)
	print_last_word(words)

def print_first_and_last_sorted(sentence):
	"""Sorts the words then prints the first and last one."""
	words = sort_sentence(sentence)
	print_first_word(words)
	print_last_word(words)
rogp10 viết 16:09 ngày 01/10/2018

Phương thức casefold

Phùng Cúc viết 15:58 ngày 01/10/2018

có 2 cách để sort mà ko để ý tới lwer hay upper case là:

  1. Chuyển hết về lower case hoặc upper case
def break_words(stuff):
	"""This function will break up words for us."""
	stuff = stuff.lower()
	words = stuff.split(' ')
	return words

def sort_words(words):
	"""Sorts the words."""
	return sorted(words)

2. Giữ nguyên


def break_words(stuff):
	"""This function will break up words for us."""
	words = stuff.split(' ')
	return words

def sort_words(words):
	"""Sorts the words."""
	return sorted(words, key=lambda s: s.lower())
Pham Thanh Cong viết 15:59 ngày 01/10/2018

Cám ơn bạn nhé!
Công

Bài liên quan
0