jam*_*sas 2 python string character
假设我有一个单词列表:resign resin redyed resist reeded
我也有一个字符串".10.10"
我需要遍历列表并找到字符串中有数字的相同位置中存在重复字符的单词.
例如,字符串".10.10"会找到"redyed"这个词,因为有1个是e,而d是0.
另一个字符串".00.0." 会找到"reeded"这个词,因为那个位置有e.
到目前为止,我在python中的尝试并不值得打印.目前我查看字符串,将所有0添加到数组,将1添加到数组,然后尝试在数组位置中查找重复的字符.但它非常笨拙并且无法正常工作.
def matches(s, pattern):
d = {}
return all(cp == "." or d.setdefault(cp, cs) == cs
for cs, cp in zip(s, pattern))
a = ["resign", "resins", "redyed", "resist", "reeded"]
print [s for s in a if matches(s, ".01.01")]
print [s for s in a if matches(s, ".00.0.")]
Run Code Online (Sandbox Code Playgroud)
版画
['redyed']
['reeded']
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
413 次 |
最近记录: |