【问题标题】:list of string to display in table要在表格中显示的字符串列表
【发布时间】:2021-03-23 10:25:34
【问题描述】:

我有一个数据:

list= """ID, Color, Brand, Price, Outstanding, Type
02, Blue, Audi, 200.12, True, 2
06, Red, BMW, 2357.13, True, 1
13, Black, Ford, 252676.12, False, 5"""

我需要将它显示在表格中(即列表列表),(不使用字典)并将每个字符串转换为原生 Python 数据类型,例如 int、float、str 等。 转换函数应该接受数据集作为输入参数并返回表..

我想我需要为此使用循环,但我是 Python 新手,不知道如何开始。

【问题讨论】:

    标签: python string type-conversion


    【解决方案1】:

    我建议使用熊猫。 Pandas 允许您制作数组、列表、csv 文件、excel 文件等的数据框。

    看看他们的文档:https://pandas.pydata.org/docs/

    还有很多 youtube 视频也解释了这个概念。

    【讨论】:

    • 挑战是我不应该使用任何字典来解决这个问题。
    • 哦,我明白了,我想念理解。你的意思是没有进口?
    • 尝试嵌套列表。列表中的列表。
    • @SylwiaKmiec pandas 没有用字典实现。如果我没记错的话,它是使用 numpy 数组实现的。这是代码挑战还是类似的东西?因为在现实世界的应用程序中,您通常会使用 pandas 进行此类操作。
    • 我很想为此使用 Pandas,但有以下要求:注意:您不得使用 numpy 或 pandas 等第三方库来完成此任务,只需使用基本 Python
    【解决方案2】:

    也许这可以满足要求?

    txt = 'ID, Color, Brand, Price, Outstanding, Type02, Blue, Audi, 200.12, True, 206, Red, BMW, 2357.13, True, 113, Black, Ford, 252676.12, False, 5'
    # Remove the whitespaces first, then split the text by commas
    l_txt = txt.replace(' ', '').split(',')
    
    tb = []
    for l in l_txt:
        tb.append([l])
    

    【讨论】:

      【解决方案3】:

      没有 pandas、dict,只有字符串和列表。

      def conv(cols):                 # only last 3 data requied conversion
          cols[3] = float(cols[3])
          cols[4] = True if (cols[4] == 'True') else False
          cols[5] = int(cols[5])
          return cols
      
      dataset = []                    # create a list to store the data
      lines = list.split('\n')        # split into lines from the text
      for line in lines:              # for each line
          cols = line.split(', ')     # split into column from the line
          # print(cols)                 # display the columns of each line
          if not dataset:             # 1st row is title
              dataset.append(cols)    # just append it
              continue                # skip the conversion for 1st row
          dataRow = conv(cols)        # convert each data string
          dataset.append(dataRow)     # then append it
      

      然后,数据在经过适当数据转换的列表列表中。

      print(dataset)
      [['ID', 'Color', 'Brand', 'Price', 'Outstanding', 'Type'], ['02', 'Blue', 'Audi', 200.12, True, 2], ['06', 'Red', 'BMW', 2357.13, True, 1], ['13', 'Black', 'Ford', 252676.12, False, 5]]
      

      【讨论】:

        【解决方案4】:
        def list_format():    
            
            lt= """ID, Color, Brand, Price, Outstanding, Type
        02, Blue, Audi, 200.12, True, 2
        06, Red, BMW, 2357.13, True, 1
        13, Black, Ford, 252676.12, False, 5"""   
                   
            
            result_list=[ i.split(',') for i in lt.split('\n')]   
            
            
            for i,val in enumerate(result_list):
                      
                for k,v in enumerate(val):
                    
                    result_list[i][k]= result_list[i][k].replace(' ','')
                    
                    if result_list[i][k].isdigit() and k==0:
                        
                        result_list[i][k]=int(result_list[i][k])
                    if k==3 and i!=0:
                        
                        result_list[i][k]=float(result_list[i][k])
                    if k==4 and i!=0:
                        
                        result_list[i][k]=bool(result_list[i][k])
                        
                    
            
            print(result_list)
        
        list_format()
        
        [['ID', 'Color', 'Brand', 'Price', 'Outstanding', 'Type'], [2, 'Blue', 'Audi', 200.12, True, '2'], [6, 'Red', 'BMW', 2357.13, True, '1'], [13, 'Black', 'Ford', 252676.12, True, '5']]
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-11-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多