by Anonymous » 24 Feb 2025, 13:14
Ich habe eine Liste von Strings, die richtig gereinigt wurde (
kann sicher verwendet werden) und je nach Zahlen korrekt sortiert werden. Als kleines Beispiel: < /p>
Code: Select all
l = ['C1', 'C1,C2', 'C2,C3', 'C3,C4', 'C4', 'C5', 'C5,C6', 'C6,C7', 'C7,C8', 'C8', 'C10', 'C10,C11', 'C11,C12', 'C12,C13', 'C13']
Was ich erreichen möchte, ist es, so viele Unterschützen zu erstellen, die mit einzelne Strings beginnen und enden, das heißt:
Code: Select all
[
['C1', 'C1,C2', 'C2,C3', 'C3,C4', 'C4'],
['C5', 'C5,C6', 'C6,C7', 'C7,C8', 'C8'],
['C10', 'C10,C11', 'C11,C12', 'C12,C13', 'C13']
]
< /code>
Ich dachte, ich solle eine Logik wie den folgenden Code hinzufügen, aber ich bin mir nicht sicher, ob ich auf dem richtigen Weg bin: < /p>
tl = []
for i in l:
# just get the variable
val = i
tl.append(val)
# split by ,
val_split = len(i.split(','))
# check if the value is the first element of the list (C1)
if val == l[0]:
print(1, val)
# check if the split of the character is longer than 2 (C1,C2)
elif val_split > 1:
print(2, val)
# check is the split of the character siis equalt to 1 (C4)
elif val_split == 1:
# here the code should compare if the character is equal to the last value of the nested list. If yes go with teh next value (C5)
if val != tl[-1]:
print(3, val)
else:
print(4, val)
Ich habe eine Liste von Strings, die richtig gereinigt wurde ([code]split(',')[/code] kann sicher verwendet werden) und je nach Zahlen korrekt sortiert werden. Als kleines Beispiel: < /p>
[code]l = ['C1', 'C1,C2', 'C2,C3', 'C3,C4', 'C4', 'C5', 'C5,C6', 'C6,C7', 'C7,C8', 'C8', 'C10', 'C10,C11', 'C11,C12', 'C12,C13', 'C13']
[/code]
Was ich erreichen möchte, ist es, so viele Unterschützen zu erstellen, die mit einzelne Strings beginnen und enden, das heißt:
[code][
['C1', 'C1,C2', 'C2,C3', 'C3,C4', 'C4'],
['C5', 'C5,C6', 'C6,C7', 'C7,C8', 'C8'],
['C10', 'C10,C11', 'C11,C12', 'C12,C13', 'C13']
]
< /code>
Ich dachte, ich solle eine Logik wie den folgenden Code hinzufügen, aber ich bin mir nicht sicher, ob ich auf dem richtigen Weg bin: < /p>
tl = []
for i in l:
# just get the variable
val = i
tl.append(val)
# split by ,
val_split = len(i.split(','))
# check if the value is the first element of the list (C1)
if val == l[0]:
print(1, val)
# check if the split of the character is longer than 2 (C1,C2)
elif val_split > 1:
print(2, val)
# check is the split of the character siis equalt to 1 (C4)
elif val_split == 1:
# here the code should compare if the character is equal to the last value of the nested list. If yes go with teh next value (C5)
if val != tl[-1]:
print(3, val)
else:
print(4, val)
[/code]