我有一个名为df_following的列表,它看起来像这样:
[('henrysimanjun13', following
henrysimanjun13 [AndiDar59797467]), ('hamdalah47', Empty DataFrame
Columns: []
Index: []), ('vivoslot', following
vivoslot [AprillParis, liputan6dotcom, detikcom]), ('KacangTopless',
following
KacangTopless [mohmahfudmd, CallMeNayr, siti_dama]), ('infosantuy1',
following
infosantuy1 [OneLouderApps, DakwahJateng, setkabgoid, okez...)]
print ("df_following[0]: ", df_following[0][0])
--> df following[0]: buin83
print ("df_following[0]: ", df_following[0][1]['following'][0][0])
--> df following[0]: AkuDutaDamai
从上面的列表中,我想做出这样的东西:
{buin83, AkuDutaDamai},{buin83, FarlinaYaz}, {buin83, Mom_isback}, {buin83, yosiago}, {killjoyyadi, susipudjiastuti}, {killjoyyadi, PetrusBun},..
我试着:
d = {}
for l in df_following:
username = l[0][0]
following = l[0][1]['following'][0][0]
if username in d:
d[username][following] = [following]
else:
d[username] = {username,following}
但我得到以下错误:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-135-437589d2001c> in <module>
3 for l in df_following:
4 username = l[0][0]
----> 5 following = l[0][1]['following'][0][0]
6
7 if username in d:
TypeError: string indices must be integers
我不知道我的代码出了什么问题。任何建议吗? 问题来源StackOverflow 地址:/questions/59386757/typeerror-string-indices-must-be-integers-lists-and-dictionary-in-python
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。