1
#!/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?

4

2 回答 2

2

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

于 2013-10-23T09:40:54.993 回答
1

text.count()如果要显示,则需要存储结果;计数未添加到列表中:

print text.count('ist')

或者

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

您不能调用.join()列表,它是字符串的方法。给定一个要加入的字符串传入一个列表。同样,需要捕获返回值:

joined = ' '.join(text)
print joined
于 2013-10-23T09:46:12.287 回答