2013-07-02 15 views
-1

我的代碼是一樣的東西下面如何添加和排序在python multidimentional數組?

​​

這裏x和y彼此相關。如果我想索引他們在一個數組或其他東西,並根據x排序索引我如何接近。考慮到索引長度僅爲4或5.意義(i)將循環最多4-5次。

快速代碼將是幫助。沒有多線程的線索。

+4

你解析HTML與正則表達式... – TerryA

+0

強制性的SO鏈接:http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 – Esenti

+0

考慮到正則表達式html不會成爲問題... –

回答

3

您可以先將值檢索到列表中。 re.findall()會自動做:

values = re.findall(r'someinteger(.+?)withanother(.+?)', html) 

然後你就可以對列表進行排序:

values.sort() 

,如果你想通過x排序(在你的例子)。

例如:

>>> s = "someinteger5withanother1someinteger4withanother2someinteger3withanother3" 
>>> values = re.findall(r'someinteger(.+?)withanother(.+?)', s) 
>>> values 
[('5', '1'), ('4', '2'), ('3', '3')] 
>>> values.sort() 
>>> values 
[('3', '3'), ('4', '2'), ('5', '1')] 

當然,你仍然處理字符串,如果你想通過數字來排序,你要麼需要做

values = [(int(x), int(y)) for x,y in values] 

將它們全部轉換爲整數,或做

values.sort(key=lambda x: int(x[0])) 
+0

我明白了分揀部分..但我如何檢索所有或任何單個對? –

+0

'values'包含所有的值,'values [n]'包含第n個對(從0開始計數)。 –