Python 按相同索引对列表进行排序

Python 按相同索引对列表进行排序,python,list,Python,List,我正在尝试对数组中的数据进行排序。我的python代码提供以下输出: Value List: [['house'], ["'tW'"], ['6.998332153088995']] Insert at array index: 3 [['6.998332153088995']] Value List: [['house'], ["'Total'"], ['91.40121710449374']] Insert at array index: 11 [

我正在尝试对数组中的数据进行排序。我的python代码提供以下输出:

Value List: [['house'], ["'tW'"], ['6.998332153088995']]
Insert at array index: 3
[['6.998332153088995']]


Value List: [['house'], ["'Total'"], ['91.40121710449374']]
Insert at array index: 11
[['91.40121710449374']]


Value List: [['garden'], ["'flower'"], ['0.1525635807425692']]
Insert at array index: 0
[['0.1525635807425692']]


Value List: [['garden'], ["'gras'"], ['0.31921114287979435']]
Insert at array index: 12
[['0.31921114287979435']]
现在,我想将这些值写入.txt文件,但我想按以下方式对它们进行排序:

#region tW     Total    flower    gras
house   6.9983 91.40121
garden                  0.152     0.312
这意味着值列表的第一项保留在开头,并在特定位置填充值。但我不想有两个条目,例如house,house应该只在一行中

但不幸的是,list.insert(位置、值)在我的情况下不起作用。现在,我在这里向您提供我的最小代码,它提供了上述输出:

newarraylist = []
print("Value List: " + str(list4))
print("Insert at array index: " + str(save_values_to_file(list4)))
newarraylist.insert(save_values_to_file(list4), list4[2])
print(newarraylist)

希望有人能帮我解决这个初学者的问题

非常棘手,但这里有一些有用的方法。首先,我将您的数据(因此我的问题)格式化为Python格式:列表列表。这有点麻烦,因为每个数据元素本身就是一个列表,但也可以这样:

lsts=[
[['house'], ["'tW'"], ['6.998332153088995']],
[['house'], ["'Total'"], ['91.40121710449374']],
[['garden'], ["'flower'"], ['0.1525635807425692']],
[['garden'], ["'gras'"], ['0.31921114287979435']]
]
其思想是使用一个标题列表来计算有多少不同的“属性”,并使用一个字典来表示“区域”,用足够的空字符串初始化以适应所有属性

headers = [lst[1][0] for lst in lsts]                           # a list with the headers ['tW', 'Total', 'flower', 'gras']
regions = set([lst[0][0] for lst in lsts])                      # a set of the regions: {'house', 'garden'}
valdict = {region:len(headers) * [''] for region in regions}    # a dict: {region:['', '', '', ''], } where the list has the same number of items as the header list

for lst in lsts:
    valdict[lst[0][0]][headers.index(lst[1][0])] = lst[2][0]    # in the dict, change one of the empty list items
“输出”部分首先打印变量,然后将所有内容作为表格打印:

# RAW OUTPUT
print(headers)
for region, values in valdict.items():
    print(region, values)

#PRETTY PRINT (a bit clumsy, but it shows a nice table format
print('\nPretty print\n')

print('{:10}'.format(''), end='')
for header in headers:
    print('{:10}'.format(header), end='')
for region in regions:
    print('\n{:10}'.format(region), end='')
    for val in valdict[region]:
        if val == '':
            print('{:<10}'.format(val), end='')                 # print empty strings in list
        else:
            print('{:<10.4f}'.format(float(val)), end='')       # format print strings as floats
唯一的问题是,因为我使用了dict,
花园
房子
的顺序是不确定的。当然,您可以对dict进行排序,也可以使用
sortedct
进一步塑造数据


附言:我对更优雅的解决方案持开放态度;-)

程序如何“读取”您的列表?您是从文件中读取它们,还是将它们存储在代码中的某个位置?我很想知道它们到底是从哪里来的?列表存储在程序中。不幸的是,我不能提供更多的代码。当清单5是例如
[['house'],[“'tW'”,'12.342514131'].
时会发生什么?顺便说一句,我不是要更多的代码,而是要你的程序使用的格式的输入数据。啊,现在我明白你的意思了。房屋和其他区域不可能出现重复元素。它只能是像花一样缺少一些值。但是我想填写关于我得到的职位的值。
["'tW'", "'Total'", "'flower'", "'gras'"]
garden ['', '', '0.1525635807425692', '0.31921114287979435']
house ['6.998332153088995', '91.40121710449374', '', '']

Pretty print

          'tW'      'Total'   'flower'  'gras'
garden                        0.1526    0.3192
house     6.9983    91.4012