Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Unexpected behavior
#1
hi,

my function :

def analyze_deviations(file_path):
    deviations = []
    
    with open(file_path, 'r') as f:
        lines = [line.strip() for line in f if line.strip()]
    
    # On parcourt le fichier deux par deux (Aller / Retour)
    for i in range(0, len(lines) - 1, 2):
        line1 = lines[i]
        line2 = lines[i+1]
        
        # Extraction de l'othellier (situé après le score de 3 char + 1 espace)
        # Format : [Score(3)] [Espace] [Board(64)] [Espace] [Player] ...
        board1 = line1[4:68]
        board2 = line2[4:68]
        
        empty1 = board1.count('-')
        empty2 = board2.count('-')
        
        # CAS SPÉCIAL : Si on voit deux plateaux de départ (50 vides)
        if empty1 == 50 and empty2 == 50:
            # Si les othelliers sont strictement identiques, 
            # c'est un match retour sans variation -> Coup 1
            if board1 == board2:
                deviations.append(1)
                i += 2
            else:
                # Si les othelliers sont différents (ou si c'est deux "Aller" de suite)
                # On applique ta convention du Coup 0
                deviations.append(0)
                i += 1
        else:
            # CAS GÉNÉRAL
            deviations.append(50 - empty2 + 1)
            i += 2
            
    return deviations
my file : extract

-20 -------------O----*-O------*OO----*O**----OOO-----*------------- * D7C4B6F6B5F7D3B4A5D8F3A3E8F8E7C8G3G4F1A7B3E2G6G5H5H3A6A4H4H6B7H2D1A2H7H8G2C2G8A8G7B8C1H1B2G1D2A1E1B1
  8 -------------O----**O-----**OO---***OO---***OO----**-O---------- O C2E2E1C1C8E8B4A5B3A4A3D2E7F3G4G3H4G1B2G5H5H6G6H3F1D1D8B8A2F8G7G2H1A1B1H8H2G8H7PAA7B7A8A6
 24 --------------O----**O---***O------OO*------O--------O--------O- * F4E2F6G4G3F2H4H5G5H3H1D6G6E7G1D2C3E1C1H6D1F1H2B5C2A4H7B3A3A2C5B2C7D7A1B1C6G7H8C8A5A6F8E8D8B6A7B7B8A8
 26 --------------O----**O---***O------OO*------O--------O--------O- * F6G5G3C3C5D2F4E2C6D6F2E7B3B6D7D8G6H5D1E1C1H3C2G4F1B5E8F8H1G7C7A4A3A2C8B8A5A6B7A8A7B1H2G1H4B2A1PAH7H6H8
 -2 O--------O------OOO---*--O*OO*-----**-------*------------------- * F3C5D3E3E2D6C6F5B6G4D2C2B5F7F6C7A4A6D7A5F8D8E8E7C8G6C1F1D1E1F2B1H4G5A2G1H7G2H1H2H3H5H6G7A7A8G8B8B7H8
-28 O--------O------OOO---*--O*OO*-----**-------*------------------- * E3D6C6C5D3C7B5A4A6E7D7A5C8F5B6D2E2C2F3F1F7F2C1B1F6H3G2D1G5G4H4H1H2H5A2B7H6H7G6A7B8F8G8G7E8D8H8A8G1E1
The file is perfectly formatted (no hidden characters).

Reading line 4 results in --------O------OOO-----OOO------------*------------------- which is correct.
Reading line 5 results in O--------O------OOO-----OOO------------*------------------- which is incorrect.
why ?

Using split() rather than [4:68] resolves the issue.

Macos 26,2 anaconda python 3.13.5

best regards
Reply
#2
Hi Hcyrano and welcome to python-forum.io!

Something is fundamentally broken in this code: you must not increase the loop variable i inside the loop for i in range(...). Rethink the logic of this loop. What do you want to do with these i += 1 or i += 2 ?
« We can solve any problem by introducing an extra level of indirection »
Reply
#3
Voici une traduction fidèle en anglais, adaptée au contexte de la programmation et de la gestion de données :

English Translation
"There are some subtleties in the text file.

These are two-legged matches (home and away).

One line represents the first leg, and the following line represents the return leg. However, the second line only begins appearing from the first instance of a variation in the first match.

If both matches are perfectly identical (no variations), the second match is omitted.

Consequently, the script usually skips two lines at a time, but sometimes only one.

For your information, the loop itself works perfectly fine; however, for the sake of clarity, I have replaced the for loop with a while loop."
Reply
#4
Hi,

I don't get the point... Why are from the input file some asteriks * characters removed in the expected result and why are a few left? Especially as your shown code doesn't touch any of the *.

And what you intend to achieve with lines = [line.strip() for line in f if line.strip()] specifically the if-clause? Running strip() on a string with no starting or trailing spaces simply does nothing, so it's safe to run, no guarding needed.

