0

假设列表中的每个字母都称为“字母”。我遇到的问题是,当字符串 == 字母第一次出现在特定列表中时,我必须将一个值附加到新列表中。之后,如果该特定列表中再次出现字符串 == 字母,我只需要更新该值。所以,是的,如果你们中的任何一个有经验的人可以帮助我,那就太好了。

谢谢

4

2 回答 2

3

您可以为此使用列表推导。

def num_common_char(lst, string):
    return [sublist.count(string) for sublist in lst]
于 2012-11-09T22:44:40.237 回答
0

You need some logic that states in words: "if not yet seen, add to list, otherwise increment". Or just be pythonic and follow @Tim's suggestion

于 2012-11-09T22:48:00.610 回答