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?
CodePudding user response:
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.
CodePudding user response:
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)
CodePudding user response:
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
CodePudding user response:
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?
- 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 initializeidx
. - You were not appending the number when
idx % 6 == 0
.