2017-03-08 127 views
-4

可以說,例如,我們有一個列表的列表,我想找出是在位置x:如何在元組元組中找到特定的值?

((a,b,c),(d,e,f),(g,h,i),(i,x,l)) 

我會如何找出什麼是有的,有保存到一個變量?

+2

請澄清你的問題。目前尚不清楚你的問題究竟是什麼。 –

+0

我編輯它,但它可能仍不清楚。我有一個元組元組,我想找出位置x的值。 – simons21

+0

「Value_of position_'x'」?這是你需要澄清的。你的「模擬輸入」中的「x」是你正在尋找位置的值嗎? – miradulo

回答

1
mystuff = (('a','b','c'),('d','e','f'),('g','h','i'),('i','x','l')) 
lookfor = 'f' # example 
for i, a in enumerate(mystuff): 
    if lookfor in a: 
     print('found {} in tuple {} at index {}'.format(lookfor, i, a.index(lookfor))) 

輸出:

found f in tuple 1 at index 2 
+0

我將如何處理該代碼以保存在該位置的任何內容。可以說,元組(a,d,g,i)的每個第一個位置都有一個單詞,我想在這些位置打印出單詞,我該怎麼做? – simons21

+0

@ simons21,因此每個元組的第一個索引是一個單詞,並且您想要打印出每個元組的第一個索引? – davedwards

+0

有點愚蠢的我用一個詞作爲例子,所以生病告訴你我真的需要它。我有一個名爲list1的列表中的sql查詢記錄列表。我知道每個列表中只有4個項目,第一個是日期。我希望能夠從每個元組中選擇每個日期並首先打印出來。 – simons21

0

這是你將如何找到列表

列表索引項的例子。
list_of_lists=[['a','b','c'],['d','e','f'],['g','h','i'],['i','x','l']] 

#create variable consisting of sublist 2 
find_sublist_two = (list_of_lists)[1] 

print (find_sublist_two) 

#create variable consisting of 2nd item of sublist 2 
find_2nd_item_in_sublist_two = (list_of_lists)[1][1] 

print (find_2nd_item_in_sublist_two) 
+0

如果我不知道某個元素在哪個元組中的位置,這個工作是否會起作用? – simons21

相關問題