英文:
Selective data saving to a file in Python
问题
I am defining a Fibonacci sequence and saving the data to a file. However, I would like to save only the distinct sequence, not the repeating ones. For example, [0,1]
should be saved only once and not three times. I present the current and expected outputs.
def fibonacci(n):
sequence = [0, 1]
while len(sequence) < n:
next_number = sequence[-1] + sequence[-2]
sequence.append(next_number)
return sequence
n = 10
for i in range(0,n):
result = fibonacci(i)
print(result)
with open("Fibonacci.txt", 'a') as f:
f.writelines('\n')
f.write(str(result))
print(result)
The current saved data is
[0, 1]
[0, 1]
[0, 1]
[0, 1, 1]
[0, 1, 1, 2]
[0, 1, 1, 2, 3]
[0, 1, 1, 2, 3, 5]
[0, 1, 1, 2, 3, 5, 8]
[0, 1, 1, 2, 3, 5, 8, 13]
[0, 1, 1, 2, 3, 5, 8, 13, 21]
The data I expect to save
[0, 1]
[0, 1, 1]
[0, 1, 1, 2]
[0, 1, 1, 2, 3]
[0, 1, 1, 2, 3, 5]
[0, 1, 1, 2, 3, 5, 8]
[0, 1, 1, 2, 3, 5, 8, 13]
[0, 1, 1, 2, 3, 5, 8, 13, 21]
英文:
I am defining a Fibonacci sequence and saving the data to a file. However, I would like to save only the distinct sequence, not the repeating ones. For example, [0,1]
should be saved only once and not three times. I present the current and expected outputs.
def fibonacci(n):
sequence = [0, 1]
while len(sequence) < n:
next_number = sequence[-1] + sequence[-2]
sequence.append(next_number)
return sequence
n = 10
for i in range(0,n):
result = fibonacci(i)
print(result)
with open("Fibonacci.txt", 'a') as f:
f.writelines('\n')
f.write(str(result))
print(result)
The current saved data is
[0, 1]
[0, 1]
[0, 1]
[0, 1, 1]
[0, 1, 1, 2]
[0, 1, 1, 2, 3]
[0, 1, 1, 2, 3, 5]
[0, 1, 1, 2, 3, 5, 8]
[0, 1, 1, 2, 3, 5, 8, 13]
[0, 1, 1, 2, 3, 5, 8, 13, 21]
The data I expect to save
[0, 1]
[0, 1, 1]
[0, 1, 1, 2]
[0, 1, 1, 2, 3]
[0, 1, 1, 2, 3, 5]
[0, 1, 1, 2, 3, 5, 8]
[0, 1, 1, 2, 3, 5, 8, 13]
[0, 1, 1, 2, 3, 5, 8, 13, 21]
答案1
得分: 1
只需添加一个新变量 last_result
来存储上一次迭代的值,并使用 If
来检查它:
def fibonacci(n):
sequence = [0, 1]
while len(sequence) < n:
next_number = sequence[-1] + sequence[-2]
sequence.append(next_number)
return sequence
last_result = None
n = 10
for i in range(0, n):
result = fibonacci(i)
print(result)
if result != last_result:
last_result = result
with open("Fibonacci.txt", 'a') as f:
f.writelines('\n')
f.write(str(result))
print(result)
英文:
Just add a new variable last_result
that stores the previous iteration value and use an If
to check it:
def fibonacci(n):
sequence = [0, 1]
while len(sequence) < n:
next_number = sequence[-1] + sequence[-2]
sequence.append(next_number)
return sequence
last_result = None
n = 10
for i in range(0,n):
result = fibonacci(i)
print(result)
if result != last_result:
last_result = result
with open("Fibonacci.txt", 'a') as f:
f.writelines('\n')
f.write(str(result))
print(result)
答案2
得分: 0
你的起始数组 sequence
已经包含斐波那契数列的前两个值。因此,你需要从2开始循环,跳过0和1。
for i in range(2, n):
# ...
英文:
Your starting array sequence
already has the first 2 values of the fibonacci sequence. Hence you need to start your loop at 2, skipping 0 and 1.
for i in range(2, n):
# ...
答案3
得分: 0
要仅保存不同的斐波那契数列,您可以修改您的代码,以跟踪先前生成的序列,并在保存到文件之前检查当前序列是否已存在。
def fibonacci(n):
sequence = [0, 1]
while len(sequence) < n:
next_number = sequence[-1] + sequence[-2]
sequence.append(next_number)
return sequence
n = 10
distinct_sequences = [] # 用于跟踪不同的序列
for i in range(n):
result = fibonacci(i)
if result not in distinct_sequences:
distinct_sequences.append(result)
with open("Fibonacci.txt", 'a') as f:
f.writelines('\n')
f.write(str(result))
print(distinct_sequences)
请注意,我已经保留了您的代码中的英文注释和代码部分,只翻译了注释中的文字。
英文:
To save only the distinct Fibonacci sequences, you can modify your code to keep track of the previously generated sequences and check if the current sequence is already present before saving it to the file.
def fibonacci(n):
sequence = [0, 1]
while len(sequence) < n:
next_number = sequence[-1] + sequence[-2]
sequence.append(next_number)
return sequence
n = 10
distinct_sequences = [] # Keep track of distinct sequences
for i in range(n):
result = fibonacci(i)
if result not in distinct_sequences:
distinct_sequences.append(result)
with open("Fibonacci.txt", 'a') as f:
f.writelines('\n')
f.write(str(result))
print(distinct_sequences)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论