Python 使用 len 作为文本但丢弃计数中的空格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19669001/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 14:18:34  来源:igfitidea点击:

Using len for text but discarding spaces in the count

pythonpython-3.x

提问by Izento

So, I am trying to create a program which counts the number of characters in a string which the user inputs, but I want to discard any spaces that the user enters.

因此,我正在尝试创建一个程序来计算用户输入的字符串中的字符数,但我想丢弃用户输入的任何空格。

def main():
    full_name = str(input("Please enter in a full name: ")).split(" ")

    for x in full_name:
        print(len(x))


main()

Using this, I can get the number of the characters in each word, without spaces, but I don't know how to add each number together and print the total.

使用这个,我可以获得每个单词中的字符数,没有空格,但我不知道如何将每个数字加在一起并打印总数。

采纳答案by inspectorG4dget

Count the length and subtract the number of spaces:

计算长度并减去空格数:

>>> full_name = input("Please enter in a full name: ")
Please enter in a full name: john smith
>>> len(full_name) - full_name.count(' ')
9
>>> len(full_name)

回答by Ashwini Chaudhary

Use sumwith a generator expression:

使用sum与发电机的表达:

>>> text = 'foo  bar  spam'
>>> sum(len(x) for x in text.split())
10

Or str.translatewith len:

str.translatelen

>>> from string import whitespace
>>> len(text.translate(None, whitespace)) #Handles all types of whitespace characters
10

回答by Ashwini Chaudhary

Why can't you just do:

你为什么不能这样做:

>>> mystr = input("Please enter in a full name: ")
Please enter in a full name: iCodez wrote this
>>> len(mystr.replace(" ", ""))
15
>>> len(mystr)
17
>>>

This gets the length of the string minus the spaces.

这将获得字符串的长度减去空格。

回答by oleg

I can propose a few versions.

我可以提出几个版本。

You can replace each space with an empty string and calculate the length:

您可以用空字符串替换每个空格并计算长度:

len(mystr.replace(" ", ""))

You can calculate the length of the whole string and subtract the number of spaces:

您可以计算整个字符串的长度并减去空格数:

len(mystr) - mystr.count(' ')

Or you can sum the lengths of all substrings after splitting the string with spaces:

或者您可以在用空格分割字符串后对所有子字符串的长度求和:

sum(map(len, mystr.split(' ')))

回答by rlms

Some code as close as possible to your original:

一些尽可能接近原始代码的代码:

def main():
    full_name = input("Please enter in a full name: ").split()
    total = 0
    for x in full_name:
        total += len(x)
    print(total)

However, I think len(full_name) - full_name.count(' ')is better.

不过,我觉得len(full_name) - full_name.count(' ')还好。

回答by Laurent LAPORTE

To count the number of characters excluding spaces, you can simply do:

要计算不包括空格的字符数,您可以简单地执行以下操作:

>>> full_name = "John DOE"
>>> len(full_name) - full_name.count(' ')
7

回答by Matteo Italia

You can also do

你也可以这样做

sum(1 for c in s if c!=' ') 

Which avoids any unnecessary temporary string or list.

这避免了任何不必要的临时字符串或列表。