Python切片字符串或添加到列表列表中的字符串

时间:2017-04-04 18:02:55

标签: python string list

我正在尝试根据元素的长度更改列表列表中的元素,如果字符串长度大于8我想要从开始到长度8切断字符串,如果字符串短于8我想在白色空间中添加更多更短的东西。这段代码出错了什么?

list_of_lists = [['PARENT', 'ID', 'DESCRIPTION1', 'DESCRIPTION2'],
             ['', 'R1', 'R1_Des_1', '0'],
             ['C1', 'E1', 'E1_Des_1', '1'],
             ['P1', 'C1', 'C1_Des_1', '2'],
             ['R1', 'P1', 'P1_Des_1', '3'],
             ['C2', 'E2', 'E2_Des_1', '4'],
             ['P2', 'C2', 'C2_Des_1', '5'],
             ['R1', 'P2', 'P2_Des_1', '6'],
             ['P2', 'C3', 'C3_Des_1', '7'],
             ['C4', 'E3', 'E3_Des_1', '8'],
             ['P3', 'C4', 'C4_Des_1', '9'],
             ['R2', 'P3', 'P3_Des_1', '10'],
             ['None', 'R3', 'R3_Des_', '11'],
             ['P3', 'C5', 'C5_Des_1', '12'],
             ['C6', 'E4', 'E4_Des_1', '13'],
             ['P4', 'C6', 'C6_Des_1', '14'],
             ['R2', 'P4', 'P4_Des_1', '15'],
             ['C7', 'E5', 'E5_Des_1', '16'],
             ['P5', 'C7', 'C7_Des_1', '17'],
             ['R3', 'P5', 'P5_Des_1', '18'],
             ['C9', 'E6', 'E6_Des_1', '19'],
             ['P6', 'C9', 'C9_Des_1', '20'],
             ['R3', 'P6', 'P6_Des_1', '21'],
             ['P6', 'C8', 'C8_Des_1', '22'],
             ['C10', 'E7', 'E7_Des_1', '23'],
             ['P7', 'C10', 'C10_Des_1', '24'],
             ['R4', 'P7', 'P7_Des_1', '25'],
             ['P7', 'C11', 'C11_Des_1', '26'],
             ['C12', 'E8', 'E8_Des_1', '27'],
             ['P8', 'C12', 'C12_Des_1', '28'],
             ['R4', 'P8','','29'],
             ['', 'P8','','30'],
             ['R1', 'P8','','31'],
             ['', 'z1','','32'],
             ['z1', 'z2','','33'],
             ['z1', 'z3','','34'],
             ['z2', 'z4','','35'],
             ['z3', 'z4','','36']]

list_of_lists_slice = list(list_of_lists)
list_string = []

for sublist in list_of_lists_slice:
    for element in sublist:
        if len(element) > 8:
            element = element[:7]
        if len(element) < 8:
            space = 8 - len(element)
            element = element + (' ' * space)


for sublist in list_of_lists_slice:
    row_str = str(' |')
    ele_str = '|'.join(sublist)
    row_str = row_str + ele_str + ('| ')
    list_string.append(row_str)
    middle = ('  ')
    middle = middle + str((len(sublist)) * (9 * ('-')))
    list_string.append(middle)

for row in list_string:
    print (row)

4 个答案:

答案 0 :(得分:2)

我不知道您的代码有什么问题,但您绝对应该使用字符串格式,结合格式语言的截断 padding 选项: / p>

>>> s1 = 'longer than 8'
>>> s2 = 'short'
>>> f'{s1:8.8}'
'longer t'
>>> f'{s2:8.8}'
'short   '

如果您为这些f字符串获得SyntaxError,请使用旧的学校方式:

>>> '{:8.8}'.format(s1)
'longer t'
>>> '{:8.8}'.format(s2)
'short   '

答案 1 :(得分:1)

实际上,您只需要两种方法:字符串切片和ljust。使用列表理解,你可以这样做:

>>> new_list = [[s[:8].ljust(8, ' ') for s in sublist] for sublist in list_of_lists]

现在,让我们使用pprint

