分割和展平下面的元组元组的最佳方法是什么?
我有这个元组元组:
(('aaaa_BBB_wacker* cccc',), ('aaaa_BBB_tttt*',), ('aaaa_BBB2_wacker,aaaa_BBB',), ('BBB_ffff',), ('aaaa_BBB2MM*\r\naaaa_BBB_cccc2MM*',), ('BBBMM\\r\\nBBB2MM BBB',), ('aaaa_BBB_cccc2MM_tttt',), ('aaaa_BBB_tttt, aaaa_BBB',))
我需要: 由\ n \,\ r \,\ n \,\ r \,“,”,“”
分割并压扁它。所以最终结果应如下所示:
['aaaa_BBB_wacker*','cccc', 'aaaa_BBB_tttt*','aaaa_BBB2_wacker','aaaa_BBB','BBB_ffff','aaaa_BBB2MM*','naaaa_BBB_cccc2MM*','BBBMM','BBB2MM BBB','aaaa_BBB_cccc2MM_tttt','aaaa_BBB_tttt', 'aaaa_BBB']
我尝试了以下内容,它最终完成了这项工作,但我必须为每种模式重复多次。
patterns = [[i.split('\\r') for i in patterns]]
patterns = [item for sublist in patterns for item in sublist]
patterns = [item for sublist in patterns for item in sublist]
patterns = [[i.split('\\n') for i in patterns]]
答案 0 :(得分:0)
鉴于
//Sum of dependencies in a graph
import java.util.*;
class node
{
int vertex,weight;
public node(int v,int w)
{
vertex = v;
weight = w;
}
}
public class dependency
{
int V;
LinkedList<Integer> adj[];
public dependency(int v)
{
V = v;
adj = new LinkedList<Integer>[v];
for (int i=0;i < v ;i++ ) {
adj[i] = new LinkedList<node>();
}
}
void addEdge(int v,node l)
{
adj[v].add(l);
}
void BFS(int s)
{
boolean visited[] = new boolean[V];
LinkedList<Integer> queue = new LinkedList<Integer>();
visited[s] = true;
queue.add(s);
while(queue.size() != 0)
{
s = queue.poll();
System.out.println(s+" ");
Iterator<Integer> i = adj[s].listIterator();
while(i.hasNext())
{
int n = i.next();
if(!visited[n])
{
visited[n] = true;
queue.add(n);
}
}
}
}
public static void main(String[] args) {
graph g = new graph(4);
node a = new node(1,10);
node b = new node(2,20);
node c = new node(2,30);
node d = new node(0,20);
node e = new node(3,30);
node f = new node(3,20);
g.addEdge(0, a);
g.addEdge(0, b);
g.addEdge(1, c);
g.addEdge(2, d);
g.addEdge(2, e);
g.addEdge(3, f);
System.out.println("Following is Breadth First Traversal "+
"(starting from vertex 2)");
g.BFS(2);
}
}
待办事项
tups = (('aaaa_BBB_wacker* cccc',), ('aaaa_BBB_tttt*',),
('aaaa_BBB2_wacker,aaaa_BBB',), ('BBB_ffff',),
('aaaa_BBB2MM*\r\naaaa_BBB_cccc2MM*',), ('BBBMM\\r\\nBBB2MM BBB',),
('aaaa_BBB_cccc2MM_tttt',), ('aaaa_BBB_tttt, aaaa_BBB',))
注记。在您的分隔符上调用import re
delimiters = ('\r', '\n', ',', ' ', '\\r', '\\n')
pat = '(?:{})+'.format('|'.join(map(re.escape, delimiters)))
result = [s for tup in tups for item in tup for s in re.split(pat, item)]
可确保它们正确转义为正则表达式。 re.escape
使他们成为替代品。 |
使您的分隔符组无法捕获,因此?:
不会返回它。 re.split
表示与之前的群组匹配一次或多次。
答案 1 :(得分:0)
您应该使用正则表达式来分割字符串:
import re
re.split(r'[\n\r, ]+', s)
使用循环会更容易:
patterns = []
for item in l:
patterns += re.split(r'[\n\r, ]+', s)
答案 2 :(得分:0)
这是一个单行..但它并不简单。您可以在替换部分中添加任意数量的项目,只需继续添加它们即可。
start = (('aaaa_BBB_wacker* cccc',), ('aaaa_BBB_tttt*',), ('aaaa_BBB2_wacker,aaaa_BBB',), ('BBB_ffff',), ('aaaa_BBB2MM*\r\naaaa_BBB_cccc2MM*',), ('BBBMM\\r\\nBBB2MM BBB',), ('aaaa_BBB_cccc2MM_tttt',), ('aaaa_BBB_tttt, aaaa_BBB',))
output = [final_item for sublist in start for item in sublist for final_item in item.replace('\\r',' ').replace('\\n',' ').split()]