我试图断言列表的类型只是作为测试,但它检索到断言错误

问题描述 投票:0回答:1

所以我有一个清单: 有一个 txt 文件,我根据换行符将文本分成不同的部分 splittext = text_nonum.split(' ' * 6)

并创建了一个名为 spl_list2 的新列表

当我 print(type(spl_list2)) 时,它显示 但当我断言 type(spl_list2) == list 时,它显示断言错误。我不确定我做错了什么,以及为什么它不将 spl_list2 断言为 true,尽管当我打印它的类型时,它显示为列表。

请帮忙并提前致谢!

#to get rid of all empty elements in the list: ('')
splittext = text_nonum.split('\n' * 6)
spl_list = []
for i in splittext:
    if i not in (''):
        spl.append(i)
#then eliminate all spaces
spl_list2 = []
for m in spl_list:
    m = m.strip()
    spl_list2.append(m)

print(type(spl_list2))
assert type(spl_list2) == list

运行代码时的预期:::

##假设断言类型==列表为真,则预计不会发生任何事情



####spl_list2 是一个长文本,如下所示:

#当打印(spl_list2)时

[“成功。 成功被认为是最甜蜜的 那些从未成功过的人……”、“……”、“…………”、“ 我带你去那里,—— 陆地,嗬!永恒! 终于上岸了!”]

####(“.....”是诗句)

##例如,当我打印(spl_list2[5])时,它输出:

A bone has obligations,
A being has the same;
A marrowless assembly
Is culpabler than shame.

But how shall finished creatures
A function fresh obtain? --
Old Nicodemus' phantom
Confronting us again!

运行它给出的所有内容时:

<class 'list'>
---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
Cell In[74], line 19
     15     spl_list2.append(m)
     17 print(type(spl_list2))
---> 19 assert type(spl_list2) == list

AssertionError: 
python list assert
1个回答
0
投票

我的代码工作正常,可能是版本问题,请检查

© www.soinside.com 2019 - 2024. All rights reserved.