Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何创建前三个字母为‘;';没有重复和排序_Python_Python 3.x - Fatal编程技术网

Python 如何创建前三个字母为‘;';没有重复和排序

Python 如何创建前三个字母为‘;';没有重复和排序,python,python-3.x,Python,Python 3.x,创建文件中前三个字母为“the”的所有单词的列表 无视事实。这可能包括“There”、“There”,也可能包括其他人。没有重复 你打印的东西。为了清楚起见,“the”、“the”和“the”在本部分中是同一个词。分类 按字母顺序排列 我很难成功地运行代码。我应该数一数我下载的长文本样本中的每个单词,以字母“the”开头,然后按字母顺序排序 定义所有单词(单词列表): 计数={} for word in wordList: if word in wordList == 'the':

创建文件中前三个字母为“the”的所有单词的列表 无视事实。这可能包括“There”、“There”,也可能包括其他人。没有重复 你打印的东西。为了清楚起见,“the”、“the”和“the”在本部分中是同一个词。分类 按字母顺序排列


我很难成功地运行代码。我应该数一数我下载的长文本样本中的每个单词,以字母“the”开头,然后按字母顺序排序

定义所有单词(单词列表): 计数={}

for word in wordList:
    if word in wordList == 'the':
        ''.sorted(wordList) #to sort alphabetically

for wordList in words:           #so there are no duplicates 
    if allWords(wordList):
        if wordList not in shortList:
            shortList.append(wordList)
以下是我的解决方案:

l = ['therefore', 'THEREIS', 'Thereafter', 'THEY', 'THEASX', 'THY', 'THEEE', 'abcthe']

def allWords(wordList):
    return filter(myFunc, wordList)

def myFunc(word):
    word = word.lower()
    if ('the' in word and 'the' in word[:3]):
        return True
    else:
        return False

count = list(dict.fromkeys(sorted(allWords(l))))
for w in count:
    print(w)

您可以使用
set
强制不重复:

words=[“因此”,“有”]#等
new_list=排序(set(如果words.lower().startswith('the')),则在words中逐字设置)

请展示您已经尝试过的内容发布代码后,告诉我们问题出在哪里。我很难成功运行代码。我应该数一数我下载的长文本样本中的每个单词,以字母“the”开头,然后按字母顺序排序。很抱歉给您带来任何困惑,这是我第一次访问此网站。您可以使用
.lower()
.startswith()
检查字符串是否以某一组字符开头,还有一个
设置
来强制它不具有重复项,这样当我通过函数放入较大的文本文件时,打印的结果不会打印重复项?列表(dict.fromkeys())应该处理重复项。我在l中用一个重复的字符串尝试了它,它只包含了一次。非常感谢!这帮了大忙