2017-03-08 36 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

相关问题