我遇到一个问题,我给出了一个包含字符串的列表列表。 我不仅要反转字符串,还要反向输出列表列表
示例:
input_list = [["CAT", "BAT", "SAT"], ["MAN","CHAN", "LAN"], ["CAL", "NJ","NYC"]]
预期输出:
[["CYN", "JN", "LAC"], ["NAL", "NAHC", "NAM"], ["TAS", "TAB", "TAC"]]
我正在尝试的代码:
doc_file = [["CAT", "BAT", "SAT"],
["MAN","CHAN", "LAN"],
["CAL", "NJ","NYC"]]
temp_list = []
temp_list2 = []
#print("reversing First Line:")
rev_main_list = list(reversed(doc_file))
#print(rev_main_list)
for i in range(len(rev_main_list)):
for j in range(len(rev_main_list[i])):
# print("value of i:", i)
# print("value of j:", j)
temp_list.append((rev_main_list[i][j][::-1]))
#print("outer loop i:", i)
temp_list2.append(temp_list[i])
rev_main_list =temp_list2
print(rev_main_list)
非常感谢您的帮助
答案 0 :(得分:1)
def reverse_it(a):
if isinstance(a,(list,tuple)):
return [reverse_it(x) for x in a[::-1]]
return a[::-1]
答案 1 :(得分:0)
for small_list in big_list:
for x in small_list:
x = x[::-1]
small_list.reverse()
big_list.reverse()
有点笨重的修复,但它应该适合你。