Python - 检查字母是否出现在连续的单词中

时间:2014-12-06 06:38:20

标签: python regex list

我有一个任务,我需要读取输入并检查输入是否出现在某些单词中。例如:

Who are your friends? Fred Bill Sue Simone
What is the message? Should you make tea?
Sue could have written this.

它打印"苏可以写这个,因为字母" S"," U"和" E"出现在每个连续的单词中。另一个例子是:

Who are your friends? James Nicky Jake
What is the message? join and make enough cash today!
James could have written this.
Jake could have written this.

两个名字都被打印,因为它们的两个字母在每个单词中连续出现。我有以下代码:

friends = input("Who are your friends? ").split()
message = input("What is the message? ").split()

name = []
other = []

for friend in friends:
  for f in friend.lower():
    for word in message:
      print("checking if", f, "is in", word.lower())
      if f in word.lower():
        print("Adding", f, " to name list")
        name.append(f)
        break
      else:
        other.append(f)
        continue

joinedResult = ''.join(name)

for person in friends:
  if person.lower() in joinedResult:
    print(person, "could have written this.")

它适用于第一个示例,但对于第二个示例,它打印所有三个名称:

James could have written this.
Nicky could have written this.
Jake could have written this.

我了解到代码不会检查名称中的字母是否连续,而是检查名称是否在任何单词中。我该如何解决这个问题?

6 个答案:

答案 0 :(得分:3)

您可以使用zipall()执行此操作:

friends = input("Who are your friends? ").split()
message = input("What is the message? ").lower().split()

for friend in friends:
  if len(friend) <= len(message):
    if all(x in y for x, y in zip(friend.lower(), message)):
        print(friend, "could have written this.")

<强>演示:

>>> 
Who are your friends? Fred Bill Sue Simone
What is the message? Should you make tea?
Sue could have written this.
>>> 
Who are your friends? James Nicky Jake
What is the message? join and make enough cash today!
James could have written this.
Jake could have written this.

答案 1 :(得分:2)

使用正则表达式可能会更容易一些:

friends = raw_input("Who are your friends? ").split()
message = raw_input("What is the message? ").lower()

name = []
other = []

for friend in friends:
    regStr = '\w*\s?' + ''.join(['\w*' + f + '\w*\s' for f in friend.lower()])
    if re.match(regStr, message):
        name.append(friend)

for friend in name:
    print friend + " could have written this."

正则表达式模式喜欢:朋友的\w*\s?(s)\w*\s\w*(u)\w*\s\w*(e)\w* Sue

测试用例:

Shoulde i? [不匹配]​​( sue 已找到但未连续=&gt; [S]ho[u]ld[e] i?

Should I make tea? [不匹配]​​

Should u make tea? [sue]

答案 2 :(得分:1)

请注意,我的意思是你的名字中的第n个字母必须出现在信息的第n个字中。也许我对此错了,你可以澄清一下。

您需要将其姓名中的每个字母与消息中的单词配对,然后检查是否包含在内。您可以使用zip

执行此操作
friends = 'James Nicky Jake'.split()
message = 'join and make enough cash today!'.split()

names = []
others = []

for friend in friends:
    match = True
    length = len(friend)

    for letter, word in zip(friend.lower(), message):
        if not letter in word.lower():
            match = False
            break

    if match:
        names.append(friend)
    else:
        others.append(friend)

for person in names:
        print(person, "could have written this.")

答案 3 :(得分:1)

def find_char(string, char):
    start_index = 0
    while True:
        yield string.lower().find(char, start_index)  # checks for the char in the string
        start_index += 1  # increments the index to find further in the word, 
        # eg: 
        # Bob constructed[index 0]
        # ob constructed[index 1]
        # b constructed[index 2]


def find_it(friends, message):
    friends = friends.split()
    for friend in friends:
        sequence_check = []
        for char in friend.lower():
            gen = find_char(message, char)  # creates the find_char generator
            for _ in message:  # limits the search to the length of the word
                char_index = next(gen) # try to find the index
                if char_index not in sequence_check: # if not in the sequence
                    sequence_check.append(char_index) # add it to it
                    break
        if -1 in sequence_check: # this check if every character of the name is in the word
            continue
        if sorted(sequence_check) == sequence_check: # this part check if it's in a sequence.
            print (friend + ' could have written ' + message)


find_it('James Nicky Jake', "join and make enough cash today!")
find_it('Fred Bill Sue Simone', "Should you make tea?")
find_it("Bob", "Bob constructed Balloon Town")

输出:

James could have written join and make enough cash today!
Jake could have written join and make enough cash today!
Sue could have written Should you make tea?
Bob could have written Bob constructed Balloon Town

完全重做,现在更加清洁。

大部分工作都是在find_char函数中完成的,这是一个在每次迭代中缩小搜索空间的生成器,因此它不会将'Bob'找到[0,1,0],而是[0,1] ,2]在序列中。

有任何问题,请随时提出。

答案 4 :(得分:0)

friends=["James","Nicky","Jake"]
words=["James could have written this","Jake could have written this"]
for friend in friends:
    for word in words:
        for name in word.split():
            if friend.lower()==name.lower():
                print friend,"yes"
            else:
                print friend,"no"

你可以使用这个简单的代码,而不是将letterletter进行比较,这也很容易出错,因为字母可以在字符串中的任何位置不一定是连续的。

答案 5 :(得分:0)

代码

def whosdoneit(names,message):
    good_names = []
    l_m = len(message)
    for name in names:
        if len(name) > l_m: continue
        if all(c.lower() in word.lower() for c, word in zip(name, message)):
            good_names.append(name)
    return good_names

print whosdoneit('Fred Bill Sue Simone'.split(),
                 'Should you make tea?'.split())

print whosdoneit('James Nicky Jake'.split(),
                 'join and make enough cash today!'.split())

输出

['Sue']
['James', 'Jake']

评论

该函数返回一个好名字列表,这些人可以编写包含其名字的para-acrostic,所以

  • 我们开始将列表初始化为空列表

接下来我们发现,没有比消息中的单词数更长的名称可以满足要求,所以,以后再使用它,

  • 我们计算并保存消息的长度(单词)

现在,

  • 我们遍历列表names以验证name是否符合规则

    • 如果名称太长,则无需进一步处理

    • 使用zip,我们在c namewordmessage构建了一对对象列表,字符all,我们构建了一个列表使用列表理解的布尔值

    • 如果all布尔值为真(anyname是有用的内置函数!),请将good_names附加到s列表中

  • 将好名单列表返回给来电者。

我还包含了一些模仿OP示例的函数调用。

可能期待已久的单行

def s(n,m):return [g for l in [len(m)] for g in n if len(g)<=l and all([c.lower() in w.lower() for c,w in zip(g,m)])] 适用于嫌犯 ...

{{1}}