在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/Python/ python下網(wǎng)絡中接收到列表,將字節(jié)循環(huán)轉(zhuǎn)換成字符串,卻報KeyError:1

python下網(wǎng)絡中接收到列表,將字節(jié)循環(huán)轉(zhuǎn)換成字符串,卻報KeyError:1錯誤?

import networkx as net
from urllib import request
def read_friends(g,name):

response = request.urlopen('https://www.livejournal.com/misc/fdata.bml?user='+name)
for line in response.readlines():
    parts = line.split()
    if len(parts) == 0:
        continue
    if parts[0] =='<':
        g.add_edge(parts[1],name)
    else:
        g.add_edge(name,parts[1])

def snowball_sampling(g,center,max_depth = 1,current_depth = 0,taboo_list=[]):

print(center,current_depth,max_depth,taboo_list)
if current_depth == max_depth:
    print('out of depth')
    return taboo_list
if center in taboo_list:
    print('taboo')
    return taboo_list
else:
    taboo_list.append(center)
    read_friends(g,center)
    for node in g.neighbors(center):
        taboo_list = snowball_sampling(g,node,current_depth=current_depth+1,max_depth = max_depth,taboo_list=taboo_list)

def trim_degrees(g,degree = 1):

g2 = g.copy()
d = net.degree(g2)
print(g2.nodes())
L = g2.nodes()
for x in range(len(L)):
    if x == 0:
        continue
    else:
        L[x] = L[x].decode('utf-8')
print(L)
'''
for n in L:
    if d[n] <= degree:
        g2.remove_node(n)
return g2
'''

g = net.Graph()
snowball_sampling(g,'kozel_na_sakse')
core = trim_degrees(g)
print(len(core))

L[x] = L[x].decode('utf-8')這一行報錯,錯誤原因KeyError:1
單獨取出接收到字節(jié)使用循環(huán)轉(zhuǎn)換得到字符串是沒問題的,但是在這就報錯

回答
編輯回答
詆毀你

L中不存在鍵==1的項目, g2.nodes()內(nèi)容是啥?

2017年2月13日 00:16
編輯回答
撥弦

請問是怎么解決的,我也遇到了這個問題。

2017年3月15日 14:28