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

ここで使用している最後の関数 text.join(text) が機能していません。リスト「テキスト」を通常のテキストとして表示したいと思います。また、text.count を使用する場合、結果 3 を表示したいのですが、「印刷テキスト」ではこの結果が得られません。「印刷テキスト」を使用すると、他の結果は正常に表示されます。誰かがこれで私を助けることができますか?

4

2 に答える 2

2

.join()strオブジェクトではなく、オブジェクトの機能listです。

dir(str)は文字列でdir(list)何ができるかを示し、これはリストで何ができるかを示しています。

試す:

' '.join(text)

textこれにより、 のすべてのオブジェクトがのセパレーターで結合されます' '

于 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 に答える