修改非理想列表格式并将其导出到Excel(Python) [英] Modifying a non-ideal list format and exporting it to Excel (Python)

查看:49
本文介绍了修改非理想列表格式并将其导出到Excel(Python)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python的新手,并不是要侮辱任何人的智慧.

VERY new to Python, don't mean to insult anyones intelligence.

我的清单很大,其中包含一系列ID和相关值,其版本非常短:

I have a large list, contains a series of ids and related values, extremely shortened version looks like so:

large = [('550111', [(4, 5, 8), (6, -4, -6)]), ('222211', [(2, -4, 5), (1, 15, -4)])]

我有类似的代码,可以将理想的格式导出到Excel,但是没有进行所需的修改:

I have code like so which exports an ideal format to Excel, however without the desired modifications:

import csv
with open("out1.csv", "wb") as fp: 
 writer = csv.writer(fp, delimiter=",")     
for entry in large:        
 writer.writerow([entry[0]])         
for line in entry[1]:             
 writer.writerow(line)        
 writer.writerow([])

从此输出到Excel (每个值都有其自己的单独的单元格-5501在A1,A2中的4,B2中的5,C2中的8,第一个空格在A4中,依此类推)看起来像:

Output from this to Excel (every value with its own separate cell - 5501 being in A1, 4 in A2, 5 in B2, 8 in C2... the first space would be in A4 etc.) looks like:

550111 
4,5,8 
6,-4,-6  
<space here - dont know how to put them in>
222211 
2,4,-5 
1,-15, 4 
 <space here - dont know how to put them in>

这是完美的,但是我也希望产生这些值的绝对最大值.这是我需要帮助的地方.我不知道如何将绝对最大值函数写入导出代码,因为不断收到错误消息,说我无法将函数应用于列表.

Which is perfect, however I also wish to produce the absolute max of the values. This is where I need help. I do not know how to write the absolute max function into exporting code as I keep getting an error saying I cannot apply a function to a list.

我还需要在最后插入一个介于1到8904之间的数字序列.

Also I need to insert a numerical sequence ranging from 1 to 8904 at the very end.

导出到Excel后所需的表单-具有绝对最大值,即忽略'-'符号排序的绝对最大值

Desired form after being exported to Excel - has absolute max .i.e neglectinf '-' signs and a sorted absolute max

550111 
4,5,8 
6,-4,-6
'space here'
'Max:'
6, 5, 8
'Sorted max'
8, 6, 5
'space here'     
222211 
2,4,-5 
1,-15,4
'space here'
'Max:'
2, 15, 5
'Sorted max:' 
15, 5, 2
'space here'
1, 2, 3, ........, 8904 

很抱歉,问题是否令人讨厌/新手/琐碎.非常感谢您的帮助.

Sorry if question is annoying/rookie/trivial. Help really appreciated.

推荐答案

我认为以下代码片段可以帮助您获取最大值和排序的最大值.抱歉,尽管如此,它们仍无法帮助您

I think the following code fragment can help you in getting the max and sorted max values. Sorry can't help you in putting them in excel though

for entry in large:
    max_list=[]
    for z in zip(*entry[1]): 
        max_list.append(max(z))
    print max_list
    max_list.sort(reverse=True)
    print max_list

[6, 5, 8]
[8, 6, 5]
[2, 15, 5]
[15, 5, 2]

基本上,对于大型数据集中的每个条目,您都必须处理条目 1 list(这是一个元组列表).使用zip可以同时压缩"多个数据结构.

Baically for every entry in large dataset, you have to process entry1 list (which is a list of tuples). Using zip you can 'zip' through multiple data-structures simultaneously.

zip(*entry[1])

此行基本上展开列表中的所有元组,并同时遍历它们.因此,在第一次迭代期间,将选择所有元组的所有第一元素并将其存储为元组.

This line basically unrolls all the tuples in your list and loops through them concurrently. Thus during first iteration, all the first elements of all the tuples are selected and stored as a tuple.

使用max函数,可以获得最大值并将其存储在单独的列表中(每个条目都有一个新列表).然后您可以对列表进行反向排序以获得所需的内容

Using max function, you can get the maximum value and store it in a separate list (A new list for each entry). Then you can sort that list in reverse to get what you want

编辑

仅注意到您需要绝对最大值.所以代码片段被修改为

Just noticed you need the absolute max. So the code fragment gets modified as

for entry in large: 
    max_list=[]
    for z in zip(*entry[1]): 
        max_list.append(max(abs(t) for t in z))
    print "Absolute max",max_list
    max_list.sort(reverse=True)
    print "Sorted Absolute Max",max_list

输入

large = [('5501', [(4, -5, 8), (6, -4, -6)]), ('2222', [(2, -4, 5), (1, -15, -4)])]

输出

Absolute max [6, 5, 8]
Sorted Absolute Max [8, 6, 5]
Absolute max [2, 15, 5]
Sorted Absolute Max [15, 5, 2]

EDIT2

由于您询问了邮政编码和编辑后的代码,因此我将尽力解释这一点.

Since you asked about zip and editted code, I'll try to explain this.

