Home > front end >  sort first numbers before the space
sort first numbers before the space

Time:04-17

I greet everyone I need help and thank you so much

so I have this code

alphabet = {
    "A": 1,
    "Á": 1,
    "Ä": 1,
    "Ă": 1,
    "á": 1,
    "a": 1,
    "B": 2,
    "b": 2,
    "C": 3,
    "Č": 3,
    "č": 3,
    "c": 3,
    "D": 4,
    "d": 4,
    "Ď": 4,
    "ď": 4,
    "E": 5,
    "É": 5,
    "e": 5,
    "é": 5,
    "ě": 5,
    "F": 6,
    "f": 6,
    "G": 7,
    "g": 7,
    "H": 8,
    "h": 8,
    "I": 9,
    "i": 9,
    "í": 9,
    "Í": 9,
    "J": 10,
    "j": 10,
    "k": 11,
    "K": 11,
    "L": 12,
    "l": 12,
    "M": 13,
    "N": 14,
    "O": 15,
    "ó": 15,
    "P": 16,
    "Q": 17,
    "R": 18,
    "ř": 18,
    "S": 19,
    "T": 20,
    "Ť": 20,
    "ť": 20,
    "U": 21,
    "V": 22,
    "W": 23,
    "X": 24,
    "Y": 25,
    "ý": 25,
    "m": 13,
    "n": 14,
    "ň": 14,
    "o": 15,
    "Ó": 15,
    "p": 16,
    "q": 17,
    "r": 18,
    "Ř": 18,
    "s": 19,
    "Š": 19,
    "š": 19,
    "t": 20,
    "u": 21,
    "ú": 21,
    "Ú": 21,
    "ů": 21,
    "v": 22,
    "V": 22,
    "w": 23,
    "x": 24,
    "Ý": 25,
    "y": 25,
    "z": 26,
    "ž": 26,
    "Z": 26,
    "Ž": 26,
    "1": 0,
    "2": 0,
    "3": 0,
    "4": 0,
    "5": 0,
    "6": 0,
    "7": 0,
    "8": 0,
    "9": 0,
    "0": 0,
    ",": 0,
    ".": 0,
    ",": 0,
    ":": 0,
    "?": 0,
    "=": 0,
    " ": 0,
    "-": 0,
    "ˇ": 0,
    "!": 0,
    "„": 0,
    "“": 0,
    ";": 0,
    "»": 0,
    "«": 0,
    "\t": 0,
    "'": 0,
    "(": 0,
    ")": 0,
    '"': 0,
    '\n': 0,
    "`": 0,
    "´": 0,
    " ": 0
}


def gematria(word):
    result = 0
    for char in word:
        result  = alphabet[char]
    return result

with open('3.txt',encoding="utf8") as f:
    line = f.readline()
    while line:
        line = f.readline()
        print(gematria(line),line, file=open("" str("66") ".txt", "a",encoding="utf8"))

with open('66.txt',encoding="utf8") as infile, open('outpu.txt', 'w') as outfile:
    for line in infile:
        if not line.strip(): continue  
        outfile.write(line)
with open("outpu.txt", "r") as f:
    lines = f.readlines()
with open("yourfile.txt", "w") as f:
    for line in lines:
        if line.strip("\n") != "0 ":
            f.write(line)

and need the file file text to arrange the numbers in a row in the diagram

the script works by opening the file 3.text there go on each line of similar sentences

input

11:1:2 Thank you for your help
11:1:1 and here is the text and the others continue the same lines

and this script eats da gematriie and need to sort the first came before the gap sort method but I do not know how to do it and saved it to a file

output

553 11:1:1 and here is the text and the others continue the same lines
274 1:1:2 Thank you for your help

I want it to be sorted from the smallest to the largest numbers before the space

274 1:1:2 Thank you for your help
553 11:1:1 and here is the text and the others continue the same lines

CodePudding user response:

Read the lines and calculate the value, then sort them and write the file:

def gematria(word):
    # using a "generator expression"
    return sum(alphabet[char] for char in word)

with open('input.txt', encoding='utf8') as f:
      # A generator expression that stores a sorted tuple of 
      # the calculation and the stripped line.
      lines = sorted((gematria(line),line.strip()) for line in f)

with open('output.txt', 'w', encoding='utf8') as f:
    for num,line in lines:
        # Looks like you wanted to skip blank lines
        if line:
            print(num, line, file=f)
  • Related