python - 如何在python中替换列表列表
问题描述
我有这个列表列表`
listoflist = [[['I', 'nvestment activity during the year is summarised as fol', 'l', 'ows:'],
['InterFace-Light', 'InterFace-Light', 'InterFace-Light', 'InterFace-Light'],
[[9.0], [9.0], [9.0], [9.0]]],
[(45.40929412841797, 167.94473266601562, 49.90929412841797, 178.0337371826172),
(47.360496520996094, 167.94473266601562, 241.59832763671875, 178.0337371826172),
(238.8065185546875, 167.94473266601562, 243.3065185546875, 178.0337371826172),
(240.51470947265625, 167.94473266601562, 259.0223083496094, 178.0337371826172)]]
[[['Cost'],
['InterFace-Bold'],
[[9.0]]],
[(526.6923828125, 189.15679931640625, 544.8453979492188, 199.52481079101562)]]
[[['Additions', '£’', '000'], ['InterFace-Bold', 'InterFace-Bold', 'InterFace-Bold'],
[[9.0], [9.0], [9.0]]],
[(56.747901916503906, 199.1571044921875, 95.19589233398438, 209.52511596679688),
(523.3358154296875, 199.1571044921875, 532.69580078125, 209.52511596679688),
(530.2658081054688, 199.1571044921875, 544.8457641601562, 209.52511596679688)]]]
我想替换listoflist[0][0][0]
为['Investment activity during the year is summarised as follows:'], ['Cost'], ['Additions', '£’000']
这是我当前的代码:
new_list = [['Investment activity during the year is summarised as follows:'],
['Cost'],
['Additions', '£’000']]
for i in listoflist:
i[0].pop(0)
for ii in new_list:
i[0].insert(0, ii)
解决方案
根据您要执行的操作(替换或插入),这里有两个选项:
插入:
for ls in listoflist:
ls.insert(0, "Investment activity during the year is summarised as follows:")
取代:
for ls in listoflist:
ls[0] = "Investment activity during the year is summarised as follows:"
编辑:
替换:
listoflist[0][0] = "value1"
listoflist[1][0] = "value2"
listoflist[2][0] = "value3"
插入:
listoflist[0].insert(0, "value1")
edit2:我想我终于明白你想做什么了:
for ls in listoflist:
if ls[0] == ["I", "nvestment activity during the year is summarised as fol", "l", "ows:"]:
ls[0] = new_list[0]
elif ls[0][0] == ["Cost"]:
ls[0] = new_list[1]
elif ls[0][0] == ["Additions", "£’", "000"]:
ls[0] = new_list[2]
推荐阅读
- windows - AppRotateFile nssm
- python - 将 for 循环转换为函数?
- python - 布尔值字典一次测试
- python-3.x - 即时简化列表过滤
- azure - Azure B2C Web 应用程序 - 如何在 Web 应用程序会话生命周期之外使用刷新令牌进行身份验证
- python - 在函数调用中链接 kwargs
- python-3.x - python3中两个非常大的文件之间相交的最有效方法是什么?
- regex - 为什么正则表达式不匹配文本?
- reactjs - 为什么我不能将状态重置为初始状态?[还原]
- python - TypeError 使用 timedelta,不能求和时间