Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 按索引拆分列表或列表列表列表_Python_Python 3.x_List - Fatal编程技术网

Python 按索引拆分列表或列表列表列表

Python 按索引拆分列表或列表列表列表,python,python-3.x,list,Python,Python 3.x,List,我想在列表的索引I处拆分列表: input_list = `['book flight from Moscow to Paris for less than 200 euro no more than 3 stops', 'Moscow', 'Paris', '200 euro', '3']` 我所需的输出: output_list = input_list = `[['book flight from Moscow to Paris for less than 200 euro no mor

我想在列表的索引
I
处拆分列表:

input_list = `['book flight from Moscow to Paris for less than 200 euro no more than 3 stops', 'Moscow', 'Paris', '200 euro', '3']`
我所需的输出:

output_list = input_list = `[['book flight from Moscow to Paris for less than 200 euro no more than 3 stops'],[ 'Moscow', 'Paris', '200 euro', '3']]`
我试过:

[list(g) for k, g in groupby(input_list, lambda s: s.partition(',')[0])]
但我得到:

[['book flight from Moscow to Paris for less than 200 euro no more than 3 stops'], ['Moscow'], ['Paris'], ['200 euro'], ['3']]

我如何才能只分成两个列表?

您可以使用索引+切片来实现这一点

单一列表

res = [input_list[0], input_list[1:]]
结果:

['book flight from Moscow to Paris for less than 200 euro no more than 3 stops',
 ['Moscow', 'Paris', '200 euro', '3']]
[['book flight from Moscow to Paris for less than 200 euro no more than 3 stops',
  ['Moscow', 'Paris', '200 euro', '3', '', 'from_location', 'to_location', 'price', 'stops', '']],
 ['get me a flight to Joburg tomorrow',
  ['Joburg', 'tomorrow', '', 'to_location', 'departure', '']]]
另见

列表列表

list_of_list =  [[u'book flight from Moscow to Paris for less than 200 euro no more than 3 stops',
                  u'Moscow', u'Paris', u'200 euro', u'3', u'', u'from_location', u'to_location', u'price',
                  u'stops', u''], [u'get me a flight to Joburg tomorrow', u'Joburg', u'tomorrow', u'',
                  u'to_location', u'departure', u'']]

res2 = [[i[0], i[1:]] for i in list_of_list]
结果:

['book flight from Moscow to Paris for less than 200 euro no more than 3 stops',
 ['Moscow', 'Paris', '200 euro', '3']]
[['book flight from Moscow to Paris for less than 200 euro no more than 3 stops',
  ['Moscow', 'Paris', '200 euro', '3', '', 'from_location', 'to_location', 'price', 'stops', '']],
 ['get me a flight to Joburg tomorrow',
  ['Joburg', 'tomorrow', '', 'to_location', 'departure', '']]]