Jul-03-2019, 01:24 PM
(Jul-03-2019, 09:22 AM)perfringo Wrote: "give a man a fish and you feed him for a day; teach a man to fish and you feed him for a lifetime."
So grab a paper and a pen and try to be a computer. What would you do if you need to achieve desired result manually:
- in order to keep longest lines you have to check all lines, right? So - go through file line by line
- in order to keep longest lines you have to know length of every line, right? So - determine length of every line
Now just pick the longest ones (there is ambiguity in task description and therefore I don't know what exactly and in which order should be picked).
Implement it in Python and off you go.
Thank you for your reply,
So I wrote this script for the moment:
L1=[] L2=[] L3=[] L4=[] with open ("C:/Users/lveillat/Desktop/Données stage/Fichiers tests/test_retrait_chaines_intermediaire.txt","r") as f1: for lignes in f1: lignes=lignes.rstrip('\n') L1.append (lignes) L2.append (lignes) for i in L1: if i not in L4: L4.append(i) L3.append(i) else: continue for j in L2: if j.startswith(i) and i != j: L4.append(j) L3.append(j) print(L3) L3.clear()With this file:
1 2 3 4 5
1 2 3 4 5 6
4 5 2 1
4 5 2 1 8 9
4 5 2 1 8
7 8 9
7 8 9 10
7 8 9 10 11
7 8 9 45 16
7 8 9 45
And he gives me this result:
['1 2 3 4 5', '1 2 3 4 5 6']
['4 5 2 1', '4 5 2 1 8 9', '4 5 2 1 8']
['7 8 9', '7 8 9 10', '7 8 9 10 11', '7 8 9 45 16', '7 8 9 45']
It's ok for the first two lines, however for the last one, the channels 7 8 9 45 16 and 7 8 9 45 are too much (they should be in a separate list).
How could I solve this problem?