如何在 python 函数中 return 包含索引的列表

How to return a list with indexes in python function

如果我在 python 中有一个列表,我可以 return 使用函数 return 列表和索引吗:

def list_my(G):
    nodes = list(G.nodes())
    return nodes

上面的代码 return 只有节点:

['Hi','hello','be','bye','in'...] 而我希望它是:

['Hi',0, 'hello',1, 'be',2, 'bye',3, 'in',4, ...]

有人可以帮我吗?

尝试使用 for 循环:

def list_my(G):
    result = list()
    for i, node in enumerate(G.nodes()):
        result.extend((node, i))
    return result