![](/img/trans.png)
[英]How to append data at specific indexes from one list to another empty list in python?
[英]Append specific rows from one list to another
尝试获取包含7列和10行的2d列表并将将仅第4,5和6列(或从索引0开始的3,4,5)的所有行追加到新列表时遇到一些困难。 原始列表实际上是一个csv,并且要长得多,但是为了解决故障,我只是将其部分放在函数中。 到目前为止,我有...
def coords():
# just an example of first couple lines...
bigList = [['File','FZone','Type','ID','Lat','Lon','Ref','RVec']
['20120505','Cons','mit','3_10','-21.77','119.11','mon_grs','14.3']
newList=[]
for row in bigList[1:]: # skip the header
newList.append(row[3])
return newList # return newList to main so it can be sent to other functions
这段代码为我提供了一个仅带有“ ID”的新列表,但我也想要“ Lat”和“ Lon”。 新列表应该看起来像... ['3_10','-21.77','119.11'] ['4_10','-21.10'...]我尝试重新编写newList.append(row [3,4 ,5])...当然是行不通的,但不确定如何去做。
row[3]
引用第四个元素。 您似乎想要第四到第六个元素,因此将其切成薄片:
row[3:6]
您还可以通过列表理解来完成所有这些工作:
newList = [row[3:6] for row in myList[1:]]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.