Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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_List_Tuples - Fatal编程技术网

Python 为前缀范围问题创建序列

Python 为前缀范围问题创建序列,python,list,tuples,Python,List,Tuples,我试图创建一个包含序列的列表,以便调用PrefixSpan算法。我需要以下表格中的列表: [ [["a", "b"], ["c"]], [["b", "c"], ["d"]], [["c", "d"], ["e"]]] [[u'a', u'b', u'c', u'd', u'e', u'f', ], [u'a', u'b',...]] 其中,嵌套列表的前两个字母是规则/顺序,单个字母是结果 我有此表格的数据: [ [["a", "b"], ["c"]], [["

我试图创建一个包含序列的列表,以便调用PrefixSpan算法。我需要以下表格中的列表:

 [ [["a", "b"], ["c"]], [["b", "c"], ["d"]], [["c", "d"], ["e"]]]
[[u'a',
  u'b',
  u'c',
  u'd',
  u'e',
  u'f',
],
 [u'a',
  u'b',...]]
其中,嵌套列表的前两个字母是规则/顺序,单个字母是结果

我有此表格的数据:

 [ [["a", "b"], ["c"]], [["b", "c"], ["d"]], [["c", "d"], ["e"]]]
[[u'a',
  u'b',
  u'c',
  u'd',
  u'e',
  u'f',
],
 [u'a',
  u'b',...]]
将以下逻辑应用于此数据:

   a1 =[]
    for i in range(len(list2)):
      a2 = list2[i]
      for j in range(len(a2)-2):
        a1.append([a2[j],a2[j+1]])
        a1.append([a2[j+2]])
其结果如下表所示:

 [[[u'a', u'b'],
          [u'c'],
          [u'd', u'e'],
          [u'f'],
          [u'g', u'h'],...]]

所以我不能创建这个嵌套的二对一元组序列。

我会用列表理解来完成。我创建了以下代码:

a = [[u'a',u'b',u'c',u'd',u'e',u'f'],[u'a2',u'b2',u'c2',u'd2',u'e2',u'f2']]
print([[[v,x[i+1],[x[i+2]]] for i,v in enumerate(x) if i<len(x)-2] for x in a])
我希望这是一个解决你们问题的办法。有关更多信息,请查阅“嵌套列表理解”