将列表传递给函数,直接和间接

时间:2019-08-26 13:19:10

标签: python list nested

我具有以下功能:

def search_stmts(stmts,lower_bound_sura,lower_bound_aya,higher_bound_sura = -1,higher_bound_aya = -1):
     ayas_list = []
     cnt = 0
     for cnt in range(len(stmts[0])):
         if stmts[0][cnt] == "root":
             ayas_list.append(self.root_lookup(stmts[1][cnt], basicrecord_instance.get_ayas_with_addressing( \
                                lower_bound_sura,lower_bound_aya,higher_bound_sura, higher_bound_aya)))
         elif stmts[0][cnt] == "word":
             #print(stmts[1][cnt])
             ayas_list.append(self.word_lookup(stmts[1][cnt], basicrecord_instance.get_ayas_with_addressing( \
                                   lower_bound_sura,lower_bound_aya,higher_bound_sura, higher_bound_aya)))

     return self.search_ayas(ayas_list)

它获得stmts作为嵌套列表:[['word','root','word'],['hello','how','are']]和4个int参数。
当我以以下方式调用函数时:

search_stmts([['word','word','root'],['hello','how','are']],1,1)

上面的调用工作正常。

但是,如果我有以下调用,它将不会获得任何数据:

list1 = ['root','word','root']
list2 = ['hello','how','are']
search_stmts([list1,list2],1,1)   

如何将我的两个列表作为一个列表传递?

1 个答案:

答案 0 :(得分:1)

如果我删除了“ basicrecord_instance”和带有“ self”的功能,我将得到准确的结果

def search_stmts(stmts,lower_bound_sura,lower_bound_aya,higher_bound_sura = -1,higher_bound_aya = -1):
     ayas_list = []
     cnt = 0
     print(range(len(stmts[0])))
     for cnt in range(len(stmts[0])):
         print(cnt)
         if stmts[0][cnt] == "root":
             ayas_list.append((stmts[1][cnt], lower_bound_sura,lower_bound_aya,higher_bound_sura, higher_bound_aya))
         elif stmts[0][cnt] == "word":
             #print(stmts[1][cnt])
             ayas_list.append((stmts[1][cnt],                                    lower_bound_sura,lower_bound_aya,higher_bound_sura, higher_bound_aya))
     return ayas_list

print(search_stmts([['word','word','root'],['hello','how','are']],1,1))
list1 = ['root','word','root']
list2 = ['hello','how','are']


print(search_stmts([list1,list2],1,1))

我得到

[('hello', 1, 1, -1, -1), ('how', 1, 1, -1, -1), ('are', 1, 1, -1, -1)]
[('hello', 1, 1, -1, -1), ('how', 1, 1, -1, -1), ('are', 1, 1, -1, -1)]