Posts: 16
Threads: 7
Joined: May 2019
Jul-03-2019, 08:57 AM
(This post was last modified: Jul-03-2019, 08:57 AM by Amniote.)
Hello everyone,
I have the following file:
1 2 3 4
7 8 9 10 11 12
4 5 2 1 8
7 8 9
4 5 2 1 8 9
7 8 9 10 11
7 8 9 10
1 2 3 4 5
I need to create a script that will keep only the longest lines (between the ones that start the same)
for example here the algorithm must return to me:
1 2 3 4 5
4 5 2 1 8 9
7 8 9 10 11 12
I do not see at all how to do that .... Can you help me please ?
Thanks :)
Posts: 1,950
Threads: 8
Joined: Jun 2018
Jul-03-2019, 09:22 AM
(This post was last modified: Jul-03-2019, 09:22 AM by perfringo.)
"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.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Posts: 16
Threads: 7
Joined: May 2019
(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?
Posts: 479
Threads: 86
Joined: Feb 2018
Jul-03-2019, 01:47 PM
(This post was last modified: Jul-03-2019, 01:47 PM by SheeppOSU.)
Try this
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 i in j or j in i:
if i != j:
L4.append(j)
L3.append(j)
i = j
print(L3)
L3.clear()
Hope this works
Posts: 1,950
Threads: 8
Joined: Jun 2018
Jul-03-2019, 02:03 PM
(This post was last modified: Jul-03-2019, 02:03 PM by perfringo.)
I don't get it. You need to get longest sequences of consecutive numbers? You need longest sequences starting with particular number?
Can you describe in spoken language what is 'longest chain'?
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Posts: 16
Threads: 7
Joined: May 2019
(Jul-03-2019, 02:03 PM)perfringo Wrote: I don't get it. You need to get longest sequences of consecutive numbers? You need longest sequences starting with particular number?
Can you describe in spoken language what is 'longest chain'?
I have to get the longest chain among all the chains that follow (it's hard to explain with words, here are some examples):
['1 2 3', '1 2 3 4', '1 2 3 4 5'] -> I want "1 2 3 4 5"
['66 9 4 ', '66 9 4 1 6', '66 9 4 1 '] -> I want "66 9 4 1 6"
[66], '66 4 9 6 1', '66 4 9 33', '66 4 9 1 6', '66 4 9 1'] -> I want "66 4 9 6 1" and "66 4 9 1 6" (both are of identical sizes)
Posts: 3,458
Threads: 101
Joined: Sep 2016
Quote: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).
Why would they be in a separate list? They're both part of the 7 8 9 chain.
Posts: 16
Threads: 7
Joined: May 2019
Quote:Why would they be in a separate list? They're both part of the 7 8 9 chain.
not quite and that's the subtlety ...
I would need :
['7 8 9', '7 8 9 10', '7 8 9 10 11']
['7 8 9 45 16', '7 8 9 45']
The chains in a list must follow each other.
Posts: 1,950
Threads: 8
Joined: Jun 2018
Jul-03-2019, 03:32 PM
(This post was last modified: Jul-03-2019, 03:33 PM by perfringo.)
(Jul-03-2019, 03:12 PM)Amniote Wrote: (it's hard to explain with words, here are some examples):
From Zen on Python (this is about implementation but you can't have implementation without problem description so it can be applied as well):
Quote:If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Or put it another way: you cant have solutions without problem description
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Posts: 3,458
Threads: 101
Joined: Sep 2016
Why isn't '7 8 9' the first chain in '7 8 9 45 16'?
It's really hard to help, when your output doesn't make sense. Help us to understand how you're coming up with what the output should be, otherwise we're just guessing at how to help.
|