Python dosyadan okunan verileri tekrardan işlemek

Merhabalar,

Şuana kadar yazdığım kod aşağıdaki şekilde burada mesela [1,6] nın True olduğu anda örneğin True -> [1, 6] [1, 2, 3, 6, 8, 10, 11, 12] burada 1 ve 6 arasında hangi rakamlar var. Daha sonra ayrı bir txt dosyasından 1 ve 2 arasındaki mesafe 2 ve 3 arasındaki mesafe 3 ve 6 arasındaki mesafeleri okutup 1 ve 6 arasındaki toplam mesafe ile bir işlem gerçekleştireceğim. Ama nasıl?

‘’’
def read_file(talepler: str):
with open(talepler) as f:
return [list(map(int, i.split())) for i in f.readlines()]

def check_intersection(talepler: str, hatlar: str):
for i in read_file(talepler):
for j in read_file(hatlar):
intersection = False
save = [k for k in j]
if i[0] in j:
index1 = j.index(i[0])
j.remove(i[0])
if i[1] in j:
index2 = save.index(i[1])
if index2 >= index1:
intersection = True
if intersection:
print(True, “->”, i, save)
else:
print(False, “->”, i, save)

check_intersection(“talepler.txt”, “hatlar.txt”)
print("\n")

p = 60 #periyot
frekanslar = [35,30,14,15]
durakta_bekleme_suresi = []

for f in frekanslar:
dbs = p / (2 * f)
dbs=round(dbs,1)
durakta_bekleme_suresi.append(dbs)
print (f,dbs)

from json import dump

def to_json(Hatcinsi, data):
with open(Hatcinsi, “w”) as f:
dump(data, f)

to_json(“hatcinsi.json”, check_intersection(“talepler.txt”, “hatlar.txt”))

from json import load

def from_json(Hatcinsi):
with open(Hatcinsi) as f:
return load(f)

data = from_json(“hatcinsi.json”)
‘’’

Merhaba,

Öncelikle kodunuzu kod görünümünde paylaşmanızı tavsiye ediyorum.

Örnek:

```python
for i in range(10):
    print(i)
```

Kodu yukarıdaki gibi yazarsanız, kodunuz aşağıdaki gibi görünecektir.

for i in range(10):
    print(i)

