如何在元组元组中找到特定值?

时间:2017-03-08 19:07:46

标签: python python-3.x

比方说,我们有一个列表列表,我想找出x位置的内容:

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

我如何找出存在的内容并将其保存到变量中?

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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)