Skip to main content
 首页 » 编程设计

python之确定列表中的任何两个字符串是否在功能上以相同的字符开头

2025年05月04日19qq78292959

所以基本上,我希望这是 False:

('click', 'bait', 'carrot', 'juice') 

这是 True,因为每个字符串都以与其他字符串不同的字符开头:

('click', 'bait', 'juice', 'silly') 

我得到的最接近的是下面的,但它看起来一点也不好看。

functools.reduce(lambda x, y: (y, x[0] != y[0]), map(operator.itemgetter(0), ('click', 'bait', 'carrot', 'juice')))[1] 

它失败了,因为它只检查彼此相邻的字符串。

请您参考如下方法:

data = ('click', 'bait', 'carrot', 'juice') 
 
len(set(w[0] for w in data)) == len(data) 
# -> False