Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一个看起来像这样的元组:
('Elizabeth', 'Peter, Angela, Thomas')
我如何将其中的最后一个值分开,使其看起来像这样:
('Elizabeth', 'Peter', 'Angela', 'Thomas')
>>> names = ('Elizabeth', 'Peter, Angela, Thomas') >>> [y for x in names for y in x.split(', ')] ['Elizabeth', 'Peter', 'Angela', 'Thomas']
还有这种方式,不过我更喜欢第一种:
>>> ', '.join(names).split(', ') ['Elizabeth', 'Peter', 'Angela', 'Thomas']
当然,您最终可以将结果转换为元组,但很可能没有必要这样做。