Seite 1 von 1

Elemente in eine andere Liste verschieben

Verfasst: Donnerstag 11. Juni 2020, 16:22
von nieselfriem
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

Re: Elemente in eine andere Liste verschieben

Verfasst: Donnerstag 11. Juni 2020, 16:36
von __blackjack__
@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)