zip()是内置的python函数,可帮助您并行地迭代多个可迭代对象(列表,元组和您可以迭代的任何其他对象).

zip() is an in-built python function which helps you in iterating over multiple iterables (lists,tuples and any other thing you can iterate over) parallely.

>>> a=[1,2,3,]
>>> b=[4,5,6,]
>>> zip(a,b)
[(1, 4), (2, 5), (3, 6)]

结果的第i个元组是输入列表中第i个元素的集合

The i-th tuple of the result is a collection of i-th elements of the input lists

在您的情况下,entry[1]是您要压缩"的元组列表,但是zip仅在我们给它多个列表而不是单个列表的情况下才有效!

In your case entry[1] is the list of tuples you want to 'zip' over, but zip only works if we give it multiple lists not a single list of lists!

这是 *运算符派上用场的地方.它将把您的列表列表扩展为多个列表.

This is where * operator comes in handy. It will splat you list of lists into multiple lists.

>>> def printme(*a): 
...     for i in a: 
...         print i
... 
>>> m=[(1,2,),(3,4,),(5,6,),]
>>> printme(m)
[(1, 2), (3, 4), (5, 6)]
>>> printme(*m)
(1, 2)
(3, 4)
(5, 6)

如果要在列表中找到最大值,则可以使用内置的

And if you want to find the maximum value in a list, you can use the in-built max() function.

现在,我们拥有解决您的问题所需的所有理论要素.可以说

Now we have all the theoretical elements needed to solve your problem. Lets say

>>> entry=('5501', [(4, -5, 8), (6, -4, -6)])
>>> entry[1]
[(4, -5, 8), (6, -4, -6)]

您想要做的是在这些元组上压缩,以便所有第一个元素形成一个单独的元组,所有第二个元素形成一个单独的元组,依此类推.

What you want to do is zip over these tuples so that all the first elements form a separate tuple, all the second elements form a separate tuple and so on.

>>> zip(entry[1])
[((4, -5, 8),), ((6, -4, -6),)]

不好!!!让我们使用*

No Good!!! Lets use the *

>>> zip(*entry[1])
[(4, 6), (-5, -4), (8, -6)]

完美!现在,对于列表中的每个元组,我们要做的就是做一个max()来找出最大值并将其存储在某个地方.

Perfect! Now for each tuple in the list, all we need to do is do a max() to find out the maximum value and store it somewhere.

>>> for z in zip(*entry[1]): 
...     print "I have",z
...     print "max is",max(z)
... 
I have (4, 6)
max is 6
I have (-5, -4)
max is -4
I have (8, -6)
max is 8

因此,如果我们初始化一个空列表并将最大值附加到该列表中,则会得到最大值列表!因此,我编写的第一个代码

So if we initialize an empty list and append the maximum values to it, we get the maximum value list! Hence the first code that I wrote

max_list=[]
for z in zip(*entry[1]): 
    max_list.append(max(z))
print max_list

给我们

[6, -4, 8]

要对max_list本身进行排序,我们使用列表的排序方法

To sort the max_list itself, we use sort method of the lists

max_list.sort(reverse=True)
print max_list

给我们

[8, 6, -4]

但是我们需要这些值的绝对最大值.

But we need the absolute max of the values.

在上面的代码片段z中,保存值.因此,我们需要一种将这些值转换为绝对值的方法.内置的 abs()仅适用于数字,因此我们需要将其应用于z的每个元素

In the above code fragment, z, holds the values. So we need a way to convert those values into their absolute. The in-built abs(), only works for a number so we need to apply it to each element of z

>>> for z in zip(*entry[1]): 
...     print z
...     new_z = []
...     for i in z: 
...         new_z.append(abs(i))
...     print new_z
... 
(4, 6)
[4, 6]
(-5, -4)
[5, 4]
(8, -6)
[8, 6]

或者我们可以使用pythonic并将其缩短为

Or we can be pythonic and shorten it to

>>> for z in zip(*entry[1]): 
...     print z
...     new_z=[abs(i) for i in z]
...     print new_z
... 
(4, 6)
[4, 6]
(-5, -4)
[5, 4]
(8, -6)
[8, 6]

但是我们还需要new_z的最大值.这很容易max(new_z).现在我们的代码是

But we also need the maximum value of new_z. That is easy max(new_z). Our code now is

max_list=[]
for z in zip(*entry[1]): 
    new_z=[abs(i) for i in z]
    max_list.append(max(new_z))       
print max_list

哪个给了我们

[6, 5, 8]

这就是我们想要的.但是,等等,我们仍然可以缩短代码

This is what we want. But wait, we can still shorten the code

new_z=[abs(i) for i in z]
max_list.append(max(new_z)) 

可以转换为单行

max_list.append(max( abs(t) for t in z ))

这将我们带到了最终代码

Which brings us to the final code

for entry in large: 
    max_list=[]
    for z in zip(*entry[1]): 
        max_list.append(max(abs(t) for t in z))
    print "Absolute max",max_list
    max_list.sort(reverse=True)
    print "Sorted Absolute Max",max_list

希望这会有所帮助

这篇关于修改非理想列表格式并将其导出到Excel(Python)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