python中的`isalpha()`未定义,尽管它是内置的(我认为)。

huangapple go评论73阅读模式
英文:

python say isalpha() is not defined despite it being built in (I think)

问题

我正在运行一个程序该程序接收一个包含数字和字母的列表将数字分离到一个单独的列表中并打印该列表但每次运行代码时都会显示isalpha未定义

yes = []
item=[1,7,-10,34,2,"a",-8]
for things in item:
if isinstance(things, str):
continue
else:
yes.append(things)
print(yes)


<details>
<summary>英文:</summary>

i am running a program that takes a list of numbers and letters, and separates the numbers into a sperate list and printing that list, but every time i run the code it says that isalpha is not defined. 


yes = []
item=[1,7,-10,34,2,"a",-8]
for things in item:
if isalpha() ==True:
continue
else:
yes.append
print(yes)


</details>


# 答案1
**得分**: 1

```isalpha()``` 是 ```str``` 类的一个方法,如果字符串中的所有字符都是字母,它会返回 ```True```,否则返回 ```False```。示例:

```python
>>> MyString = "fwUBCEF&#232;fewf"
>>> MyString.isalpha()
True
>>> MyOtherString = "f13bbG"
>>> MyOtherString.isalpha()
False

为了将项目数组中的字母和数字分开,你需要像这样重新编写代码:

letters = []
numbers = []

item = [1, 7, -10, 34, 2, "a", -8]
for things in item:
    if str(things).isalpha():
        letters.append(things)
    else:
        numbers.append(things)

print(letters)
print(numbers)

# 输出:
# ['a']
# [1, 7, -10, 34, 2, -8]

在条件内部需要使用 str(things),因为isalpha() 只能用于字符串,而不是整数。如果不添加这个,你将会得到一个错误,错误消息为 AttributeError: 'int' object has no attribute 'isalpha'

你也可以使用 isinstance() 函数来检查 things 是否是字符串。可以这样做:

letters = []
numbers = []

item = [1, 7, -10, 34, 2, "a", -8]
for things in item:
    if isinstance(things, str) and things.isalpha():
        letters.append(things)
    else:
        numbers.append(things)

print(letters)
print(numbers)

# 输出:
# ['a']
# [1, 7, -10, 34, 2, -8]

由于字母只能是字符串,你可以使用isinstance() 函数检查things是否是str 类型。在这种情况下,两个条件都必须为 True

希望这能帮助你 python中的`isalpha()`未定义,尽管它是内置的(我认为)。

英文:

isalpha() is a method for the str class which returns True if all the characters inside a string are letters and False if that's not the case. Example:

&gt;&gt;&gt; MyString = &quot;fwUBCEF&#232;fewf&quot;
&gt;&gt;&gt; MyString.isalpha()
True
&gt;&gt;&gt; MyOtherString = &quot;f13bbG&quot;
&gt;&gt;&gt; MyOtherString.isalpha()
False

In order to separate all the letters and numbers inside the item array you need to rewrite your code like this:

letters = []
numbers = []

item=[1,7,-10,34,2,&quot;a&quot;,-8]
for things in item:
    if str(things).isalpha():
        letters.append(things)
    else:
        numbers.append(things)

print(letters)
print(numbers)

# output:
# [&#39;a&#39;]
# [1, 7, -10, 34, 2, -8]

You will have to do str(things) inside the conditions because the isalpha() is only a function you can use on strings and not integers. If you don't add this you will get an error saying AttributeError: &#39;int&#39; object has no attribute &#39;isalpha&#39;.

You could also do this if you use the isinstance() function to check wether things is a string or not. You would do it like this:

letters = []
numbers = []

item=[1,7,-10,34,2,&quot;a&quot;,-8]
for things in item:
    if isinstance(things, str) and things.isalpha():
        letters.append(things)
    else:
        numbers.append(things)

print(letters)
print(numbers)

# output:
# [&#39;a&#39;]
# [1, 7, -10, 34, 2, -8]

Since the letters can only be strings you can check if the things is of type str with the isinstance() function. Here two conditions have to be True.

I hope that I could help you python中的`isalpha()`未定义,尽管它是内置的(我认为)。

答案2

得分: 0

.isalpha 是一个方法而不是一个函数 <sup>(查看此教程 以了解更多区别)</sup>,因此不能按名称调用。我认为您想要

    if str(things).isalpha(): continue

或者

    if isinstance(things, str) and things.isalpha(): 
        continue

<br>

另外,在 continue 后面不需要 else,所以可以继续使用 yes.append(things) <sup>(而不仅仅是 yes.append)</sup>:

yes = []
item=[1,7,-10,34,2,"a",-8]
for things in item:
    if isinstance(things, str) and things.isalpha(): 
        continue
    yes.append(things)
    print(yes)

(但为什么您要在循环内部而不是在循环之后打印 yes [可能重复]?)


或者,您可以只是使用 列表推导 <sup>(查看输出)</sup>

item=[1,7,-10,34,2,"a",-8]
yes = [i for i in item if not (isinstance(i, str) and i.isalpha())] 
print(*yes, sep='\n')
英文:

.isalpha is a method not a function <sup>(see this tutorial to learn more about the difference)</sup>, so it cannot be called by name. I think you want

    if str(things).isalpha(): continue

or

    if isinstance(things, str) and things.isalpha(): 
        continue

<br>

Also, the else is not necessary after continue, so you can follow with yes.append(things) <sup>(not just yes.append)</sup>:

yes = []
item=[1,7,-10,34,2,&quot;a&quot;,-8]
for things in item:
    if isinstance(things, str) and things.isalpha(): 
        continue
    yes.append(things)
    print(yes)

(But why are you printing yes [potentially on repeat] inside the loop instead of after the loop?)


Alternately you could just use list comprehension <sup>(view output)</sup>

item=[1,7,-10,34,2,&quot;a&quot;,-8]
yes = [i for i in item if not (isinstance(i, str) and i.isalpha())] 
print(*yes, sep=&#39;\n&#39;)

huangapple
  • 本文由 发表于 2023年3月7日 04:57:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/75655791.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定