开发者

python: inserting a whole line into a list

import csv

with open('thefile.csv', 'rb') as f:
  data = list(csv.reader(f))
  import collections
  counter = collections.defaultdict(int)

  for row in data:
        counter[row[10]] += 1


with open('/pythonwork/thefile_subset11.csv', 'w') as outfile:
    writer = csv.writer(outfile)
    sample_cutoff=500
    b[]
    for row in data:
        if counter[row[10]] >= sample_cutoff:
           writer.writerow(row)

in addition to writing the data to a file, i would like to insert it into a list b[]

can i 开发者_高级运维just do b.insert[row] ?


It's b.append(row), but otherwise yes. And instead of b[] you want b = []. Another way to do it would be to make the list first, and then just write each element of the list to the file:

b = [row for row in data if counter[row[10]] >= sample_cutoff]
map(writer.writerow, b)


Yes, b = [] would support an insert method. The syntax is as follows.

b.insert(postiion_int, element)


It's list.insert(index, item) or list.append(item).

Also, b[] is a name error and syntax error. I suppose you mean b = [].

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