Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/278.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.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_Text_Split - Fatal编程技术网

Python 拆分函数使用标点符号拆分单词。想预防。拆分后如何按字母顺序排列?

Python 拆分函数使用标点符号拆分单词。想预防。拆分后如何按字母顺序排列?,python,text,split,Python,Text,Split,我有两个问题。代码如下: Read =open("C:\Users\Moondra\Desktop/test1.txt",'r') text =Read.read() words =text.split() print(words) print(words.sort()) ##counts=dict() ##for word in words: ## counts[word] = counts.get(word,0)+1 ## ## ##print counts 我想读的文字是

我有两个问题。代码如下:

Read =open("C:\Users\Moondra\Desktop/test1.txt",'r')
text =Read.read()
words =text.split()
print(words)
print(words.sort())

##counts=dict()
##for word in words:
##    counts[word] = counts.get(word,0)+1
##
##
##print counts    
我想读的文字是:

test1.txt

你好

一切都好。发生了什么事? 你在哪?你好

希望很快见到你

你什么时候过来

晚餐我该做什么

结束

来自txt文件的文本结束

我的两个问题如下:

  • 我正在尝试实现一个计数每个单词的代码,其中我计算每个单词在文档中出现的次数。 但是,当我使用上述代码拆分单词时,“Hello”将分别显示为“Hello!”、“Hello.”甚至“Hello.”。我怎样才能避免这种情况

  • 接下来,我尝试按字母顺序对列表中的元素进行排序,但运行
    sort()
    方法后得到的结果是
    none
    ,这让我很困惑


  • 谢谢

    此代码适用于您描述的内容:

    import re
    
    with open("C:\Users\Moondra\Desktop/test1.txt", 'r') as file:
        file = file.read()
    
    words_list = re.findall(r"[\w]+", file)
    words_list = sorted(words_list, key=str.lower)
    
    patterns = ["Hello"]
    counter = 0
    
    
    for word in words_list:
        for pattern in patterns:
            if word == pattern:
                counter+=1
    
    print("The word Hello occurred {0} times".format(counter)) # prints the number of times 'Hello' was found
    print(words_list) # prints your list alphabetically
    
    但是,您应该注意以下几点:

    • 我使用了
      re
      模块而不是sort。这是因为在re模块中使用引擎比使用
      split()
      函数尝试拆分字符串要简单得多
    • 我重命名了一些变量,以遵循Python的指南和命名约定。根据您的喜好随意重命名
    • sort()
      返回列表的原因是列表的
      sort()
      属性不返回新列表,而是更改旧列表。也就是说,列表的
      sort()
      属性进行适当排序。您使用的
      sort()
      返回数据类型
      None
      。您需要使用内置Python函数
      sorted()
      sorted()
      函数返回数据类型
      list

    此代码适用于您描述的内容:

    import re
    
    with open("C:\Users\Moondra\Desktop/test1.txt", 'r') as file:
        file = file.read()
    
    words_list = re.findall(r"[\w]+", file)
    words_list = sorted(words_list, key=str.lower)
    
    patterns = ["Hello"]
    counter = 0
    
    
    for word in words_list:
        for pattern in patterns:
            if word == pattern:
                counter+=1
    
    print("The word Hello occurred {0} times".format(counter)) # prints the number of times 'Hello' was found
    print(words_list) # prints your list alphabetically
    
    但是,您应该注意以下几点:

    • 我使用了
      re
      模块而不是sort。这是因为在re模块中使用引擎比使用
      split()
      函数尝试拆分字符串要简单得多
    • 我重命名了一些变量,以遵循Python的指南和命名约定。根据您的喜好随意重命名
    • sort()
      返回列表的原因是列表的
      sort()
      属性不返回新列表,而是更改旧列表。也就是说,列表的
      sort()
      属性进行适当排序。您使用的
      sort()
      返回数据类型
      None
      。您需要使用内置Python函数
      sorted()
      sorted()
      函数返回数据类型
      list

    我想你应该看一看。我想你应该看一看。用
    with
    语句打开文件,只是为了确保读取后内存被释放?谢谢。我将对代码和模块进行修改 看看我还有什么问题。至于“sort()返回列表的原因,是因为列表的sort()属性不返回新列表,而是更改了旧列表。也就是说,列表的sort()属性进行了排序。您需要使用内置Python函数sorted()”来代替“我对这条评论有点困惑。排序函数没有返回一个列表,它返回了一个NONE(除非这是一个列表)@moondra我会看看我是否能在我的回答中澄清。用
    with
    语句打开文件不是更好,只是为了确保在阅读后内存被释放?谢谢。我将对代码和模块进行修改 看看我还有什么问题。至于“sort()返回列表的原因,是因为列表的sort()属性不返回新列表,而是更改了旧列表。也就是说,列表的sort()属性进行了排序。您需要使用内置Python函数sorted()”来代替“我对这条评论有点困惑。排序函数没有返回一个列表,它返回了一个NONE(除非这是一个列表)@moondra我看看我能否在我的答案中澄清。