在python中创建列表时获取列表对象的消息内置方法附加

时间:2017-01-29 07:39:11

标签: python list

我正在尝试构建一个列表,其中包含列表中列表的第二个元素。我首先抓取了一些链接并存储在links_2016

for i in links_2016:
    parsed = urlparse.urlparse(i)
    path = parsed[2] 
    pathlist = path.split("/")
    list_pathlist.append(pathlist)

#get the months
months = []
for i in range(0,44): #there are 44 elements in list_pathlist
    list_pathlist[i][2] #get the second element 
    months.append

但是当我打印几个月时,我会在列表中获得内置方法消息:

'06'
<built-in method append of list object at 0x103c85170>
'06'
<built-in method append of list object at 0x103c85170>
'06'
<built-in method append of list object at 0x103c85170>
'06'
<built-in method append of list object at 0x103c85170>
'06'
<built-in method append of list object at 0x103c85170>
'06'
<built-in method append of list object at 0x103c85170>
'06'
<built-in method append of list object at 0x103c85170>
'06'
<built-in method append of list object at 0x103c85170>
'06'
<built-in method append of list object at 0x103c85170>
'06'
<built-in method append of list object at 0x103c85170>
'06'
<built-in method append of list object at 0x103c85170>
'06'
<built-in method append of list object at 0x103c85170>
'06'
<built-in method append of list object at 0x103c85170>
'06'
<built-in method append of list object at 0x103c85170>
'06'
<built-in method append of list object at 0x103c85170>
'06'
<built-in method append of list object at 0x103c85170>
'06'
<built-in method append of list object at 0x103c85170>
'06'
<built-in method append of list object at 0x103c85170>
'06'
<built-in method append of list object at 0x103c85170>
'06'
<built-in method append of list object at 0x103c85170>
'06'
<built-in method append of list object at 0x103c85170>
'06'
<built-in method append of list object at 0x103c85170>
'06'
<built-in method append of list object at 0x103c85170>
'06'
<built-in method append of list object at 0x103c85170>
'06'
<built-in method append of list object at 0x103c85170>
'06'
<built-in method append of list object at 0x103c85170>
'06'
<built-in method append of list object at 0x103c85170>
'06'
<built-in method append of list object at 0x103c85170>
'06'
<built-in method append of list object at 0x103c85170>
'06'
<built-in method append of list object at 0x103c85170>
'06'
<built-in method append of list object at 0x103c85170>
'06'
<built-in method append of list object at 0x103c85170>
'06'
<built-in method append of list object at 0x103c85170>
'06'
<built-in method append of list object at 0x103c85170>
'06'
<built-in method append of list object at 0x103c85170>
'06'
<built-in method append of list object at 0x103c85170>
'06'
<built-in method append of list object at 0x103c85170>
'06'
<built-in method append of list object at 0x103c85170>
'06'
<built-in method append of list object at 0x103c85170>
'06'
<built-in method append of list object at 0x103c85170>
'06'
<built-in method append of list object at 0x103c85170>
'06'
<built-in method append of list object at 0x103c85170>
'06'
<built-in method append of list object at 0x103c85170>
'06'
<built-in method append of list object at 0x103c85170>

然后在我执行print months之后我只得到[],这意味着结果有问题。

我还需要计算月份列表中的元素,所以我想了解如何解决这个问题。

1 个答案:

答案 0 :(得分:0)

希望现在有效

for i in links_2016:
    parsed = urlparse.urlparse(i)
    path = parsed[2] 
    pathlist = path.split("/")
    list_pathlist.append(pathlist)

#get the months
months = []
for i in range(0,len(list_pathlist)): #there are 44 elements in list_pathlist 
    months.append(list_pathlist[i][2]) #you are actually getting the third item
                                       #since list indice start at 0, if you want the second item do [1]
print(months)

计算月份列表中的元素len(months)