Regards, noisefloor
Reply
#5
Thank you for identifying the problem. It was caused by legacy code that I failed to delete. The fixed indexing failed because it was stripping leading and trailing whitespaces.
Reply
#6
(Jan-17-2026, 03:54 PM)Hcyrano Wrote: For your information, the loop itself works perfectly fine; however, for the sake of clarity, I have replaced the for loop with a while loop."
At best, incrementing the i variable in the for loop has no effect. See this example
>>> for i in range(0, 20, 2):
...     print(i)
...     i += 1000
... 
0
2
4
6
8
10
12
14
16
18
Things are different with a while loop.
« We can solve any problem by introducing an extra level of indirection »
Reply
#7
What exactly do you want to achieve?

I saved this data:

Quote:-20 -------------O----*-O------*OO----*O**----OOO-----*------------- * D7C4B6F6B5F7D3B4A5D8F3A3E8F8E7C8G3G4F1A7B3E2G6G5H5H3A6A4H4H6B7H2D1A2H7H8G2C2G8A8G7B8C1H1B2G1D2A1E1B1
8 -------------O----**O-----**OO---***OO---***OO----**-O---------- O C2E2E1C1C8E8B4A5B3A4A3D2E7F3G4G3H4G1B2G5H5H6G6H3F1D1D8B8A2F8G7G2H1A1B1H8H2G8H7PAA7B7A8A6
24 --------------O----**O---***O------OO*------O--------O--------O- * F4E2F6G4G3F2H4H5G5H3H1D6G6E7G1D2C3E1C1H6D1F1H2B5C2A4H7B3A3A2C5B2C7D7A1B1C6G7H8C8A5A6F8E8D8B6A7B7B8A8
26 --------------O----**O---***O------OO*------O--------O--------O- * F6G5G3C3C5D2F4E2C6D6F2E7B3B6D7D8G6H5D1E1C1H3C2G4F1B5E8F8H1G7C7A4A3A2C8B8A5A6B7A8A7B1H2G1H4B2A1PAH7H6H8
-2 O--------O------OOO---*--O*OO*-----**-------*------------------- * F3C5D3E3E2D6C6F5B6G4D2C2B5F7F6C7A4A6D7A5F8D8E8E7C8G6C1F1D1E1F2B1H4G5A2G1H7G2H1H2H3H5H6G7A7A8G8B8B7H8
-28 O--------O------OOO---*--O*OO*-----**-------*------------------- * E3D6C6C5D3C7B5A4A6E7D7A5C8F5B6D2E2C2F3F1F7F2C1B1F6H3G2D1G5G4H4H1H2H5A2B7H6H7G6A7B8F8G8G7E8D8H8A8G1E1

as

fp = '/home/peterr/temp/vivelafrance.txt'
Then using your function:

res = analyze_deviations(fp)
I get:

Output:
res # [10, 2, 0]
Is that anything like what you want?

To count the - in each line:

for i in range(0, len(lines) -1, 2):
    line1 = lines[i][4:]
    line2 = lines[i+1][4:]
    print(f'line1 dashes count: {line1.count('-')}')
    print(f'line2 dashes count: {line2.count('-')}')

Gives:

Output:
line1 dashes count: 50 line2 dashes count: 41 line1 dashes count: 49 line2 dashes count: 49 line1 dashes count: 50 line2 dashes count: 50
If I use .split() on a line[0] from lines:

res = lines[0].split()
len(res) # returns 4 
Result:

Output:
['-20', '-------------O----*-O------*OO----*O**----OOO-----*-------------', '*', 'D7C4B6F6B5F7D3B4A5D8F3A3E8F8E7C8G3G4F1A7B3E2G6G5H5H3A6A4H4H6B7H2D1A2H7H8G2C2G8A8G7B8C1H1B2G1D2A1E1B1']
Je n'comprends pas! Que souhaites tu accomplir exactement ?
Reply
#8
Observation based on my interpretation of the problem and the code: there is itertools.batched for batching data from the iterable into tuples of length n:

>>> from itertools import batched
>>> data = range(10)
>>> for pair in batched(data, 2):
...     print(pair)
...
(0, 1)
(2, 3)
(4, 5)
(6, 7)
(8, 9)
If overlapping is required then there is itertools.pairwise:

>>> from itertools import pairwise
>>> for pair in pairwise(data):
...     print(pair)
...
(0, 1)
(1, 2)
(2, 3)
(3, 4)
(4, 5)
(5, 6)
(6, 7)
(7, 8)
(8, 9)
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Unexpected round behavior pythonCoder 1 3,215 Feb-19-2019, 02:39 PM
Last Post: marienbad
  Unexpected ininite loop behavior RedSkeleton007 4 4,316 Jan-27-2019, 05:32 PM
Last Post: aakashjha001
  Unexpected behavior appending to a List of Dictionaries Nomad 2 3,991 Apr-03-2018, 04:22 AM
Last Post: Nomad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020