gpt4 book ai didi

python - 根据条件从另一个列表创建新列表

转载 作者:行者123 更新时间:2023-12-01 01:54:49 26 4
gpt4 key购买 nike

我正在尝试根据条件从另一个列表创建一个新列表:

lst = [("Id01","Code1",1),("Id01","#instr1",1),("Id01","#instr2",1),("Id01","#instr4",1),
("Id01","Code2",1),("Id01","#instr3",1),("Id01","#instr2",1),("Id02","Code2",1),
("Id02","#instr2",1),("Id02","#instr5",1)]

table, instrlist = '', ''; code, instructions = [], []; qty = 0

for idx, l in enumerate(lst):
table = l[0]
if not l[1].startswith('#'):
code = l[1]; qty = l[2]; instructions = []
else:
instructions.append(l[1])
print idx, table, code, instructions, qty
每次代码出现在包含“#”的元组之后的元组上时,我都需要将正确的行传输到程序的另一部分并重置以开始处理另一部分。我设置了一系列条件,得到了这个结果:

0 Id01 Code1 [] 1
1 Id01 Code1 ['#instr1'] 1
2 Id01 Code1 ['#instr1', '#instr2'] 1
3 Id01 Code1 ['#instr1', '#instr2', '#instr4'] 1
4 Id01 Code2 [] 1
5 Id01 Code2 ['#instr3'] 1
6 Id01 Code2 ['#instr3', '#instr2'] 1
7 Id02 Code2 [] 1
8 Id02 Code2 ['#instr2'] 1
9 Id02 Code2 ['#instr2', '#instr5'] 1

但是我真正需要的结果是

3 Id01 Code1 ['#instr1', '#instr2', '#instr4'] 1
6 Id01 Code2 ['#instr3', '#instr2'] 1
9 Id02 Code2 ['#instr2', '#instr5'] 1

什么条件我需要再次过滤?

我还不够熟练,无法使用列表理解或内置过滤器,并且我希望让代码尽可能具有可读性(对于新手来说),至少在我了解更多信息之前是这样。

更新:

jpp提供的解决方案似乎是最有效和可读的:

from collections import defaultdict
from itertools import count, chain

lst = [("Id01","Code1",1),("Id01","#instr1",1),("Id01","#instr2",1),("Id01","#instr4",1),
("Id01","Code2",1),("Id01","#instr3",1),("Id01","#instr2",1),("Id02","Code2",1),
("Id02","#instr2",1),("Id02","#instr5",1)]

d = defaultdict(list)
enums = []
c = count()

for ids, action, num in lst:
if not action.startswith('#'):
my_ids, my_action = ids, action
enums.append(next(c))
else:
d[(my_ids, my_action)].append([action, num])
next(c)
enums = enums[1:] + [len(lst)]

for idx, ((key1, key2), val) in enumerate(d.items()):
print (enums[idx]-1, key1, key2, list(chain.from_iterable(val)), val[0][-1])

但是我遇到了一些问题。

  1. 由于某些原因,顺序是错误的(最后一行变成了第一行):结果:

    (3, 'Id02', 'Code2', ['#instr2', 1, '#instr5', 1], 1) <--- 应该是最后一个

    (6, 'Id01', '代码1', ['#instr1', 1, '#instr2', 1, '#instr4', 1], 1)

    (9, 'Id01', '代码2', ['#instr3', 1, '#instr2', 1], 1)

  2. 元组上的数字字段并不总是“1”,有时脚本不会尊重它(我这边缺少信息),因为它始终采用元组中找到的数字。需要与“Code”元组配对,可以省略。

我正在解决这个问题,一旦解决问题我就会更新我的帖子。

最佳答案

collections.defaultdict 提供了一个直观的解决方案。这个想法是创建一个字典,如果第二个组件不以 '#' 开头,则将键设置为元组的前两个组件。然后迭代字典以您所需的格式打印

使用 itertools.count 需要进行一些困惑的工作才能获得所需的索引。我相信您可以在这方面做出改进。

from collections import defaultdict
from itertools import count, chain

lst = [("Id01","Code1",1),("Id01","#instr1",1),("Id01","#instr2",1),("Id01","#instr4",1),
("Id01","Code2",1),("Id01","#instr3",1),("Id01","#instr2",1),("Id02","Code2",1),
("Id02","#instr2",1),("Id02","#instr5",1)]

d = defaultdict(list)
enums = []
c = count()

for ids, action, num in lst:
if not action.startswith('#'):
my_ids, my_action = ids, action
enums.append(next(c))
else:
d[(my_ids, my_action)].append([action, num])
next(c)

enums = enums[1:] + [len(lst)]

结果:

for idx, ((key1, key2), val) in enumerate(d.items()):
print(enums[idx]-1, key1, key2, list(chain.from_iterable(val)), val[0][-1])

3 Id01 Code1 ['#instr1', 1, '#instr2', 1, '#instr4', 1] 1
6 Id01 Code2 ['#instr3', 1, '#instr2', 1] 1
9 Id02 Code2 ['#instr2', 1, '#instr5', 1] 1

关于python - 根据条件从另一个列表创建新列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50337070/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com