当我将数据集导入数组时,每个元素的长度显示为 1

发布于 2025-01-11 21:57:49 字数 578 浏览 0 评论 0原文

我正在编写一个程序,将 .txt 文件中的一长串单词导入到一个名为 wordlist 的数组中。然后我想根据单词的长度将它们分类。然而,由于某种原因,当单词存储在数组中时,每个单词的长度都显示为 1。

这是代码

wordlist = []
with open('words.txt', 'r') as words:
    for line in words:
        strplines = line.strip()
        list = strplines.split()
        wordlist.append(list)
        loading = loading + 1
        print(loading,'/ 113809 words loaded')

如果我然后执行类似的操作,

print(len(wordlist[15000]))

输出为 1,尽管该单词实际上有 6 个字符长。 我在另一个程序中尝试了这个,但唯一的区别是我手动将一些元素输入到数组中并且它起作用了。这意味着我从 .txt 文件中删除行的方式可能存在问题。

I'm making a program where I import a long list of words from a .txt file into one array called wordlist. I then want to sort them into categories based on the length of the words. However for some reason, when the words are stored in the array, the length shows up as 1 for every single one.

Here is the code

wordlist = []
with open('words.txt', 'r') as words:
    for line in words:
        strplines = line.strip()
        list = strplines.split()
        wordlist.append(list)
        loading = loading + 1
        print(loading,'/ 113809 words loaded')

If I then do something like this

print(len(wordlist[15000]))

The output is 1 despite that word actually being 6 characters long.
I tried this in another program, but the only difference was that I manually inputted a few elements into the array and it worked. That means theres probably an issue with the way I strip the lines from the .txt file.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不奢求什么 2025-01-18 21:57:49

那么单词列表是一个数组的数组?如果是这样,当您检查其元素的 len 时,它将返回该数组中的元素数量,即 1。但是如果您执行类似操作,则会

len(wordlist[1500][0])

获取存储在数组中索引 1500 处的第一个单词的 len。

So the wordlist is an array of arrays? If so when you check the len of it's element, it would return the number of elements in this array so 1. But if you do something like

len(wordlist[1500][0])

you get the len of the first word that is stored in array at index 1500.

简单气质女生网名 2025-01-18 21:57:49

看起来您不想追加到数组(您将添加一个列表),但您想要扩展数组。

并且请,请,即使内置函数不是保留字,也请避免使用它们!因此,可以将您的列表称为 lstmylist 或其他名称,但不是 list...

代码可能会变成:

wordlist = []
with open('words.txt', 'r') as words:
    for line in words:
        strplines = line.strip()
        lst = strplines.split()
        wordlist.extend(lst)
        loading = loading + 1
        print(loading,'/ 113809 words loaded')

It looks that you do not want to append to the array (you would add a list), but you want to extend the array.

And please, please, even if builtins are not reserved words, avoid to use them! So call your list lst or mylist or whatever but not list...

The code could become:

wordlist = []
with open('words.txt', 'r') as words:
    for line in words:
        strplines = line.strip()
        lst = strplines.split()
        wordlist.extend(lst)
        loading = loading + 1
        print(loading,'/ 113809 words loaded')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文