簡體   English   中英

如何將列表中的值存儲到多個變量(在python django中)

[英]how to store values in list to more than one variable ( in python django )

我有一些列表值,例如..

test_list = ["grandfathers", "name", "is","John"] 

我想將“ is”之前的所有索引值都放到一個變量中……並將“ is”之后的所有索引值存儲到另一個變量中

舉個例子

value1 = "grandfathers name"
value2 = "John"

“”的索引位置有時會發生變化,並且test_list中的索引總數也不總是相同,因此,我需要一個適用於任何情況的解決方案。

使用列表index()獲得'is'索引並進行切片:

before = ' '.join(test_list[:test_list.index('is')])
after = ' '.join(test_list[test_list.index('is')+1:])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM