gpt4 book ai didi

python - 关于Python中的序列类型

转载 作者:行者123 更新时间:2023-11-30 23:32:07 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com