在嵌套列表中将字符串转换为标题大小写

2024-03-28 19:34:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个嵌套列表,我正在尝试将所有标题大写(每个嵌套列表中的第三个元素,即cardinal richelieu等)。你知道吗

我的脑袋似乎绕不开正确的方法。我以为replace可以工作,但我得到了一个错误:TypeError:replace()参数2必须是str,而不是内置函数或方法

下面是嵌套列表,后面是我的替换尝试。我感谢任何帮助我理解我做错了什么!你知道吗

    [['j234hg', '19 October 1969', 'court scene with cardinal richelieu'], ['d45j5jkd', '28 December 1969', 'THE ROYAL PHILHARMONIC ORCHESTRA GOES TO THE BATHROOM'], ['s4k5jk', '8 December 1970', 'crossing the atlantic on a tricycle'], ['zd7u4h', '19 October 1969', 'Bicycle Repair Man'], ['f983', '22 December 1970', 'Royal Episode 13 (or: The Queen Will Be Watching)'], ['j8s74', '15 September 1970', 'THE SEMAPHORE VERSION OF WUTHERING HEIGHTS'], ['n4j6l3j', '7 December 1972', 'Mr. Pither']]    

    records_list = [[x.replace(i[2], i[2].title) for x in i] for i in records_list]

Tags: the方法in元素标题列表forreplace