2012-12-25 67 views
84

我對Python中的數據結構有點困惑; ()[]{}。我正在嘗試整理一個簡單的列表,可能因爲我無法確定我無法對其進行排序的數據類型。按字母順序排列Python數據結構列表

我的目錄是簡單的:['Stem', 'constitute', 'Sedge', 'Eflux', 'Whim', 'Intrigue']

我的問題是,這是什麼類型的數據,以及如何將單詞按字母順序排序?

+0

如果要對列表進行排序,那麼您可以使用「list = ['Stem','構成','Sedge','Eflux',' Whim','Intrigue'] list.sort() print list「。 – kannanrbk

+0

'[]'包含內置數據類型'list',(請參閱http://www.tutorialspoint.com/python/python_lists.htm)。列表只是一組值(它們可以包含其他可迭代對象 - 即嵌套列表)。 '()'包含了內建元組。它們是不可變的(不能改變)。 (請參閱http://www.tutorialspoint.com/python/python_tuples.htm)。 並且'{}'包含內置的'詞典'。與字典(用於單詞)相似,其中「關鍵」將是該詞並且「值」是定義。 (請參閱http://www.tutorialspoint.com/python/python_dictionary.htm)。 –

回答

124

[]表示list()表示tuple{}表示dictionary。您應該看看official Python tutorial,因爲這些是Python編程的基礎。

你有什麼是一個字符串列表。您可以這樣排序是:

In [1]: lst = ['Stem', 'constitute', 'Sedge', 'Eflux', 'Whim', 'Intrigue'] 

In [2]: sorted(lst) 
Out[2]: ['Eflux', 'Intrigue', 'Sedge', 'Stem', 'Whim', 'constitute'] 

正如你所看到的,以大寫字母開頭的單詞得到優先於那些剛開始用小寫字母。如果你想將它們獨立進行排序,這樣做:

In [4]: sorted(lst, key=str.lower) 
Out[4]: ['constitute', 'Eflux', 'Intrigue', 'Sedge', 'Stem', 'Whim'] 

您還可以通過這樣做逆向排序列表:

In [12]: sorted(lst, reverse=True) 
Out[12]: ['constitute', 'Whim', 'Stem', 'Sedge', 'Intrigue', 'Eflux'] 

In [13]: sorted(lst, key=str.lower, reverse=True) 
Out[13]: ['Whim', 'Stem', 'Sedge', 'Intrigue', 'Eflux', 'constitute'] 

請注意:如果你使用Python 3工作,那麼str是包含人類可讀文本的每個字符串的正確數據類型。但是,如果您仍然需要使用Python 2,那麼您可能會使用Python 2中的數據類型爲unicode的unicode字符串,而不是str。在這種情況下,如果您有一個unicode字符串列表,則必須編寫key=unicode.lower而不是key=str.lower

+0

在MongoDB數據庫的pymongo'find_one()'結果中使用第二個示例時,出現錯誤:'descriptor'lower'需要'str'對象,但收到'unicode'。結果是一個字符串數組,並且像這樣實現:'results ['keywords'] = sorted(keywords ['keywords'],key = str.lower)'。有人知道如何解決這個問題嗎? – user1063287

+0

@ user1063287對不起,我遲到了。在你的情況下,你需要寫'key = unicode.lower'而不是'key = str.lower'。這是因爲你正在處理unicode字符串,而不是字節串。請參考官方的[Unicode HOWTO](https://docs.python.org/3/howto/unicode.html)瞭解更多信息,尤其是Python 2和3之間的差異。 – pemistahl

6

你正在處理一個python列表,並且對它進行排序就像做這件事一樣簡單。

my_list = ['Stem', 'constitute', 'Sedge', 'Eflux', 'Whim', 'Intrigue'] 
my_list.sort() 
7

您可以使用內置的sorted函數。

print sorted(['Stem', 'constitute', 'Sedge', 'Eflux', 'Whim', 'Intrigue']) 
3
>>> a =() 
>>> type(a) 
<type 'tuple'> 
>>> a = [] 
>>> type(a) 
<type 'list'> 
>>> a = {} 
>>> type(a) 
<type 'dict'> 
>>> a = ['Stem', 'constitute', 'Sedge', 'Eflux', 'Whim', 'Intrigue'] 
>>> a.sort() 
>>> a 
['Eflux', 'Intrigue', 'Sedge', 'Stem', 'Whim', 'constitute'] 
>>> 
19

Python有一個內置的函數調用sorted,它會給你一個排序列表的可迭代的,你給它(如列表([1,2,3]);一個字典({1:2,3:4},雖然它只會返回一個({1,2,3,4)的一組;所述密鑰的排序的列表。或一個元組((1,2,3,4)))

>>> x = [3,2,1] 
>>> sorted(x) 
[1, 2, 3] 
>>> x 
[3, 2, 1] 

解釋也有一個sort方法將在原地執行排序

兩者都取key參數,它應該是可調用的(函數/ lambda),您可以使用它來更改要排序的內容。
例如,從由值排序,你可以使用下面的代碼字典得到(key,value) -pairs列表:

>>> x = {3:2,2:1,1:5} 
>>> sorted(x.items(), key=lambda kv: kv[1]) # Items returns a list of `(key,value)`-pairs 
[(2, 1), (3, 2), (1, 5)] 
+0

@ jwpat7,是啊。更好? –

+0

感謝您真正解釋它是如何工作的。很有幫助。 – joshmcode

3

ListName.sort()會按字母順序排序。您可以在方括號中添加reverse=False/True來顛倒項目的順序:ListName.sort(reverse=False)

+0

這是一個對Ruby的評論? – allanberry