我需要从代表服务器任务的文件中读取行。这些线由几对组成,其中一些是重复的。我需要解析它们,消除重复项并将它们分成 2 组以便更好地执行。这是 aline 的样子:
'task1'->3124, 'task2'->7743, 'task1'->3124, 'task3'->3456, 'task4'->23984
我想获得:
task1 task2
task3 task4
我对 Python 很陌生(只有 C 经验),并且希望在我的代码中使用更多 Python 函数得到一些帮助。这是我当前的代码(仍然不完整,但已经有错误):
def findgroups(s):
group=[]
n=[]
for i in range(len(s)):
group.append(s[i])
if s[i]==',':
n.append(group)
group=[]
return n
def finduniques(groups):
unique=[]
for i in range(len(groups)):
if groups.count(groups[i])==1:
unique.append(groups[i])
return unique
def makegroups(groups):
group1=[]
group2=[]
for i in range(len(groups)):
if i%2==0:
group1.append(groups[i])
if i%2==1:
group2.append(groups[i])
def printgroups(group1, group2):
for i in range(len(group1)):
for j in range(len(group2)):
if i==j:
print group1[0],group2[0]
line = "'task1'->3124, 'task2'->7743, 'task1'->3124, 'task3'->3456, 'task4'->23984"
groups = find_groups(line)
uniques = find_uniques(groups)
pairs = makegroups(uniques)
print printgroups(pairs)