Hirdetés

Keresés

Új hozzászólás Aktív témák

  • sztanozs

    veterán

    válasz jerry311 #4273 üzenetére

    miert kell 7 dict es nem egy dict 7 elemmel?
    szoveg='''AMRunningMode : Normal
    AntispywareSignatureLastUpdated : 29/04/2024 05:36:35
    AntispywareSignatureVersion : 1.409.590.0
    AntivirusSignatureLastUpdated : 29/04/2024 05:36:33
    AntivirusSignatureVersion : 1.409.590.0
    NISSignatureLastUpdated : 29/04/2024 05:36:33
    NISSignatureVersion : 1.409.590.0'''
    cleanlines = [{k.strip():v.strip() for k,v in [line.split(':', maxsplit=1)]} for line in szoveg.splitlines() if line.strip()]

  • axioma

    veterán

    válasz jerry311 #4273 üzenetére

    Nem egeszen egyertelmu, hogy ekvivalens kodra vagy-e kivancsi, de mondjuk.
    A te logikaddal csak "more pythonic way":

    def parseupdates(dictentry):
    cleanlines = [line for line in dictentry.splitlines() if line.strip()]
    d = " : "
    workarray = {}
    for line in cleanlines:
    arr = line.split(d)
    workarray[arr[0].strip()] = arr[1].strip()

    Sot, ha biztos hogy mindig pontosan egy : van benne:
    def parseupdates(dictentry):
     d = " : "
    workarray = {}
    for line in dictentry.splitlines():
       if line.strip():
    a,b = line.split(d)
    workarray[a.strip()] = b.strip()

    Azert a line nevet nem jo indexnek hasznalni, foleg hogy a for ciklus pont ki tudja venni a konkret elemet is, nem kell az indexeket kovetni. Ami me'g nagyon idegen, hogy a res dictionary-nak van inicializalva, kozben tombkent hasznaltad.

    Hm varj most nezem nem csak egy : -od van a peldaban. Es azokat te se rakod utana ossze... tehat az sztem eleve hozott volna "36":"35" , "36":"33" parokat, es mazlid hogy mind paratlan darab :, amugy me'g jobban keresztbe sikerult volna (nem futtattam, csak gyanitom)

    Szoval az eredeti feladatra talan inkabb:
    def parseupdates(dictentry):
     d = " : "
      workarray = {}
      for line in dictentry.splitlines():
       idx = line.find(d)
    if idx != -1:
         workarray[line[:idx].strip()] = line[idx+1:].strip()

Új hozzászólás Aktív témák