打印没有括号的列表,没有引号,没有逗号和额外的间距

时间:2015-09-06 05:25:17

标签: python

我有一个属性列表attrlist [],我在整个代码中都附加了字符串。如何在没有括号,引号和逗号的情况下以适当的间距打印此列表?这就是我的意思:

当前代码:

==21841== Invalid read of size 8
==21841==    at 0x400C9B: A::model(std::vector<double, std::allocator<double> >*) (in /home/code/snippets/cpptest/main)
==21841==    by 0x40233D: void std::_Mem_fn<void (A::*)(std::vector<double, std::allocator<double> >*)>::operator()<std::vector<double, std::allocator<double> >*, void>(A*, std::vector<double, std::allocator<double> >*&&) const (in /home/code/snippets/cpptest/main)
==21841==    by 0x4021F0: void std::_Bind<std::_Mem_fn<void (A::*)(std::vector<double, std::allocator<double> >*)> (A*, std::_Placeholder<1>)>::__call<void, std::vector<double, std::allocator<double> >*&&, 0ul, 1ul>(std::tuple<std::vector<double, std::allocator<double> >*&&>&&, std::_Index_tuple<0ul, 1ul>) (in /home/code/snippets/cpptest/main)
==21841==    by 0x401EB0: void std::_Bind<std::_Mem_fn<void (A::*)(std::vector<double, std::allocator<double> >*)> (A*, std::_Placeholder<1>)>::operator()<std::vector<double, std::allocator<double> >*, void>(std::vector<double, std::allocator<double> >*&&) (in /home/code/snippets/cpptest/main)
==21841==    by 0x401B3C: std::_Function_handler<void (std::vector<double, std::allocator<double> >*), std::_Bind<std::_Mem_fn<void (A::*)(std::vector<double, std::allocator<double> >*)> (A*, std::_Placeholder<1>)> >::_M_invoke(std::_Any_data const&, std::vector<double, std::allocator<double> >*) (in /home/code/snippets/cpptest/main)
==21841==    by 0x401152: std::function<void (std::vector<double, std::allocator<double> >*)>::operator()(std::vector<double, std::allocator<double> >*) const (in /home/code/snippets/cpptest/main)
==21841==    by 0x400D3C: B::run() (in /home/code/snippets/cpptest/main)
==21841==    by 0x400E18: main (in /home/code/snippets/cpptest/main)
==21841==  Address 0x5a1c1a0 is 0 bytes after a block of size 80 alloc'd
==21841==    at 0x4C2B0E0: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==21841==    by 0x401FBF: __gnu_cxx::new_allocator<double>::allocate(unsigned long, void const*) (in /home/code/snippets/cpptest/main)
==21841==    by 0x401D8E: std::_Vector_base<double, std::allocator<double> >::_M_allocate(unsigned long) (in /home/code/snippets/cpptest/main)
==21841==    by 0x40199E: std::_Vector_base<double, std::allocator<double> >::_M_create_storage(unsigned long) (in /home/code/snippets/cpptest/main)
==21841==    by 0x401306: std::_Vector_base<double, std::allocator<double> >::_Vector_base(unsigned long, std::allocator<double> const&) (in /home/code/snippets/cpptest/main)
==21841==    by 0x40107D: std::vector<double, std::allocator<double> >::vector(unsigned long, double const&, std::allocator<double> const&) (in /home/code/snippets/cpptest/main)
==21841==    by 0x400D1D: B::run() (in /home/code/snippets/cpptest/main)
==21841==    by 0x400E18: main (in /home/code/snippets/cpptest/main)
==21841== 
0
==21841== 
==21841== HEAP SUMMARY:
==21841==     in use at exit: 0 bytes in 0 blocks
==21841==   total heap usage: 4 allocs, 4 frees, 137 bytes allocated
==21841== 
==21841== All heap blocks were freed -- no leaks are possible

我的输出:

print('[%s]' % ', '.join(map(str, attrlist)))

我想要输出程序:

[1, bomb, C4, red, evil, yup]

&#39; 1&#39;的开始到了炸弹之前的空间#39是4个空格 爆炸的开始&#39;一直到C4&#39; C4之前的最后一个空间是16个空格。 C4&#39;的开始一直到最后一个空格之前的红色&#39;是10个空格。 红色&#39;的开始到了邪恶之前的空间是11个空格。

阵列会改变,所以&#39; 1&#39;可能是&#39; 222&#39;但仍占用总共4个空格。炸弹,C4,红色(又名,地点,颜色)也是如此。然而,在那之后,关键字不必间隔,只是列在其间的空格。

非常感谢您的帮助。非常感谢。顺便说一句,我是为一个安装在树莓派上的工具组织系统做这个,运行python 2.7.10。

2 个答案:

答案 0 :(得分:3)

支架和逗号很容易摆脱,只需在代码中删除它们。

print(' '.join(map(str, attrlist)))

但是既然你想要自定义间距,你应该看看str.format。它使用特殊的format string syntax

attrs = [1, 'bomb', 'C4', 'red', 'evil', 'yup']
print('{0:<4}{1:<16}{2:<10}{3:<11}{rest}'.format(*attrs[:4], rest=' '.join(attrs[4:])))
# Note in earlier versions of py2 you have to refer to format arguments by index or key, {} won't work

答案 1 :(得分:1)

这绝不是有效的,但这是一个解决方案:

attrlist = [1, 'bomb', 'C4', 'red', 'evil', 'yup', 'puppy'] #this list needed a puppy
lengthlist = [4, 16, 10, 11]

ans = ''
for attr, length in zip(attrlist, lengthlist):
    ans += format(attr, '<{}'.format(length))

ans += ' '.join(map(str, attrlist[len(lengthlist):]))

print(ans)

您可以在docs

中查看字符串格式规则
相关问题