在python中忽略大小写的简单方法?

2024-09-27 22:24:26 发布

您现在位置:Python中文网/ 问答频道 /正文

for wclass in word_class_dict[most_ambigious_word]:
    for sent in brown_sents:
        if (most_ambigious_word.capitalize(), wclass) in sent or (most_ambigious_word.upper(), wclass) in sent or (most_ambigious_word.lower(), wclass) in sent:
            print most_ambigious_word,"-",wclass
            print " ".join(tuple[0] for tuple in sent)
            break

为了澄清brown_sents是一个元组列表,不能更改。至于简化的部分,我发现这3个不同的检查有点难写。有什么想法吗?在

编辑(对于那些对分配任务感兴趣的人): brown-sents是元组的列表,其中包含以下元素:

^{pr2}$

所以,我在找say word1,但情况应该不重要。一、 例如,word1与{}和{}相同。wclass是wordclass,所以我只想打印出包含不同的word1,wclass对的句子(显然,如果word1有多个wordclass,我想遍历wordclass并打印出所有单词的一个示例,这是最外层的for循环)。在


Tags: orinmostforwordsent元组print
3条回答
for wclass in word_class_dict[most_ambigious_word]:
    for sent in brown_sents:
        if (most_ambigious_word.lower(), wclass) in ((word[0].lower(),word[1]) for word in sent) :
            print most_ambigious_word,"-",wclass
            print " ".join(tuple[0] for tuple in sent)
            break

这其实就是所需要的。在

这应该是有效的:

if (most_ambigious_word.lower(), wclass) in (sent[0].lower(), sent[1]):
    # ...

如果搜索多个单词,则创建一个集合是有意义的:

print(set(brown_sents).intersection(zip(repeat(most_ambiguous_word),
                                        word_class_dict[most_ambiguous_word])))

Example

^{pr2}$

输出

{('word2', 'wordclass2'), ('word2', 'wordclass3')}

要理解它的作用,请将脚本保存到一个文件中,例如search-word.py,然后运行:

$ python -i search-word.py

它显示Python提示符:

>>>

您可以尝试单个表达式来查看它们的作用,例如:

>>> zip(repeat('a'), [1,2,3])
[('a', 1), ('a', 2), ('a', 3)]
>>> set('abcaadeff')
set(['a', 'c', 'b', 'e', 'd', 'f'])
>>> set('abcaadeff').intersection('abc')
set(['a', 'c', 'b'])

查看帮助:

>>> help(zip)
Help on built-in function zip in module __builtin__:

zip(...)
    zip(seq1 [, seq2 [...]]) -> [(seq1[0], seq2[0] ...), (...)]

    Return a list of tuples, where each tuple contains the i-th element
    from each of the argument sequences.  The returned list is truncated
    in length to the length of the shortest argument sequence.

q退出。如果个别帮助信息不清晰:

>>> help(repeat)
Help on class repeat in module itertools:

class repeat(__builtin__.object)
 |  repeat(element [,times]) -> create an iterator which returns the element
 |  for the specified number of times.  If not specified, returns the element
 |  endlessly.
...[snip]...

请尝试查看模块的联机帮助:

>>> module = 'itertools'
>>> import webbrowser
>>> webbrowser.open('http://docs.python.org/library/' + module)

找到^{}函数。在

简而言之:阅读文档,在提示下尝试一些代码,重复。如果你卡住了,ask question。在

相关问题 更多 >

    热门问题