在Python中选择性数据保存到文件

huangapple go评论68阅读模式
英文:

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) &lt; 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(&quot;Fibonacci.txt&quot;, &#39;a&#39;) as f: 
        f.writelines(&#39;\n&#39;) 
        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) &lt; 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(&quot;Fibonacci.txt&quot;, &#39;a&#39;) as f: 
           f.writelines(&#39;\n&#39;) 
           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) &lt; 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(&quot;Fibonacci.txt&quot;, &#39;a&#39;) as f: 
            f.writelines(&#39;\n&#39;) 
            f.write(str(result))
    
print(distinct_sequences)

huangapple
  • 本文由 发表于 2023年6月6日 17:01:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76412991.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定