打印出来
>>> from pprint import pprint
>>> pprint(new_list)
[['PARENT  ', 'ID      ', 'DESCRIPT', 'DESCRIPT'],
 ['        ', 'R1      ', 'R1_Des_1', '0       '],
 ['C1      ', 'E1      ', 'E1_Des_1', '1       '],
 ['P1      ', 'C1      ', 'C1_Des_1', '2       '],
 ['R1      ', 'P1      ', 'P1_Des_1', '3       '],
 ['C2      ', 'E2      ', 'E2_Des_1', '4       '],
 ['P2      ', 'C2      ', 'C2_Des_1', '5       '],
 ['R1      ', 'P2      ', 'P2_Des_1', '6       '],
 ['P2      ', 'C3      ', 'C3_Des_1', '7       '],
 ['C4      ', 'E3      ', 'E3_Des_1', '8       '],
 ['P3      ', 'C4      ', 'C4_Des_1', '9       '],
 ['R2      ', 'P3      ', 'P3_Des_1', '10      '],
 ['None    ', 'R3      ', 'R3_Des_ ', '11      '],
 ['P3      ', 'C5      ', 'C5_Des_1', '12      '],
 ['C6      ', 'E4      ', 'E4_Des_1', '13      '],
 ['P4      ', 'C6      ', 'C6_Des_1', '14      '],
 ['R2      ', 'P4      ', 'P4_Des_1', '15      '],
 ['C7      ', 'E5      ', 'E5_Des_1', '16      '],
 ['P5      ', 'C7      ', 'C7_Des_1', '17      '],
 ['R3      ', 'P5      ', 'P5_Des_1', '18      '],
 ['C9      ', 'E6      ', 'E6_Des_1', '19      '],
 ['P6      ', 'C9      ', 'C9_Des_1', '20      '],
 ['R3      ', 'P6      ', 'P6_Des_1', '21      '],
 ['P6      ', 'C8      ', 'C8_Des_1', '22      '],
 ['C10     ', 'E7      ', 'E7_Des_1', '23      '],
 ['P7      ', 'C10     ', 'C10_Des_', '24      '],
 ['R4      ', 'P7      ', 'P7_Des_1', '25      '],
 ['P7      ', 'C11     ', 'C11_Des_', '26      '],
 ['C12     ', 'E8      ', 'E8_Des_1', '27      '],
 ['P8      ', 'C12     ', 'C12_Des_', '28      '],
 ['R4      ', 'P8      ', '        ', '29      '],
 ['        ', 'P8      ', '        ', '30      '],
 ['R1      ', 'P8      ', '        ', '31      '],
 ['        ', 'z1      ', '        ', '32      '],
 ['z1      ', 'z2      ', '        ', '33      '],
 ['z1      ', 'z3      ', '        ', '34      '],
 ['z2      ', 'z4      ', '        ', '35      '],
 ['z3      ', 'z4      ', '        ', '36      ']]
>>>

您可以将上述嵌套列表理解视为以下内容的简写:

new_list = []
for sublist in list_of_lists:
    sub = []
    for s in sublist:
        sub.append(s[:8].ljust(8, ' '))
    new_list.append(sub)

一旦人们学习了列表理解,他们就会过度使用它们。上面的示例可能是边缘情况,因为它变得不可读可读性计数。嵌套的for循环可能更合适。

你原来的尝试有一些微妙的错误。我把它清理干净并保持原有的精神。我添加了评论,希望能明白出现的问题。

new_list = [] # need to build a new list!
for sublist in list_of_lists:
    sub = [] # need to build new sublists!
    for element in sublist:
        if len(element) > 8:
            sub.append(element[:8]) # slice to length 8
        else: # use else! You were skipping strings with len == 8
            space = 8 - len(element)
            sub.append(element + (' ' * space))
    new_list.append(sub)

答案 2 :(得分:1)

试试这个:

[['{:>8}'.format(x[:8]) for x in row] for row in  list_of_lists]

答案 3 :(得分:1)

您可以使用此列表压缩

[i[:8] if len(i)>8 else i+[' ']*(8-len(i)) for i in list_of_lists]