我有一个主列表,它基于2个子列表。我想用“search_value”参数创建一个函数,并打印“search_value”项的索引位置,包括子列表的列表索引。在
示例:grocery = ["Juice", "Tomato", "Potato", "Banana", "Milk", "Bread"]
clothes = ["Shirt", "Pant", "Jacket", "Sweater", "Hat", "Pajama", "T-Shiraz", "Polo"]
master_list = [grocery, clothes]
预期结果:
^{pr2}$
我不熟悉python&已经编写了工作代码。只想知道如何用更少的努力去做def function(in_coming_string_to_search):
grocery = ["Juice", "Tomato", "Potato", "Banana", "Milk", "Bread"]
clothes = ["Shirt", "Pant", "Jacket", "Sweater", "Hat", "Pajama", "T-Shiraz", "Polo"]
master_list = [grocery, clothes]
length = int(len(master_list))
print master_list, "List has:", length, "list items", '\n'
to_do_list_first_array_index = 0
counter = 0
list_one_length = int(len(master_list[0]))
while counter < list_one_length:
for a in master_list[to_do_list_first_array_index]:
# print a
if a == in_coming_string_to_search:
print "The Item You Searched For is", in_coming_string_to_search, ". It is in the first list with index position of:", counter
counter = counter + 1
to_do_list_second_array_index = 1
counter2 = 0
list_two_length = int(len(master_list[1]))
while counter2 < list_two_length:
for b in master_list[to_do_list_second_array_index]:
if b == in_coming_string_to_search:
print "The Item You Searched For is", in_coming_string_to_search, ". It is in the second list with index position of:", counter2
counter2 = counter2 + 1
if __name__ == '__main__':
string_to_search = "Tomato"
function(string_to_search)