1

I'm working currently on a basic project, where I want to visualize later some data logged by some sensors.

I have a file with numbers:

75
0
0
30
35
32
38
45
53
44
51
62
43
34
56
42
28
32
43
56
43
46
16
33
48
...

And I want to create a list of lists, by every sub list will contain 6 element from the original ones, like [75,0,0,30,35,32],[38,45,53,44,51,62],...].

f = open("log2.txt", "r")
content = f.readlines()
 
# Variable for storing the sum
idx = 0

line = []
db =  [] 

# Iterating through the content
for number in content:
    print(idx%6)
    if idx % 6 == 0:
        #newline
        db.append(line)
        line = []
        print("AAAAA")
    else:
        #continue line
        line.append(int(number))
    idx += idx+1

print(db)

But the result of db is only [[]]. Why? How can I do it well?

Gábor
  • 327
  • 1
  • 7
  • debugging aside, it may be easiest to create a flat list with all the numbers and then chunk it with one of the methods from [here](https://stackoverflow.com/questions/312443/how-do-you-split-a-list-into-evenly-sized-chunks). – timgeb May 12 '22 at 10:00
  • 2
    `idx += idx+1` doesn't do what you think it does (or is it a typo?), it probably never gets divisible by 6 (except of course for the 1st value which is 0). You meant `idx += 1`. In order to avoid that completely use *enumerate*. You already started debugging by printing *idx*, print some other variables as well, to see what's going on. – CristiFati May 12 '22 at 10:03

4 Answers4

2

Taking inspiration from another answer here, we can provide a solution with lazy evaluation.

def chunks(lst, n):
    """Yield successive n-sized chunks from lst."""
    for i in range(0, len(lst), n):
        yield lst[i:i + n]


f = open("log2.txt", "r")
content = list(int(line) for line in f)

list_of_lists = list(chunks(content, 6))

Here we don't keep intermediate data in memory and this approach can be used to process very large files.

fshabashev
  • 619
  • 6
  • 20
1

Method 1(List comprehension):

You can create a list comprehension looping through the lines of f:

f = open("log2.txt", "r")
content = f.readlines()
l = list(map(int, content)) # Converting each line to integer.
l = [l[i:i+6] for i in range(0,len(l), 6)] # Creating lists of 6 elements
print(l)

For eg, if a file contains:

1
2
3
4
5
6
7
8
9
10
11
12

Then the output is:

[[1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11, 12]]

Method 2(Improvements in your code):

f = open("log2.txt", "r")
content = f.readlines()
line = []
db =  [] 

# Iterating through the content
for idx, number in enumerate(content):
    if idx % 6 == 0:
        #newline
        line = [int(number)] # You need to add that number too!
        db.append(line)
    else:
        #continue line
        line.append(int(number))
print(db)

Output:

[[1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11, 12]]

What were you doing wrong?

  1. Using a variable to keep track of the index. Instead, you can use enumerate() to get both the value and the index. Then you won't need to update or initialize idx.
  2. You were not appending the number when idx % 6 == 0.
Abhyuday Vaish
  • 2,357
  • 5
  • 11
  • 27
1
f = open("log2.txt", "r", encoding='utf-8')
content = f.read()

content = content.split()
int_content = []
for i in content:
    int_content.append(int(i))

print(int_content)

final = []
# Iterating through the content
rng = 0
temp = []
for number in int_content:
    if rng != 6:
        temp.append(number)
        print(temp)
        range += 1
        continue
    else:
        rng = 0
        final.append(temp)
        temp = []

print(final)
Rahil Kadakia
  • 135
  • 1
  • 6
1

There is much more simple way to do that:

f = open("log2.txt", "r")
content = f.readlines()

# now your content looks like this: ['75\n', '0\n', '0\n',...]
content = [int(x.strip()) for x in content]

# now your content looks like this [75, 0, 0, 30, 35, ...]

result = []
while content:
   result.append(content[:6])
   content = content[6:]

# now your result looks like this: [[75, 0, 0, 30, 35, 32], [38, 45, 53, 44, 51, 62],...]
# and your content variable is empty list



ex4
  • 2,289
  • 1
  • 14
  • 21
  • I think your solution would have a quadratic complexity O(n^2). this operation: `content = content[6:]` required array reallocation and copying. – fshabashev May 12 '22 at 10:19
  • That is possible. Question was about "basic project" so I suppose performance isn't the biggest issue here. But that definetely could be optimized if there is lot of data. – ex4 May 12 '22 at 10:25