简体   繁体   English

检查条件语句中的数据类型

[英]Checking data type in a conditional statement

I want to print a character from a string if it is an integer.如果它是 integer,我想从字符串中打印一个字符。

This is a simplified and specific sample code which I wrote after zeroing-in on the problem in a code I was writing for a Kata in Codewars.这是我在 Codewars 中为 Kata 编写的代码中的问题归零后编写的简化且特定的示例代码。 While the Kata is not relevant here, I can't seem to figure out how to use data type in a conditional statement (something like if type(char) == int ).虽然 Kata 在这里不相关,但我似乎无法弄清楚如何在条件语句中使用数据类型(类似于if type(char) == int )。

string = "th3is i1s anot4her ra2ndom strin5g"
for word in string:
    for char in word:
        if type(char) == int:
            print(char)
  1. You never split your string into words, so the outer loop makes no sense.您永远不会将字符串拆分为单词,因此外部循环没有意义。
  2. You are iterating over characters, which are length 1 strings.您正在迭代长度为 1 的字符串的字符。 The type of a length 1 string is never equal to int .长度为 1 的字符串的类型永远不会等于int
  3. You can use the str.isdigit method.您可以使用str.isdigit方法。

Rewritten code with a single loop:用一个循环重写代码:

for c in string:
    if c.isdigit():
        print(c)

As a oneliner:作为单行者:

print('\n'.join(c for c in string if c.isdigit()))

I think isdigit() can work, also you can use regex.我认为isdigit()可以工作,你也可以使用正则表达式。 https://www.w3schools.com/python/ref_string_isdigit.asp https://www.w3schools.com/python/ref_string_isdigit.asp

You are partially on the right track with using the type keyword, however, if you type in但是,如果您type

type(<some variable>)

into the interpreter, you will always get an output similar to the form "<class '<type'>" where <type> is the type of your variable.进入解释器,你总是会得到一个 output 类似于"<class '<type'>"的形式,其中<type>是你的变量的类型。 You can use the practically with this code (it's an example so modify it for your purpose):您可以将此代码实际使用(这是一个示例,因此请根据您的目的对其进行修改):

myvar = "some python string"

if type(myvar) == "<class 'str'>":
    # true
    print("Is a string")

now as you can imagine, this program would output现在你可以想象,这个程序将 output

>>> Its a string

You can make this work with any type by changing the str part of the "<class 'str'>" block to the type that you are looking for (for example: "<class 'int'>" for integers).您可以通过将"<class 'str'>"块的str部分更改为您正在查找的类型(例如:整数"<class 'int'>" )来使其适用于任何类型。

Hope this helps!希望这可以帮助!

This can be done with regular expressions, You can import re module in Python to use regular expressions:这可以通过正则表达式来完成,您可以在 Python 中导入re模块来使用正则表达式:

import re
sentences = "th3is i1s anot4her ra2ndom strin5g"
num = re.findall("[0-9]", sentences)
print(num)

Basically, this code will return all numerical values in that string基本上,此代码将返回该字符串中的所有数值

Don't worry.. Everybody who is a master programmer today was once a beginner and therefore would have had all obvious basic questions:).别担心.. 今天每个成为高级程序员的人都曾经是初学者,因此会有所有明显的基本问题:)。

You can use, str.isnumeric() boolean check.您可以使用 str.isnumeric() boolean 检查。 By using that you can avoid the second for loop since the second for loop doesn't actually make sense here.通过使用它,您可以避免第二个 for 循环,因为第二个 for 循环在这里实际上没有意义。

Here is the revised code-这是修改后的代码-

string = "th3is i1s anot4her ra2ndom strin5g"
for char in string:
    if char.isnumeric():
        print(char)

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

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