在列表列表中查找列表的长度,即使我使用 len(),代码显示的长度不正确

  • 本文关键字:列表 不正确 代码 显示 len 查找 python
  • 更新时间 :
  • 英文 :


我创建了一个列表列表(如下所示(。字符串有6个字符长,但len返回的是1而不是6,我不知道为什么。问题代码和输出

Motifs = [['AACGTA'],
['CCCGTT'],
['CACCTT'],
['GGATTA'],
['TTCCGG']]
#print(Motifs)
#count = {"A":[],"C":[],"G":[],"T":[]}
#print(count)
stringlenght = Motifs[0]
print(stringlenght)
k = len(stringlenght)
print(k)
#k should be 6 but is printing as 1   
n = len('AACGTA')
print(n)
#n which is the same as Motifs[0] prints properly as 6
这是因为Motifs[0]也是一个列表。你可以做k = len(stringlength[0]),也可以修改Modifs:
Motifs = ['AACGTA', 'CCCGTT', 'CACCTT', 'GGATTA', 'TTCCGG']

然后它就起作用了。

最新更新