你可以使用這樣的事情:
>>> from functools import reduce
>>> from operator import add
>>> tuplelista = [('word1', 'description1'), ('word2', 'description2')]
>>> flat_tuplelista = reduce(add, tuplelista)
>>> flat_tuplelista
...['word1', 'description1', 'word2', 'description2']
另一種方式
>>> tuplelista = [('word1', 'description1'), ('word2', 'description2')]
>>> flat_tuplelista = sum(tuplelista,())
>>> flat_tuplelista
...['word1', 'description1', 'word2', 'description2']
你可以簡單的檢查:
>>> if word in tuplelista:
... print "word already exists"
... else:
... desc = raw_input("Give descrption to the word: ")
... tuplelista.append((word,desc))
順便說一句,在我看來,這將是更好將數據存儲在字典中,其中單詞是關鍵字,描述是一個值。然後,您將可以簡單地檢查該單詞是否在字典中:
>>> words = {'word1': 'desription1', 'word2': 'description2'}
>>> if word in words:
... print "word already exists"
... else:
... desc = raw_input("Give descrption to the word: ")
... words[word] = desc
是的,但「單詞」是代碼的意思,如果它已經在那裏。 –
@johndoe你正在一個包含'tuples'的列表中尋找'string' – alfasin