Elemente in eine andere Liste verschieben

Wenn du dir nicht sicher bist, in welchem der anderen Foren du die Frage stellen sollst, dann bist du hier im Forum für allgemeine Fragen sicher richtig.
Antworten
Benutzeravatar
nieselfriem
User
Beiträge: 135
Registriert: Sonntag 13. Januar 2013, 16:00

Hallo,

ich möchte eigentlich nichts kompliziertes machen, stelle mich aber aktuell einfach etwas an.
Ausgangslage:

Code: Alles auswählen

host_list_subdomain=[]
host_list=['server-d9009.subdomain.domain.de', 'server-d9010.subdomain.domain.de', 'server-a5001.subdomain.domain.de', 'server-ran001.domain.subdomain.com', 'server-jen001.domain.subdomain.com', 'server-backup-a0001.domain.subdomain.com', 'server-zen001.domain.subdomain.com', 'server-zen002.domain.subdomain.com']
Ziel:

Code: Alles auswählen

host_list_subdomain=['server-d9009.subdomain.domain.de', 'server-d9010.subdomain.domain.de']
host_list=['server-a5001.subdomain.domain.de', 'server-ran001.domain.subdomain.com', 'server-jen001.domain.subdomain.com', 'server-backup-a0001.domain.subdomain.com', 'server-zen001.domain.subdomain.com', 'server-zen002.domain.subdomain.com']
Dazu habe ich folgendes ausproboiert:

Code: Alles auswählen

host_list_subdomain=[]
host_list=['server-d9009.subdomain.domain.de', 'server-d9010.subdomain.domain.de', 'server-a5001.subdomain.domain.de', 'server-ran001.domain.subdomain.com', 'server-jen001.domain.subdomain.com', 'server-backup-a0001.domain.subdomain.com', 'server-zen001.domain.subdomain.com', 'server-zen002.domain.subdomain.com']
for host in host_list:
    if "d90" in host:
        host_list_subdomain.insert(0, host_list.pop(host_list.index(host))) 
print(host_list_subdomain)
print(host_list)
Leider kommt es zu folgendem Ergebnis:

Code: Alles auswählen

host_list_subdomain=['server-d9009.subdomain.domain.de']
host_list=['server-d9010.subdomain.domain.de', 'server-a5001.subdomain.domain.de', 'server-ran001.domain.subdomain.com', 'server-jen001.domain.subdomain.com', 'server-backup-a0001.domain.subdomain.com', 'server-zen001.domain.subdomain.com', 'server-zen002.domain.subdomain.com']
Es fehlt also die 'server-d9010.subdomain.domain.de' in der host_list_subdomain und wird in der host_list außer acht gelassen.

Was mach ich hier falsch?

VG niesel
Benutzeravatar
__blackjack__
User
Beiträge: 14052
Registriert: Samstag 2. Juni 2018, 10:21
Wohnort: 127.0.0.1
Kontaktdaten:

@nieselfriem: Man darf eine Datenstruktur über die gerade iteriert wird, nicht verändern.

Selbst wenn das ginge hast Du da komische Operationen mit ekligen Laufzeiten. `insert()` beispielsweise, oder `pop()`, oder `index()` (und das auch noch für ein Element an das man per Iteration gelangt ist und wofür man mit `enumrate()` den Index deutlich billiger und vor allem konstant billig bekommen würde).

Wie fast immer der Rat: Verändere keine Listen. Erzeuge neue Listen mit den Elementen, die Du haben willst.

Edit:

Code: Alles auswählen

    subdomain_hosts = []
    hosts = [
        'server-d9009.subdomain.domain.de',
        'server-d9010.subdomain.domain.de',
        'server-a5001.subdomain.domain.de',
        'server-ran001.domain.subdomain.com',
        'server-jen001.domain.subdomain.com',
        'server-backup-a0001.domain.subdomain.com',
        'server-zen001.domain.subdomain.com',
        'server-zen002.domain.subdomain.com',
    ]

    old_hosts = hosts
    hosts = list()
    for host in old_hosts:
        (subdomain_hosts if "d90" in host else hosts).append(host)

    print(subdomain_hosts)
    print(hosts)
“Vir, intelligence has nothing to do with politics!” — Londo Mollari
Antworten