【发布时间】:2014-07-20 21:34:25
【问题描述】:
所以目前我正在尝试使用数据嵌套列表并将这些列表中的一些元素转换为整数。所以现在嵌套列表会打印:
def Top5_Bottom5_test():
with open("Population.txt", "r") as in_file:
nested = [line.strip().split(',') for line in in_file][1:] #This creates the nested list
nested[0:][1:] = [int(x) for x in nested[0][1:]] #This is what I'm trying to use to make the numbers in the lists into integers.
print nested
打印出来:
[['Alabama', '126', '79', '17'], ['Alaska', '21', '100', '10'], ['Arizona', '190', '59', '16'], ['Arkansas', '172', '49', '28']....]
但我试图让它输出以便我可以使用冒泡排序的是:
[['Alabama', 126, 79, 17], ['Alaska', 21, 100, 10], ['Arizona', 190, 59, 16], ['Arkansas', 172, 49, 28]....]
有了这个,我的最终目标是让列表按第 [1] 个元素按降序排序,但当它们是字符串形式时,我似乎无法做到这一点。尽量避免使用 sort() 和 sorted() 函数。
【问题讨论】:
标签: python python-2.7 int nested-lists