Re: [Tutor] joining selected items in list

2012-06-24 Thread Steven D'Aprano

David wrote:

Dear Tutors,

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.


This solves like a good case for one of the oldest, most general techniques in 
use: an accumulator.


You construct a new list, from the old list, but using a temporary accumulator 
first. The basic idea is:


for each item in old list:
if data in the accumulator is complete:
move the completed accumulator data to the new list
clear the accumulator so it is ready to start collecting fresh data
otherwise:
move item from old list to the accumulator


There's a million variations on this, depending on whether you test the 
accumulator data at the start of the loop or the end of the loop, how and 
where you process items, etc.


In your case, it sounds like it will be even easier, since you need collect 
*at most* two items:


set a temporary variable to empty  # the accumulator
for each item in old list:
if the temporary variable is not empty:
combine the temp variable and the current item
move them to the new list
otherwise:
if item is complete on its own:
move it straight into the new list
otherwise:
move it to a temporary variable



Converting this to Python is trivial:


tmp = ''
for item in old_list:
if tmp:
new_list.append(tmp + item)
else:
if stands_alone(item):  # You must write this function.
new_list.append(item)
else:
tmp = item


Define this function first:

def stands_alone(item):
decide whether item stands alone, and if so, return True
otherwise return False


That decision function is the hard part. Is there some rule that tells you 
whether or not a word in the list belongs with the next word?





--
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] joining selected items in list

2012-06-24 Thread Andreas Perstinger
On Sun, 24 Jun 2012 18:11:10 +0200 
David  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
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] joining selected items in list

2012-06-24 Thread Alan Gauld

On 24/06/12 17:11, David wrote:

Dear Tutors,

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.



Can you define the set of rules whereby items are grouped?
If not you are right there is nothing that can help you.
But if you can then we should be able to suggest something.
But for now your exact requirement seems more than a little arbitrary.


--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/



___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] joining selected items in list

2012-06-24 Thread Emile van Sebille

On 6/24/2012 9:11 AM David said...

Dear Tutors,

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'].


Can you define the complete list of items that belong together?

If so, there's are several techniques for locations a sub-list within a 
list at 
http://stackoverflow.com/questions/2250633/python-find-a-list-within-members-of-another-listin-order


But if not, I'm not sure there's an answer.

Emile





I am not aware of any list methods that can help me here, and would thus
be grateful for a hint or two.

Thank you!

David



Original list:


[['Intervall-', 'Anzahl', 'Rufzeit', 'Rufzeit', 'Rufzeit', 'Rufzeit',
'>', 'Mittlere', 'Anzahl', 'Unzul\xe4ssiger', '\xdcberlauf',
'Zielanschlu\xdf', 'keine', 'Antwort', 'nicht', 'aktiv',
'Ung\xfcltiger', 'REST', '(andere']]

What I want is this:

[['Intervall-',
'Anzahl',
'Rufzeit',
'Rufzeit',
'Rufzeit',
'Rufzeit>',
'Mittlere',
'Anzahl',
'Unzul\xe4ssiger',
'\xdcberlauf',
'Zielanschlu\xdf',
'keine Antwort',
'nicht aktiv',
'Ung\xfcltiger',
'REST' (andere']]
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor




___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] joining selected items in list

2012-06-24 Thread David
Dear Tutors,

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.

Thank you!

David



Original list:


[['Intervall-', 'Anzahl', 'Rufzeit', 'Rufzeit', 'Rufzeit', 'Rufzeit',
'>', 'Mittlere', 'Anzahl', 'Unzul\xe4ssiger', '\xdcberlauf',
'Zielanschlu\xdf', 'keine', 'Antwort', 'nicht', 'aktiv',
'Ung\xfcltiger', 'REST', '(andere']]

What I want is this:

[['Intervall-',
'Anzahl',
'Rufzeit',
'Rufzeit',
'Rufzeit',
'Rufzeit >',
'Mittlere',
'Anzahl',
'Unzul\xe4ssiger',
'\xdcberlauf',
'Zielanschlu\xdf',
'keine Antwort',
'nicht aktiv',
'Ung\xfcltiger',
'REST' (andere']]
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor