简体   繁体   English

小写和拆分输入 python 3.x

[英]Lowercase and split input python 3.x

I am trying to get an input() to be both lowercase and split (into separate words) and here is what I have:我试图让 input() 既小写又拆分(分成单独的单词),这是我所拥有的:

commandSplit = input.split()
str.join(commandSplit)
str.lower(commandSplit)

However, I receive the error:但是,我收到错误:

TypeError: descriptor 'join' requires a 'str' object but received a 'list'类型错误:描述符“join”需要“str”对象,但收到“list”

Can anyone explain what the error is and how to resolve it?任何人都可以解释错误是什么以及如何解决它?

The way I would get both lowercase and split for a string is using list comprehension.我为字符串同时获得小写和拆分的方式是使用列表理解。

mystr = 'This is Jamie Brinegar the novice Python programmer"
[word.lower() for word in mystr.split()] 

There are a few things to understand.有几件事需要理解。

First, as explained by @Patrick, your usage of str.join is incorrect.首先,正如@Patrick 所解释的,您对str.join的使用是不正确的。 The examples you copied from intended for str to be a string variable containing the delimiter you want to use to join a list of strings, ie您复制的示例旨在使str成为一个字符串变量,其中包含要用于连接字符串列表的分隔符,即

', '.join(['a', 'b', 'c'])

In your code above, it is unclear what the value of str is.在上面的代码中,尚不清楚str的值是什么。

Second, str.lower() has a similar usage: in the examples you followed, str is intended to indicate the string you want to convert to lower-case.其次, str.lower()也有类似的用法:在您遵循的示例中, str旨在指示您要转换为小写的字符串。 For example:例如:

"A String".lower() == "a string"

Finally, as to how to solve the problem at hand, the other answers give great strategies.最后,关于如何解决手头的问题,其他答案给出了很好的策略。 Essentially, convert you string to lower-case first, then split it:本质上,首先将您的字符串转换为小写,然后将其拆分:

input = 'Some User Input'
words = input.lower().split()
# => ['some','user','input']

In general you want to use the form variable.method()一般来说,你想使用形式 variable.method()

I believe what you want could be done like this我相信你想要的可以像这样完成

commandSplit = input().split()
commandSplit = [x.lower() for x in commandSplit]
var = input("Enter the line\n")
word_list = var.split(' ')
word_list = [str(word).lower() for word in word_list]
print(' '.join(word_list))

The output of split function is a list type. split 函数的输出是列表类型。 To use join function the format is ".join(sequence_data type)" With the above code:要使用连接函数,格式为“.join(sequence_data type)” 使用上面的代码:

Input:输入:

 String Split And Join

Output:输出:

 string split and join

Other answers offered good alternative solutions, I'd like to point out the reason you get其他答案提供了很好的替代解决方案,我想指出您得到的原因

TypeError: descriptor 'join' requires a 'str' object but received a 'list'类型错误:描述符“join”需要“str”对象,但收到“list”

is because you are using an unbounded method of class str (string) without an instance as first parameter.是因为您使用的是str (string) 类的无界方法,而没有实例作为第一个参数。 Python permits such usage; Python 允许这种用法; therefore, the following 2 lines yield the same result.因此,以下 2 行产生相同的结果。

str.join(", ", ["a", "b", "c"])
", ".join(["a", "b", "c"])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM