Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
232 views
in Technique[技术] by (71.8m points)

python - How to get a list of tuples from several text files?

I want to access .txt files in 46 subdirectories and extract the number of 0s and 1s in the text of each file. So far I've written this code:

from pathlib import Path

def count_0s(paths):
  for p in paths:
    list_zeros = []
    list_ones = []
    for line in p.read_text().splitlines():
      zeros = 0
      zeros += line.count('0')
      ones = 0
      ones += line.count('1')
    list_zeros.append(zeros)
    list_ones.append(ones)    
  return list_zeros, list_ones

path = "/content/drive/MyDrive/data/classes/"
paths = Path(path).glob("*/marked*.txt")
n_zeros=count_0s(paths)
n_zeros

I want to get the function return in the form of 2 lists (one with the number of 0s and the other with the number of 1s) to use in a Pandas dataframe. Sorry if the questions are duplicated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There are couple mistakes in your function:

  • you've added some unnecessary square brackets (splitlines() already returns a list)
  • you don't iterate over characters, but over lines

Here is a corrected function:

def count_0s(paths):
  zeros_list = []
  ones_list = []
  for p in paths:
    zeros = 0
    ones = 0

    for line in p.read_text().splitlines():
        for c in line:
            if c == '0':
                zeros += 1
            else:
                ones += 1
    zeros_list.append(zeros)
    ones_list.append(ones)
  return zeros_list, ones_list

Be aware that this is probably a very inefficient way of counting 0 and 1. For example just using line.count('0') instead of a for loop can increase the speed by a factor of 10.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...