flask - python列表问题
本文介绍了flask - python列表问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问 题
已知有序列表
posts=['post/1','post/2','post/3']
现在已经知道post(posts列表中任意值,如post/1),怎么这个post,来获取post[-1]和post[1]的值
如:已经get post/2,怎么获取列表中前一项post/1和后一项post/3
解决方案
posts = ['post/1', 'post/2', 'post/3']
know_string = 'post/2'
know_string_index = posts.index(know_string)
#the forward string
forward_string = None if know_string_index == 0 else posts[know_string_index - 1]
print(forward_string)
#the back string
back_string = None if know_string_index == length(posts) else posts[know_string_index + 1]
print(back_string)
这篇关于flask - python列表问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文