循环进入连接每个元组元素的元组列表

亲爱的,

我一直在尝试使此代码更好/更快,更可持续,但尚未成功进行改进。

基本上,我需要循环访问元组列表,并将元组的每个元素与数据框过滤器表达式完全连接,如解决方案中所示。但是,我列表中的元组的长度不一样,它们可以更改,并且有时具有不同的长度,有时我可以得到的元组中每个元素少于4个,有时大于6个,甚至大于7/8,依此类推。

我的解决方案可行,但是每次元组的长度增加时,我都需要进行编辑,我知道这不是最有效的解决方案。为了避免嵌套的if,我将如何使用每个元组的长度在元组内部进行迭代?

感谢您的建议和意见

实际清单:

`Comb = [(),
 ('RD00',),
 ('RDC2',),
 ('RDC3',),
 ('RDC4',),
 ('RD00', 'RDC2'),
 ('RD00', 'RDC3'),
 ('RD00', 'RDC4'),
 ('RDC2', 'RDC3'),
 ('RDC2', 'RDC4'),
 ('RDC3', 'RDC4'),
 ('RD00', 'RDC2', 'RDC3'),
 ('RD00', 'RDC2', 'RDC4'),
 ('RD00', 'RDC3', 'RDC4'),
 ('RDC2', 'RDC3', 'RDC4'),
 ('RD00', 'RDC2', 'RDC3', 'RDC4')]`
我想出了这个解决方案,我需要连接每个元组中的元组元素,并在元组中每个元素的前面添加表达式:
`for p in comb:
    if len(p) == 1:
        print(''"(fF2_Principal['Messagetype']=="'{}'.format("'"+p[0]+"')"))
    if len(p) == 2:
        print(''"(fF2_Principal['Messagetype']=="'{} & '"   (fF2_Principal['Messagetype']=="'{}'.format("'"+p[0]+"')", "'"+p[1]+"')"))
    elif len(p) == 3:
        print(''"(fF2_Principal['Messagetype']=="'{} & '"(fF2_Principal['Messagetype']=="'{} & '"(fF2_Principal['Messagetype']=="'{}'.format("'"+p[0]+"')", "'"+p[1]+"')","'"+p[2]+"')"))
    elif len(p) == 4:
        print(''"(fF2_Principal['Messagetype']=="'{} & '"(fF2_Principal['Messagetype']=="'{} & '" (fF2_Principal['Messagetype']=="'{} & '"(fF2_Principal['Messagetype']=="'{}'.format("'"+p[0]+"')", "'"+p[1]+"')","'"+p[2]+"')","'"+p[3]+"')"))
    elif len(p) == 5: # not done yet, my previous data had 5 elements in one tuple
        print('{} : {} : {} : {} : {}'.format(p[0], p[1],p[2],p[3],p[4]))`
上面的代码生成以下结果,这些结果可以满足我的目的,但是我无法监视数据以查看何时会出现不同长度的元组。   结果:
    (fF2_Principal['Messagetype']=='RD00')
    (fF2_Principal['Messagetype']=='RDC2')
    (fF2_Principal['Messagetype']=='RDC3')
    (fF2_Principal['Messagetype']=='RDC4')
    (fF2_Principal['Messagetype']=='RD00') & (fF2_Principal['Messagetype']=='RDC2')
    (fF2_Principal['Messagetype']=='RD00') & (fF2_Principal['Messagetype']=='RDC3')
    (fF2_Principal['Messagetype']=='RD00') & (fF2_Principal['Messagetype']=='RDC4')
    (fF2_Principal['Messagetype']=='RDC2') & (fF2_Principal['Messagetype']=='RDC3')
    (fF2_Principal['Messagetype']=='RDC2') & (fF2_Principal['Messagetype']=='RDC4')
    (fF2_Principal['Messagetype']=='RDC3') & (fF2_Principal['Messagetype']=='RDC4')
    (fF2_Principal['Messagetype']=='RD00') & (fF2_Principal['Messagetype']=='RDC2') & 
    (fF2_Principal['Messagetype']=='RDC3')
    (fF2_Principal['Messagetype']=='RD00') & (fF2_Principal['Messagetype']=='RDC2') & 
    (fF2_Principal['Messagetype']=='RDC4')
    (fF2_Principal['Messagetype']=='RD00') & (fF2_Principal['Messagetype']=='RDC3') & 
    (fF2_Principal['Messagetype']=='RDC4')
    (fF2_Principal['Messagetype']=='RDC2') & (fF2_Principal['Messagetype']=='RDC3') & 
    (fF2_Principal['Messagetype']=='RDC4')
    (fF2_Principal['Messagetype']=='RD00') & (fF2_Principal['Messagetype']=='RDC2') & 
    (fF2_Principal['Messagetype']=='RDC3') & (fF2_Principal['Messagetype']=='RDC4')
非常感谢您的意见,并就如何改进此代码并使之更具可持续性提供建议。
评论
  • 红颜暗
    红颜暗 回复

    The easiest way to do this is using a list comprehension (to write each entry in the tuple's text individually) along with str.join() to patch an arbitrary number of them together.

    >>> for tup in Comb:
    ...     msg = " & ".join(
    ...         f"(fF2_Principal['Messagetype']=='{e}')" for e in tup
    ...     )
    ...     print(msg)
    ...
    
    (fF2_Principal['Messagetype']=='RD00')
    (fF2_Principal['Messagetype']=='RDC2')
    (fF2_Principal['Messagetype']=='RDC3')
    (fF2_Principal['Messagetype']=='RDC4')
    (fF2_Principal['Messagetype']=='RD00') & (fF2_Principal['Messagetype']=='RDC2')
    (fF2_Principal['Messagetype']=='RD00') & (fF2_Principal['Messagetype']=='RDC3')
    (fF2_Principal['Messagetype']=='RD00') & (fF2_Principal['Messagetype']=='RDC4')
    (fF2_Principal['Messagetype']=='RDC2') & (fF2_Principal['Messagetype']=='RDC3')
    (fF2_Principal['Messagetype']=='RDC2') & (fF2_Principal['Messagetype']=='RDC4')
    (fF2_Principal['Messagetype']=='RDC3') & (fF2_Principal['Messagetype']=='RDC4')
    (fF2_Principal['Messagetype']=='RD00') & (fF2_Principal['Messagetype']=='RDC2') & (fF2_Principal['Messagetype']=='RDC3')
    (fF2_Principal['Messagetype']=='RD00') & (fF2_Principal['Messagetype']=='RDC2') & (fF2_Principal['Messagetype']=='RDC4')
    (fF2_Principal['Messagetype']=='RD00') & (fF2_Principal['Messagetype']=='RDC3') & (fF2_Principal['Messagetype']=='RDC4')
    (fF2_Principal['Messagetype']=='RDC2') & (fF2_Principal['Messagetype']=='RDC3') & (fF2_Principal['Messagetype']=='RDC4')
    (fF2_Principal['Messagetype']=='RD00') & (fF2_Principal['Messagetype']=='RDC2') & (fF2_Principal['Messagetype']=='RDC3') & (fF2_Principal['Messagetype']=='RDC4')
    

    You'll note that this produces an empty line for the first element. If you don't want to print anything, even an empty line, then you can add if len(e) > 0 to the end of the comprehension, right after for e in tup, which will filter tuples out if they contain 0 elements.