Python 2.7 将元素添加回列表的列表
大家好,我想在temp中循环添加一个B后缀到B的元素中,并将它们存储回原来的位置,我该怎么做呢。我尝试了这个方法,它只将所有新元素作为个体存储在节点中Python 2.7 将元素添加回列表的列表,python-2.7,Python 2.7,大家好,我想在temp中循环添加一个B后缀到B的元素中,并将它们存储回原来的位置,我该怎么做呢。我尝试了这个方法,它只将所有新元素作为个体存储在节点中 temp = [[1,2,3,4],[5,6,7,8],[9,10,11,12]] nodes = [] for j in temp: for i in j: nodes.append('%s' % i + 'B') print nodes 期望输出: temp = [[1B,2B,3B,4B],[5B,6B,7B,8B],[
temp = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]
nodes = []
for j in temp:
for i in j:
nodes.append('%s' % i + 'B')
print nodes
期望输出:
temp = [[1B,2B,3B,4B],[5B,6B,7B,8B],[9B,10B,11B,12B]]
谢谢您可以按如下方式操作:
for i,selection in enumerate(temp):
for j,element in enumerate(selection):
temp[i][j] = str(element)+ "B"
试试这个:
temp = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]
nodes = [map(lambda x: str(x) + 'B', l) for l in temp]
print nodes
谢谢@sshashank,不过输出结果与我之前得到的完全一致。我需要一个列表格式的新列表。只是问一下,你如何期望你的变量
bus\u route\u nodes
不分配任何内容就存在?@applepi,这是一个输入错误,我已经更新了它。谢谢你现在能帮忙吗?