Skip to main content
 首页 » 编程设计

python之如何在列表理解中执行作业

2024年11月24日11kevingrace

所以在Python 3.6中为tress创建测试用例时,我想知道是否有一个好的方法来执行列表推导中的赋值等操作。我应该使用 lambda 吗?

我要插入

Prev_Node.next = Node(i) 
Prev_Node = Node(i) 

进入

iList = [1,2,3,4,10] 
Prev_Node = Node(iList[0]) 
l1 = [Node(i) <Any assignment action?> for i in iList[1:])] 

提前感谢您的宝贵时间,

请您参考如下方法:

使用迭代器遍历你的列表,不要摆弄 .Next 等来构建你自己的。

如果你正在寻找列表的头部和尾部,那么你可以这样做:

int_list = [1, 2, 3, 4, 10] 
node_list = [Node(i) for i in int_list] 
head = node_list[0]  # Get the head Node and leave it in the list. 
head, *tail = node_list  # Get the head Node and remove it from the list. 

并像这样迭代

for node in node_list: 
    print(node) 

看看各种list function关于如何进一步操作现有列表。