python:将整行插入列表中

发布于 2024-09-11 12:04:08 字数 526 浏览 2 评论 0原文

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)

除了将数据写入文件之外,我还想将其插入到列表 b[] 中,

我可以执行 b.insert[row] 吗?

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] ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

过去的过去 2024-09-18 12:04:08

它是 b.append(row),但除此之外是的。而不是 b[],而是 b = []。另一种方法是先创建列表,然后将列表的每个元素写入文件:

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

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)
大姐,你呐 2024-09-18 12:04:08

是的,b = [] 将支持 insert 方法。语法如下。

b.insert(postiion_int, element)

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

b.insert(postiion_int, element)
时光磨忆 2024-09-18 12:04:08

它是 list.insert(index, item) 或 list.append(item)。

另外,b[] 是名称错误和语法错误。我想你的意思是b = []

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文