python - 关于Python中的序列类型

标签 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

我在这里使用的最后一个函数,text.join(text) 不起作用。我想将列表“文本”显示为常规文本。此外,当使用 text.count 时,我想显示结果 3,但是使用“打印文本”时我无法得到此结果。使用“打印文本”时其他结果看起来很好。有人可以帮我解决这个问题吗?

最佳答案

.join()str 对象的函数,而不是 list 对象。

dir(str) 向您展示可以对字符串执行哪些操作,此 dir(list) 向您展示可以对列表执行哪些操作。

尝试:

' '.join(text)

这将使用 ' ' 分隔符连接 text 的所有对象

关于python - 关于Python中的序列类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19538070/

相关文章:

javascript - 在 python 中, "export"来自模块的定制对象

python - Python 中的简单 3D 图形

python正则表达式获取电子邮件地址的第一部分

python - 通过映射到包含另一列中字符串值子集的字典来创建数据帧列。作为其关键

python - 与列表和长度一起使用的 while 语句

python - 如何绘制渐近线?

python - HINT : There is a column named "title" in table "wenzhang", 但不能从这部分查询中引用

python - 替换数据框中的值,行名等于列名

python - 线性回归没有按我的预期工作

python - 谷歌Foobar L4 : Bringing a gun to a trainer fight