我想在列表的最后一项BinList
前面加一个字符串less than or equal to
BinList
有10个项目长
我已经用一个字符串less than
在列表中的所有其他项前面加上了我编写的函数:
def prepend(list, string):
string += '{0}'
list = [string.format(i) for i in list]
return (list)
但我不能把它用在单子上
我试过了
lastbin = BinList[10]
string = 'less than or equal to '
FinalCell = string.format(lastbin)
但这只是返回less than or equal to
也不是BinList[10]
的数目
list[:-1] = [1, 2]
(排除最后一项)list[:-1] =
,见list slicing vs assignment不需要循环或任何东西,只需要像第二个例子一样使用切片,但在同一行中简单地赋值。此外,BinList[-1]将访问列表中的最后一个元素:
您甚至可以使prepend将列表中的最后一个元素与其他元素分开:
这将返回:
相关问题 更多 >
编程相关推荐