假设一个元组列表,其内容为任意长度:
quotes = [('Shakespeare', 'Piccard', 'Allen'),
("To be, or not to be", "Earl Grey, 60 degrees", "I feel that life is divided into the horrible and the miserable. That's the two categories. The horrible are like, I don't know, terminal cases, you know, and blind people, crippled. I don't know how they get through life. It's amazing to me. And the miserable is everyone else. So you should be thankful that you're miserable, because that's very lucky, to be miserable."),
('beer', 'tea', 'vodka')
]
出于调试目的,我想输出列表的内容:
print str(quotes)
但是,我只想要任何元组值的前 N 个字符,如果它像第三个引号一样长,我不需要整个内容。我知道我可以编写一个函数来遍历列表,然后遍历每个元组并切片前 N 个字符,但是作为 Python,我怀疑有一种更简单、更短、更“Pythonic”的方式。在那儿?
我不是在为当前示例寻找 XY 解决方案,它只是说明一点的示例。