简体   繁体   English

对于句子中的不同单词,我怎样才能得到一个 output 像 A=B

[英]For different words in a sentence, how can i get an output like A=B

I'm trying to find different words in 2 different sentence and my sentences inside of Excel's A row and B row.我试图在 Excel 的 A 行和 B 行中找到 2 个不同的句子和我的句子中的不同单词。

def UncommonWords(A, B): 

    res = len(test_string.split()) 
    count = {} 

    for word in A.split(): 
        count[word] = count.get(word, 0) + 1

    for word in B.split(): 
        count[word] = count.get(word, 0) + 1

    return [word for word in count if count[word] == 1]  


A = "wu tang clan"
B = "wu tang can"

print(UncommonWords(A, B)) 
print(A,'=',B)

A = "wu tang clan" B = "wu tang can" I have A and B sentences. A = “武当派” B = “武当能” 我有 A 和 B 句。 As you can see 'clan' and 'can' words different.正如你所看到的'clan' 和'can' 词不同。 I need an output like:我需要一个 output 像:

clan=can部落=可以

So I dont want to match not unique words.所以我不想匹配不唯一的单词。 And my A and B sentences always gonna change because i will get my sentences from Excel but my code just return me我的 A 和 B 句子总是会改变,因为我会从 Excel 得到我的句子,但我的代码只是返回给我

wu tang clan=wu tang can武当族=武当罐

in my code return part give me an output like ['clan'],['can'] but this not seperated.在我的代码返回部分给我一个 output 像 ['clan'],['can'] 但这不是分开的。 I thought if i can seperate them like that 'A sentence unique word: clan' as dif1 'B sentence unique word: clan' as dif2我想如果我可以像那样将它们分开'A 句子唯一词:clan' 作为 dif1 'B 句子唯一词:clan' 作为 dif2

i will put dif1 and dif2 to print(dif1,'=',dif2) probably my output will ['clan=can'] and its so fine for me.我会将 dif1 和 dif2 放入 print(dif1,'=',dif2) 可能我的 output 将 ['clan=can'] 对我来说很好。 By the way sorry for my English, thank you for helps.顺便说一句对不起我的英语,谢谢你的帮助。

Edit1:编辑1:

 Specific examples:

 A = "put returns between two sentence"
 B = "put returns bet ween 2 sentence"

 Output should be like: between two = bet ween 2

 So i have to find unique words in A with in order then find in B, after that output should be as above.

Edit2:编辑2:

 A = "a breath before surfacing"
 B = "a be before sura"

 Output should be like: breath=be|surfacing=sura

 uncommon_word_in_A[0] ?= uncommon_word_in_B[0]
 uncommon_word_in_A[1] ?= uncommon_word_in_B[1]

The result you get is to be expected since you print your arguments instead of printing the result of the function (noticed the return statement at the end of the function?).您得到的结果是可以预料的,因为您打印的是 arguments 而不是打印 function 的结果(注意到 function 末尾的return语句?)。 You'd want something like:你会想要这样的东西:

result = UncommonWords(A, B)
print("{} = {}".format(result[0], result[1])

but this blindly assumes you always have exactly one word of difference between your two strings - which will certainly not be the case for real data.但这盲目地假设你的两个字符串之间总是只有一个词的区别——实际数据肯定不是这种情况。

Also, your code is very inefficient.此外,您的代码效率非常低。 Using the builtin set type and it's symmetric_difference operation gives the same result in much less time and much less code:使用内置的set类型和它的symmetric_difference操作可以在更少的时间和更少的代码中得到相同的结果:

print(set(A).symmetric_difference(set(B)))

Oh and yes, Python's naming conventions are all_lower for variables, functions and modules and CamelCase for classes.哦,是的,Python 的命名约定是all_lower用于变量、函数和模块,而CamelCase用于类。

Here is sample code for your reference这是供您参考的示例代码

 def UncommonWords(A, B): 
    A = A.split()
    B = B.split()
    uncommon = [(A[i],B[i]) for i in range(0,len(A)) if not str(A[i])==str(B[i])]
    return uncommon


A = "wu tang clan"
B = "wu tang can"

uncommon = UncommonWords(A, B) 
print(['='.join([i[0],i[1]]) for i in uncommon])

Hope this helps希望这可以帮助

def UncommonWords(A, B):
    A_list=A.split()
    B_list=B.split()
    uncommon_word_in_A = []
    uncommon_word_in_B = []
    i = 0
    for word in A_list:
        if word not in B_list:
            uncommon_word_in_A.append(word)


    for word2 in B_list:
        if word2 not in A_list:
            uncommon_word_in_B.append(word2)

    for i in range(0, len(uncommon_word_in_A)):
        print(uncommon_word_in_A[i], end=" ")

    print("=", end=" ")

    for i in range(0, len(uncommon_word_in_B)):
        print(uncommon_word_in_B[i], end=" ")


A = "put returns between two sentence"
B = "put returns bet ween 2 sentence"

UncommonWords(A, B)

Output: Output:

 between two = bet ween 2 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 NLTK 从句子中获取单词对? - How can I get pairs of words from a sentence with NLTK? 如何使用NLTK或stanfordcorenlp等python包提取句子中的场景/地点单词? - How can I extract scene/place words in a sentence by using python packages like NLTK or stanfordcorenlp? 如何在一个句子中获得单词的长度? - How to get the length of words in a sentence? 如何检查一个句子中是否出现多个单词? - How can I check if multiple words appear in a sentence? 如何从句子中删除数字和长度小于 2 的单词? - How can I remove numbers, and words with length below 2, from a sentence? 如何对这些单词进行排序以在 Python 中制作这句话? - How can I sort these words to make this sentence in Python? 如何删除以特定字符集开头的句子中的单词? - How can I remove words in a sentence starting with a particular set of characters? 我怎样才能得到像 output 这样的答案? 代码是什么 - How can i get the answer like output? What will be the code 如何在Python中将0x1b87打印为\\ x1b \\ x87? - How can I get 0x1b87 to print like \x1b\x87 in Python? 创建常用词列表时出现意外 output。 如何获得给定 class 的前 10 个最常用词? - Unexpected output when creating a list of frequent words. How can I get the top 10 most frequent words for a given class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM