Marcel Iseli
Marcel Iseli

Reputation: 11

Regarding the sequence types in Python

#!/usr/bin/python
# -*- coding: utf-8 -*-

# Marcel Iseli
# Python program to manipulate a list 
# by Marcel Iseli

# initialize the variable with a list of words

word1= raw_input()

text = ['Dies', 'ist', 'ein', 'kleiner', 'Text', 'ohne', 'Umlautzeichen', 
    ',', 'der', 'schon', 'in', 'einer', 'Liste', 'gespeichert', 'ist', '.','Er',
    'ist', 'gut', 'geeignet', ',', 'um', 'den',
    'Umgang', 'mit', 'Listen', 'zu', 'verstehen']

# for the list with the name text

for item in text:
    # print the new content of text'

    print 'Bitte enter druecken, um dem Text ein Punkt hinzuzufuegen.'  

    word1 = raw_input()
    text.append('.')
    print text

    print 'Bitte enter druecken, um die Vorkommen vom finiten Verb ist zu zaehlen'

    word1 = raw_input()
    text.count('ist')
    print text

    print 'Bitte enter druecken, um einen weiteren Satz anzufuegen'

    word1 = raw_input()
    text.append('Weils so schoen ist, fuege ich jetzt noch diesen Satz hinzu')
    print text

    print 'Bitte enter druecken, um das Wort gut zu entfernen'

    word1 = raw_input()
    text.remove('gut')  
    print text

    print 'Bitte enter druecken, um das Wort hier einzufuegen.'

    word1 = raw_input()
    text.insert(1, 'hier')
    print text

    print 'Bitte enter druecken, um das Wort dies mit dem Wort das zu ersetzen'

    word1 = raw_input()
    text[0] = 'Das'

    print text

    text.join(text)

    break

The last function that I am using here, text.join(text) is not working. I would like to display the list "text" as regular text with it. Moreover when using text.count, I would like to display the result 3, but with "print text" I can't get this results..the other results appear fine while using "print text". Can somebody help me with this?

Upvotes: 1

Views: 82

Answers (2)

Martijn Pieters
Martijn Pieters

Reputation: 1121594

You need to store the results of text.count() if you want to display it; the count is not added to the list:

print text.count('ist')

or

ist_count = text.count('ist')
print ist_count

You cannot call .join() on a list, it is a method of strings instead. Given a string to join with, pass in a list. Again, the return value needs to be captured:

joined = ' '.join(text)
print joined

Upvotes: 1

Torxed
Torxed

Reputation: 23490

.join() is a function of str objects, not list objects.

dir(str) shows you what you can do with a string and this dir(list) shows you what you can do with a list.

Try:

' '.join(text)

This will join all objects of text with a separator of ' '

Upvotes: 2

Related Questions