[Tutor] joining selected items in list

Andreas Perstinger andipersti at gmail.com
Sun Jun 24 22:43:38 CEST 2012


On Sun, 24 Jun 2012 18:11:10 +0200 
David <ldl08 at gmx.net> wrote:

> I have a list that I wish to reorganise into fewer list items.
> What happens is that some of the items belong together:
> not ['keine', 'Antwort'] but ['Keine Antwort'].
> 
> I am not aware of any list methods that can help me here, and would
> thus be grateful for a hint or two.

If you know the indeces of the items which belong together, you could
do for example:

l = [['Intervall-', 'Anzahl', 'Rufzeit', 'Rufzeit', 'Rufzeit',
      'Rufzeit', '>', 'Mittlere', 'Anzahl', 'Unzul\xe4ssiger',
      '\xdcberlauf', 'Zielanschlu\xdf', 'keine', 'Antwort', 'nicht',
      'aktiv', 'Ung \xfcltiger', 'REST', '(andere']]
indices = [5, 12, 14, 17]

for index in reversed(indices):
    l[0][index] = " ".join([l[0][index], l[0].pop(index + 1)])

Bye, Andreas


More information about the Tutor mailing list