问题描述
我已经创建了这样的字典:
Cursor cursor2 = getContentResolver().query(ContactsContract.RawContacts.CONTENT_URI,null,ContactsContract.RawContacts.ACCOUNT_TYPE_AND_DATA_SET + "= ?",new String[] { "com.viber.voip" },null);
while (cursor2.moveToNext())
{
String name=cursor2.getString(cursor2.getColumnIndex(ContactsContract.RawContacts.DISPLAY_NAME_PRIMARY));
//Adding contact name into the ArrayList
myViberContacts.add(name+"\n");
//Giving the TextView the value of the arraylist
emervib.setText(myViberContacts.toString());
然后,我遍历所有项目,然后可以递归进行:
d = {1: {3: {},4: {6: {}}},5: {}}
我的目标是以一种可以操纵输出的方式将输出存储到字符串变量中。因此结果应该是这样的:
def pretty(d,indent=0):
for key,value in d.items():
print('\t' * indent + str(key))
if isinstance(value,dict):
pretty(value,indent+1)
else:
print('\t' * (indent+1) + str(value))
pretty(d)
我尝试通过以下实现方式实现自己的目标:
msg ="""
1
3
4
6
5
"""
但是我的尝试输出是:def pretty(d,indent=0,indent_before=0,msg_old=""):
for key,value in d.items():
#print('\t' * indent + str(key) + '({})({})'.format(indent,indent_before))
msg = msg_old+'\t' * indent + str(key) + '({})({})\n'.format(indent,indent_before)
if isinstance(value,indent+1,indent,msg)
else:
print('\t' * (indent+1) + str(value))
return msg
msg = pretty(result)
print(msg)
您能提出一种巧妙而优雅的方法来达到预期效果吗?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)