2012-03-09 44 views

回答

5

快到了,這將工作:

''.join(t[0] for t in tuples) 

BTW,不要使用tuple作爲變量,因爲它也是一個Python類型。

0

如何:

d = [("hello", 12), ("yo", 30)] 
' '.join([ t[ 0 ] for t in d ]) 

#output 
'hello yo' 

,或者如果你不想空間:

d = [("hello", 12), ("yo", 30)] 
''.join([ t[ 0 ] for t in d ]) 

#output 
'helloyo' 
0

你有什麼工作差不多,除了xxx.joinxxx作爲分隔符連接參數,並且因爲join是一個函數,所以它需要括號。

所以,如果你想'helloyo',只是做:

''.join([tuple[0] for tuple in tuples]) 

事實上,對於join,你甚至不需要列表理解:

''.join(tuple[0] for tuple in tuples) 
0

你是接近,但join是一個函數,所以你需要(),而不是[]

0

join是一個方法,如果字符串。你正在使用[],你想要''.join()

相關問題