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

```````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.