YOL*_*OLO -5 python python-3.x pandas
我有一个包含数字和名字的清单.
lst = ['new car', '232', 'famous bike','232', 'new car', '232plane', 'new car', 'plane232']
Run Code Online (Sandbox Code Playgroud)
我只想new car, famous bike数字而不是数字或字母数字.
输出将是2,因为有两个独特的单词:汽车,自行车.
我知道有一个简单的答案,但我无法理解它.
谢谢.
您可以使用它str.isalpha来确定字符串是否仅包含字母.然后set从那里做一个(制作一个独特元素的容器)然后确定它的长度set
>>> len(set(i for i in lst if i.isalpha()))
2
Run Code Online (Sandbox Code Playgroud)