python循环添加列表,循环以在Python中将多个列表追加到单个列表

In the following code, row[8], row[9], ..., row[27] each contain many numbers. I need all of the numbers in row[8], row[9], etc. to append to stat_by_symbol as separate lists within within stat_by_symbol. However, the code below appends all of the numbers across all rows within a single list.

As an example of what I need, if I called stat_by_symbol['aaa'][0], then I should get the list of numbers pulled from row[8].

How can I fix this? Many thanks.

EDIT for further clarification. I've attached a snapshot of the csv. I need stat_by_symbol['aaa'][0] to give me all of the numbers under Column i. Similarly, stat_by_symbol with index 1 would give me all of the numbers under Column j.

98d5734838757c00ca7c86476769abaf.png

stat_by_symbol = {}

with open('zzdata.csv', 'rb') as f:

reader = csv.reader(f)

reader.next()

for row in reader:

for symbol in symbols:#symbols in a list

if symbol in row:

for i in range(8, 28):

stat_by_symbol.setdefault(symbol, []).append(row[i])

解决方案stat_by_symbol = dict((symbol, [[] for i in xrange(8,28)]) for symbol in symbols)

with open('zzdata.csv', 'rb') as f:

reader = csv.reader(f)

reader.next()

for row in reader:

for symbol, symbol_list in stat_by_symbol.iteritems():

if symbol in row:

for symbol_list2, cell in zip(symbol_list, row[8:28]):

symbol_list2.append(cell)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值