gpt4 book ai didi

python - 将新子列表添加到现有列表

转载 作者:太空宇宙 更新时间:2023-11-03 12:48:52 25 4
gpt4 key购买 nike

大家好!

我正在尝试向现有列表中添加一个新的子列表,但我不太确定该怎么做。这是我的代码:

data = [[4,5],[3,7]]
search = 9
for sublist in data:
if search in sublist:
sublist.append(0)
print("there", sublist)
break
else:
print("not there")
break
def sublist():
[5,6]
print[data]

但是,如果不存在搜索,则子列表不会添加到原始列表中。我该怎么做?

干杯!5813

最佳答案

只需附加它:

>>> data = [[4,5],[3,7]]
>>> data.append([5,6])
>>> data
[[4, 5], [3, 7], [5, 6]]

关于python - 将新子列表添加到现有列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19106502/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com