python中用字典写出树形数据结构并在控制台中打印树形数据结构
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python中用字典写出树形数据结构并在控制台中打印树形数据结构相关的知识,希望对你有一定的参考价值。
#!/usr/bin/python3# -*- coding: utf-8 -*-
def print_tree(tree):
buff = ['ROOT/']
_print_tree(tree, buff, '', 0)
print('\\n'.join(buff))
def _print_tree(tree, buff, prefix, level):
count = len(tree)
for k, v in tree.items():
count -= 1
if v:
buff.append('%s +- %s/' % (prefix, k))
if count > 0:
_print_tree(v, buff, prefix + ' | ', level + 1)
else:
_print_tree(v, buff, prefix + ' ', level + 1)
else:
buff.append('%s +- %s' % (prefix, k))
def test():
tree =
'bin': 'bash': None, 'cat': None, 'cp': None, ,
'etc':
'init.d': 'apache2':None, 'slapd':None, 'sshd':None, ,
'passwd': None,
'hosts': None,
,
'var':
'log':
'apache2': 'accesslog':None, 'errorlog': None, ,
,
,
print_tree(tree)
if __name__ == '__main__':
test()
输出结果:
ROOT/+- etc/
| +- passwd
| +- init.d/
| | +- apache2
| | +- sshd
| | +- slapd
| +- hosts
+- bin/
| +- cp
| +- bash
| +- cat
+- var/
+- log/
+- apache2/
+- errorlog
+- accesslog 参考技术A #!/usr/bin/python
import os, sys, string
class XXTree:
def __init__(self):
pass
def printHelp(self, cmd):
print 'Please use the following cmd:'
print ' ' + cmd + ' dir'
print 'e.g.'
print ' ' + cmd + ' /home/fkong/tmp'
def getTree(self, dir, op):
list = self.getList(dir, 0, op)
treelist = []
for i in range(0, len(list)):
fullpath = list[i]
parpath = os.path.dirname(list[i])
filename = os.path.basename(list[i])
if(fullpath == dir):
treelist.append(fullpath)
continue
path = fullpath.replace(dir, "")
names = path.split("/")
name = "`---" + names[len(names) - 1]
for j in range(1, len(names) - 1):
name = " " + name
treelist.append(name)
pos = name.index("`")
j = i - 1
while j > 0:
name = treelist[j]
if(name[pos] == '`' or name[pos] == ' '):
name = name[0: pos] + "|" + name[pos + 1: len(name)]
treelist[j] = name
else:
break
j = j - 1
for i in range(0, len(treelist)):
print treelist[i]
def getList(self, dir, layer, op):
list = []
if layer == 0: list.append(dir)
files = os.listdir(dir)
for file in files:
file = os.path.join(dir, file)
if os.path.isdir(file):
list.append(file)
list += self.getList(file, layer + 1, op)
elif op == '-d':
pass
else:
list.append(file)
return list
if len(sys.argv) < 2 :
t = XXTree()
t.printHelp(sys.argv[0])
else:
t = XXTree()
dir = None
if len(sys.argv) == 2:
dir = sys.argv[1]
op = None
if len(sys.argv) == 3:
op = sys.argv[1]
dir = sys.argv[2]
t.getTree(dir, op)
在python中用字典翻译数据框[重复]
【中文标题】在python中用字典翻译数据框[重复]【英文标题】:translate dataframe with dictionary in python [duplicate] 【发布时间】:2018-06-08 22:59:52 【问题描述】:有以下pandas Dataframe
示例:
df = pd.DataFrame([[1,2],[1,2],[3,5]])
df
0 1
0 1 2
1 1 2
2 3 5
还有以下字典:
d = 1:'foo',2:'bar',3:'tar',4:'tartar',5:'foofoo'
我想使用字典 d
来“翻译”数据框。 输出如下:
result = pd.DataFrame([['foo','bar'],['foo','bar'],['tar','fofo']])
result
0 1
0 foo bar
1 foo bar
2 tar fofo
我想避免使用 for 循环。我试图找到的解决方案是地图或类似的东西......
【问题讨论】:
【参考方案1】:解决方案
替换整个数据框:
result_1 = df.replace(d)
替换数据框的特定列:
result_2 = df.replace("COLUMN":d)
【讨论】:
只翻译一栏怎么样? df.replace("COLUMN":d) :)以上是关于python中用字典写出树形数据结构并在控制台中打印树形数据结构的主要内容,如果未能解决你的问题,请参考以下文章