-5

how to find words consisting of both letters and numerals values of a different length in a string

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

I want to take words consisting of both letters and numerals values from d.

and output to be like this

   ka99ap9999, kl8ar888

If answer is already available, please provide me the link, Thanks

Chaitu
  • 151
  • 2
  • 9

2 Answers2

3

As mentioned by Willem Van Onsem, 'car' is also alphanumeric.

To get ka99ap9999 as output, first check every word is alphanumeric and then check it is not alphabetic and digit only.

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

Code :

python C:/Users/punddin/PycharmProjects/demo/demo.py
ka99ap9999
kl8ar888
Dinesh Pundkar
  • 4,160
  • 1
  • 23
  • 37
2

It seems like you are looking for words where any character isdigit and any character isalpha. Just put that in a list comprehension and you are done:

>>> 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']
tobias_k
  • 81,265
  • 12
  • 120
  • 179
  • This way also works: `[w for w in " ".join(d).split() if any(c.isalpha() for c in w) and any(c.isdigit() for c in w)]` – RoadRunner Jan 09 '18 at 11:00