在python中从字符串中查找由字母和数字值组成的单词

2024-10-03 13:28:17 发布

您现在位置:Python中文网/ 问答频道 /正文

如何在字符串中查找由不同长度的字母和数字值组成的单词

   d = ["his car number is ka99ap9999", "bike that met with accident is kl8ar888"]

我想从d中提取由字母和数字组成的单词

输出是这样的

   ka99ap9999, kl8ar888

如果已经有答案,请给我提供链接,谢谢


Tags: 字符串numberthatiswith字母数字单词
2条回答

似乎您正在查找any字符isdigitandany字符isalpha的单词。只要把它放在一个列表中,你就完成了:

>>> d = ["his car number is ka99ap9999", "bike that met with accident is kl8ar888"]
>>> [w for s in d for w in s.split() if any(c.isalpha() for c in w) and any(c.isdigit() for c in w)]
['ka99ap9999', 'kl8ar888']

正如Willem Van Onsem所提到的,“”也是字母数字的。你知道吗

要获得ka99ap9999作为输出,首先检查每个单词是否为字母数字,然后检查它是否仅为字母和数字。你知道吗

d = ["his car number is ka99ap9999", "bike that met with accident is kl8ar888"]

for i in d:
    for x in i.split(' '):
        if x.isalnum() and not x.isalpha() and not x.isdigit():
            print x

代码:

python C:/Users/punddin/PycharmProjects/demo/demo.py
ka99ap9999
kl8ar888

相关问题 更多 >