我想将元组列表转换为字符串列表。
代码:
a = {"man"}
if a is not None:
for bb in b:
for bbb in bb:
if bbb[1] not in a:
total = "".join(bbb)
第二个代码:
total = set()
if a is not None:
for bb in b:
for bbb in bb:
if bbb[1] not in a:
total.add("".join(bbb))
total_list = list(total)
当前输出:
['-1|kin', '-1|u', '1|jack', '1|finish', '1|hmm', '-1|already', '-1|kao', '-1|jack', '1|king', '1|kao']
预期输出:
如果bbb [1]位于a中,我不想采用列表的相同索引。例如,[(“ 0 |”,“ man”),(“ 1 |”,“ king”)]包含在a中,因此不会打印整个索引。
答案 0 :(得分:1)
您每次迭代都覆盖total
。大概您真正想要的是一个像这样更新的集合:
total = set()
if a is not None:
for bb in b:
for bbb in bb:
if bbb[1] not in a:
total.add("".join(bbb))
total_list = list(total)
答案 1 :(得分:1)
如果顺序不重要:
>>> {t0+t1 for sl in b for t0,t1 in sl if t1 not in a}
{'1|king', '0|leader'}
或者,如果您想维持秩序:
>>> seen=set()
>>> [t0+t1 for sl in b for t0,t1 in sl if t1 not in a and t0+t1 not in seen and not seen.add(t0+t1)]
['1|king', '0|leader']
通过评论,您可以执行以下操作:
>>> [x+y for sl in filter(lambda l: all(y not in a for x,y in l), (sl for sl in b)) for x,y in sl]
['0|leader', '1|king']
或者
>>> [x+y for sl in b for x,y in sl if all(t1 not in a for t0,t1 in sl)]
['0|leader', '1|king']
答案 2 :(得分:0)
这应该可以工作。
s="abc-dirk-alt.avi"
rx="-([^-]*)-"
if [[ $s =~ $rx ]]; then
echo ${BASH_REMATCH[1]};
fi