【问题标题】:How to remove some extra tuples and lists and print some extra stuff between the strings in python?如何删除一些额外的元组和列表并在python中的字符串之间打印一些额外的东西?
【发布时间】:2022-01-09 03:27:21
【问题描述】:
 bounds = reader.readtext(np.array(images[0]), min_size=0,slope_ths=0.2, ycenter_ths=0.7, height_ths=0.6,width_ths=0.8,decoder='beamsearch',beamWidth=10) 
 print(bounds)

上述代码的输出是

我确实有类似的字符串格式

[([[1004, 128], [1209, 128], [1209, 200], [1004, 200]],
  'EC~L',
  0.18826377391815186),
 ([[177, 179], [349, 179], [349, 241], [177, 241]], 'OKI', 0.9966741294455473),
 ([[180, 236], [422, 236], [422, 268], [180, 268]],
  'Oki Eleclric Industry Co',
  0.8091106257361781),
 ([[435, 243], [469, 243], [469, 263], [435, 263]], 'Ltd', 0.9978489622393302),
 ([[180, 265], [668, 265], [668, 293], [180, 293]],
  '4-11-22 , Shibaura, Minalo-ku, Tokyo 108-855| Japan',
  0.6109240973537998),
 ([[180, 291], [380, 291], [380, 318], [180, 318]],
  'Tel +81-3-5440-4884',
  0.9406430290171053)]

如何编写一个 python 代码,打印类似于以下格式的上述格式:

[1004, 128, 1209, 128, 1209, 200, 1004, 200], 
'EC~L'
##################
[177, 179, 349, 179, 349, 241, 177, 241],
'OKI'
##################
[180, 236, 422, 236, 422, 268, 180, 268],
'Oki Eleclric Industry Co'
##################
[435, 243, 469, 243, 469, 263, 435, 263], 
'Ltd'
##################
[180, 265, 668, 265, 668, 293, 180, 293],
'4-11-22 , Shibaura, Minalo-ku, Tokyo 108-855| Japan'
##################
[180, 291, 380, 291, 380, 318, 180, 318],
'Tel +81-3-5440-4884'

谁能帮我解决这个问题?

【问题讨论】:

    标签: python python-3.x string list tuples


    【解决方案1】:

    如果我理解正确:

    for l, s, _ in bounds:
        print([lll for ll in l for lll in ll])
        print(s)
        print('##################')
    
    

    输出:

    [1004, 128, 1209, 128, 1209, 200, 1004, 200]
    EC~L
    ##################
    [177, 179, 349, 179, 349, 241, 177, 241]
    OKI
    ##################
    [180, 236, 422, 236, 422, 268, 180, 268]
    Oki Eleclric Industry Co
    ##################
    [435, 243, 469, 243, 469, 263, 435, 263]
    Ltd
    ##################
    [180, 265, 668, 265, 668, 293, 180, 293]
    4-11-22 , Shibaura, Minalo-ku, Tokyo 108-855| Japan
    ##################
    [180, 291, 380, 291, 380, 318, 180, 318]
    Tel +81-3-5440-4884
    ##################
    

    【讨论】:

      【解决方案2】:

      我想我会为这个单行解决方案做出贡献。

      print("\n##################\n".join(("{},\n'{}'".format([x for item in items[0] for x in item], items[1])) for items in bounds))
      

      产生的格式与提问者的愿望完全相同:

      [1004, 128, 1209, 128, 1209, 200, 1004, 200],
      'EC~L'
      ##################
      [177, 179, 349, 179, 349, 241, 177, 241],
      'OKI'
      ##################
      [180, 236, 422, 236, 422, 268, 180, 268],
      'Oki Eleclric Industry Co'
      ##################
      [435, 243, 469, 243, 469, 263, 435, 263],
      'Ltd'
      ##################
      [180, 265, 668, 265, 668, 293, 180, 293],
      '4-11-22 , Shibaura, Minalo-ku, Tokyo 108-855| Japan'
      ##################
      [180, 291, 380, 291, 380, 318, 180, 318],
      'Tel +81-3-5440-4884'
      

      【讨论】:

        【解决方案3】:

        另一种解决方案

        from itertools import chain
        
        for numbers, description, _ in bounds:
            numbers = list(chain(*numbers))
            print(f"{numbers},\n"
                  f"'{description}'\n"
                  "##################")
        

        输出:

        [1004, 128, 1209, 128, 1209, 200, 1004, 200],
        'EC~L'
        ##################
        [177, 179, 349, 179, 349, 241, 177, 241],
        'OKI'
        ##################
        [180, 236, 422, 236, 422, 268, 180, 268],
        'Oki Eleclric Industry Co'
        ##################
        [435, 243, 469, 243, 469, 263, 435, 263],
        'Ltd'
        ##################
        [180, 265, 668, 265, 668, 293, 180, 293],
        '4-11-22 , Shibaura, Minalo-ku, Tokyo 108-855| Japan'
        ##################
        [180, 291, 380, 291, 380, 318, 180, 318],
        'Tel +81-3-5440-4884'
        ##################
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-08-06
          • 2012-04-18
          • 2012-07-26
          • 2010-12-16
          相关资源
          最近更新 更多