2013-04-11 143 views
0

所以,如果我有這樣的:匹配長度與相同長度的字符串列表

a = "hi"
b = ["hello there", "goodbye", "nice to meet you"]

所以如果LEN(一)爲2,則是有可能找到一個字符串b中的長度相同嗎?在這種情況下,「你好」

爲「嗨」我正在計數的字符,但「你好」我正在計算的話。

我試圖分割字符串: b = [["hello", "there"], "goodbye", "nice to meet you"] 但我只找到一個方法。如果你的意思是,數量的b元素的話的應該是相同的,以一個個

+5

如何'「你好」'具有相同的長度爲'「喜」'? – eumiro 2013-04-11 13:06:35

+0

對不起,「嗨」我正在計數的字符,但對於「你好」我正在計算單詞......這可能嗎? – user2240542 2013-04-11 13:09:38

回答

4

其拆分在a字符數的:

a = "hi" 
b = ["hello there", "goodbye", "nice to meet you"] 

next(w for w in b if len(w.split()) == len(a)) 
# returns 'hello there' 

[w for w in b if len(w.split()) == len(a)] 
# returns ['hello there'] 
+0

哦,好吧!謝謝你的幫助! – user2240542 2013-04-11 13:17:20

相關問題