Kod görünümü verebilmek için tek tırnak (') işaretini yerine alt gr + noktalı virgül tuş kombinasyonuyla oluşturulan sola doğru yatık tırnak (`) işaretini kullanmanız gerekiyor.

Sorununuza gelecek olursak, merak ettiğim bir şey var. [1, 2, 3, 6, 8, 10, 11, 12] gibi listelerde tekrar eden sayılar oluyor mu?

def read_file(talepler: str):
    with open(talepler) as f:
        return [list(map(int, i.split())) for i in f.readlines()]
    
    
def check_intersection(talepler: str, hatlar: str):
    for i in read_file(talepler):
        for j in read_file(hatlar):
            intersection = False
            save = [k for k in j]
            if i[0] in j:
                index1 = j.index(i[0])
                j.remove(i[0])
                if i[1] in j:
                    index2 = save.index(i[1])
                    if index2 >= index1:
                        intersection = True
            if intersection:
                print(True, "->", i, save)
            else:
                print(False, "->", i, save)
      
      
check_intersection("talepler.txt", "hatlar.txt")
print("\n")

p = 60 #periyot
frekanslar = [35,30,14,15]
durakta_bekleme_suresi = []
    
    
for f in frekanslar:
    dbs = p / (2 * f)
    dbs=round(dbs,1)
    durakta_bekleme_suresi.append(dbs)
    print (f,dbs)
    

from json import dump

def to_json(Hatcinsi, data):
    with open(Hatcinsi, "w") as f:
        dump(data, f)
        
to_json("hatcinsi.json", check_intersection("talepler.txt", "hatlar.txt")) 

from json import load


def from_json(Hatcinsi):
    with open(Hatcinsi) as f:
        return load(f)


data = from_json("hatcinsi.json")

Hayır hiç tekrar eden sayım yok

O zaman aşağıdaki fonksiyonu kullanarak yapabilirsiniz herhalde. Ben size bir şablon göstereyim. Siz onu kendi kodunuza uyarlamaya çalışın:

def find_numbers_in_interval(array):
    if array[0]:
        start = array[2].index(array[1][0]) + 1
        end = array[2].index(array[1][1])
        return array[2][start: end]


# Veritabanından dönen ilk değer bu olsun
ilk_deger = [True, [1, 6], [1, 2, 3, 6, 8, 10, 11, 12]]

print(find_numbers_in_interval(ilk_deger))  # [2, 3]

Bir şeyler oluyor gibi en azından bu hafta da elimde gösterebileceğim bir sonuç oluşuyor. Tekrardan teşekkür ederim :slight_smile:

Kime göstermeniz gerekiyor?

Ben inşaat mühendisliğinde yüksek lisans öğrencisiyim. Tezim için bunu yapmam gerekiyor. Paket program kullanmayı reddedip güzel bir sonuç elde etmek ve bir adım öne çıkmak istediğim için pythonu seçtim. Yaptıklarımı danışman hocama gösteriyorum.

Hmm, yani öğrenmeniz gerekmediği halde python öğrenmeye başladınız. Güzel. Size başarılar dilerim. Bu arada umarım paylaştığım kodları anlıyorsunuzdur.

Çok teşekkür ederim. Evet bölümümde python bilen biri olmadığı için öğrenmek istedim. Ayrıca yardımcı olduğunuz bütün kodları satır satır ne anlama geldiğini araştırıp anlamaya çalışıyorum.

1 Beğeni

Peki, kolay gelsin diyeyim o zaman, iyi çalışmalar. :slightly_smiling_face:

1 Beğeni

Bu arada aşağıdaki fonksiyonu,

def intersection(x, y):
    result = {i: False for i in x}
    for i, j in enumerate(x):
        for k, m in enumerate(y):
            if j == m and all(n in y[k:] for n in x[i:]):
                result[j] = True
    return all(result.values())

şu şekilde değiştirmeniz gerekiyor.

def intersection(x, y):
    result = {i: False for i in x}
    if len(result) == 1 and y.count(x[0]) < x.count(x[0]):
        return False
    for i, j in enumerate(x):
        for k, m in enumerate(y):
            if j == m and all(n in y[k:] for n in x[i:]):
                result[j] = True
    return all(result.values())

Sadece True veren değerleri almak için de şöyle yapabilirsiniz.

def get_true_values(array):
    return [i for i in array if i[0]]


true_data = get_true_values(data)
for i in true_data:
    print(i)

Çıktı:

[True, [2, 1], [9, 12, 8, 10, 7, 2, 1, 10, 4, 1, 9, 12, 5, 2]]
[True, [2, 2], [9, 12, 8, 10, 7, 2, 1, 10, 4, 1, 9, 12, 5, 2]]
[True, [1, 4], [9, 12, 8, 10, 7, 2, 1, 10, 4, 1, 9, 12, 5, 2]]
[True, [1, 4], [14, 8, 4, 12, 13, 5, 14, 4, 1, 7, 4, 8, 9]]
[True, [1, 5], [9, 12, 8, 10, 7, 2, 1, 10, 4, 1, 9, 12, 5, 2]]
[True, [1, 7], [14, 8, 4, 12, 13, 5, 14, 4, 1, 7, 4, 8, 9]]
[True, [1, 8], [14, 8, 4, 12, 13, 5, 14, 4, 1, 7, 4, 8, 9]]
[True, [1, 9], [9, 12, 8, 10, 7, 2, 1, 10, 4, 1, 9, 12, 5, 2]]
[True, [1, 9], [14, 8, 4, 12, 13, 5, 14, 4, 1, 7, 4, 8, 9]]
[True, [2, 10], [9, 12, 8, 10, 7, 2, 1, 10, 4, 1, 9, 12, 5, 2]]
[True, [2, 10], [10, 7, 12, 11, 7, 8, 6, 13, 6, 3, 2, 12, 10]]
[True, [2, 12], [9, 12, 8, 10, 7, 2, 1, 10, 4, 1, 9, 12, 5, 2]]
[True, [2, 12], [10, 7, 12, 11, 7, 8, 6, 13, 6, 3, 2, 12, 10]]
[True, [1, 1], [9, 12, 8, 10, 7, 2, 1, 10, 4, 1, 9, 12, 5, 2]]
[True, [2, 2], [9, 12, 8, 10, 7, 2, 1, 10, 4, 1, 9, 12, 5, 2]]
[True, [2, 4], [9, 12, 8, 10, 7, 2, 1, 10, 4, 1, 9, 12, 5, 2]]
[True, [1, 5], [9, 12, 8, 10, 7, 2, 1, 10, 4, 1, 9, 12, 5, 2]]
[True, [1, 7], [14, 8, 4, 12, 13, 5, 14, 4, 1, 7, 4, 8, 9]]
[True, [2, 9], [9, 12, 8, 10, 7, 2, 1, 10, 4, 1, 9, 12, 5, 2]]
[True, [2, 10], [9, 12, 8, 10, 7, 2, 1, 10, 4, 1, 9, 12, 5, 2]]
[True, [2, 10], [10, 7, 12, 11, 7, 8, 6, 13, 6, 3, 2, 12, 10]]
[True, [1, 12], [9, 12, 8, 10, 7, 2, 1, 10, 4, 1, 9, 12, 5, 2]]
[True, [1, 1], [9, 12, 8, 10, 7, 2, 1, 10, 4, 1, 9, 12, 5, 2]]
[True, [1, 2], [9, 12, 8, 10, 7, 2, 1, 10, 4, 1, 9, 12, 5, 2]]
[True, [2, 4], [9, 12, 8, 10, 7, 2, 1, 10, 4, 1, 9, 12, 5, 2]]
[True, [2, 5], [9, 12, 8, 10, 7, 2, 1, 10, 4, 1, 9, 12, 5, 2]]
[True, [1, 7], [14, 8, 4, 12, 13, 5, 14, 4, 1, 7, 4, 8, 9]]
[True, [2, 9], [9, 12, 8, 10, 7, 2, 1, 10, 4, 1, 9, 12, 5, 2]]
[True, [2, 10], [9, 12, 8, 10, 7, 2, 1, 10, 4, 1, 9, 12, 5, 2]]
[True, [2, 10], [10, 7, 12, 11, 7, 8, 6, 13, 6, 3, 2, 12, 10]]
[True, [1, 12], [9, 12, 8, 10, 7, 2, 1, 10, 4, 1, 9, 12, 5, 2]]
[True, [2, 1], [9, 12, 8, 10, 7, 2, 1, 10, 4, 1, 9, 12, 5, 2]]
[True, [1, 2], [9, 12, 8, 10, 7, 2, 1, 10, 4, 1, 9, 12, 5, 2]]
[True, [1, 4], [9, 12, 8, 10, 7, 2, 1, 10, 4, 1, 9, 12, 5, 2]]
[True, [1, 4], [14, 8, 4, 12, 13, 5, 14, 4, 1, 7, 4, 8, 9]]
[True, [2, 5], [9, 12, 8, 10, 7, 2, 1, 10, 4, 1, 9, 12, 5, 2]]
[True, [1, 9], [9, 12, 8, 10, 7, 2, 1, 10, 4, 1, 9, 12, 5, 2]]
[True, [1, 9], [14, 8, 4, 12, 13, 5, 14, 4, 1, 7, 4, 8, 9]]
[True, [1, 10], [9, 12, 8, 10, 7, 2, 1, 10, 4, 1, 9, 12, 5, 2]]
[True, [2, 12], [9, 12, 8, 10, 7, 2, 1, 10, 4, 1, 9, 12, 5, 2]]
[True, [2, 12], [10, 7, 12, 11, 7, 8, 6, 13, 6, 3, 2, 12, 10]]

Kodun tam hali:

from random import randint
from json import dump, load


def write_file(filename: str, nl: bool):
    with open(filename, "w") as f:
        for i in range(56):
            if nl:
                if i and i % 14 == 0:
                    f.write("\n")
                else:
                    f.write(f"{randint(1, 14):<10}")
            else:
                if i >= 14:
                    i %= 14
                f.write(f"{randint(1, 2)}{i + 1:>10}\n")


def read_file(filename: str):
    with open(filename) as f:
        return [list(map(int, i.split())) for i in f.readlines()]


def intersection(x, y):
    result = {i: False for i in x}
    if len(result) == 1 and y.count(x[0]) < x.count(x[0]):
        return False
    for i, j in enumerate(x):
        for k, m in enumerate(y):
            if j == m and all(n in y[k:] for n in x[i:]):
                result[j] = True
    return all(result.values())


def check_intersection(filename1, filename2):
    return [
        (intersection(i, j), i, j)
        for i in read_file(filename1)
        for j in read_file(filename2)
    ]


def get_true_values(array):
    return [i for i in array if i[0]]


def to_json(filename, data):
    with open(filename, "w") as f:
        dump(data, f)


def from_json(filename):
    with open(filename) as f:
        return load(f)


def main():
    write_file("file1.txt", False)
    write_file("file2.txt", True)
    to_json("test.json", check_intersection("file1.txt", "file2.txt"))
    data = from_json("test.json")
    true_data = get_true_values(data)
    for i in true_data:
        print(i)


if __name__ == "__main__":
    main()
2 Beğeni

Bunları direk aklınızdan mı yazıyorsunuz yoksa örnek kodları bu şekilde bulabileceğim bir yer var mı?

Evet, aklımdan yazıyorum.

1 Beğeni