English 中文(简体)
随共同日期任意包装
原标题:Arbitrary Amount of Tuple Unpacking With Common Date

<><>Input

datas2 = [[("01/01/2011", 1), ("02/02/2011", "No"), ("03/03/2011", 11)],
[("01/01/2011", 2), ("03/03/2011", 22), ("22/22/2222", "no")],
[("01/01/2011", 3), ("03/03/2011", 33), ("22/22/2222", "333")]]

www.un.org/Depts/DGACM/index_spanish.htm 预期产出

[("01/01/2011", 1, 2, 3), ("03/03/2011", 11, 22, 33)]

<<>>>

我被问到的是真实的数据和更多的实例(历史中的mes):

A                       B                       C
09.05.2011;1.561        12.04.2011;14.59        12.04.2011;1.5
10.05.2011;1.572        13.04.2011;14.50        13.04.2011;1.5    
11.05.2011;1.603        14.04.2011;14.56        14.04.2011;1.5    
12.05.2011;1.566        15.04.2011;14.54        15.04.2011;1.5    
13.05.2011;1.563        18.04.2011;14.54        18.04.2011;1.5    
16.05.2011;1.537        19.04.2011;14.52        19.04.2011;1.5    
17.05.2011;1.528        20.04.2011;14.53        20.04.2011;1.5    
18.05.2011;1.543        21.04.2011;14.59        21.04.2011;1.5    
19.05.2011;1.537        26.04.2011;14.65        26.04.2011;1.6    
20.05.2011;1.502        27.04.2011;14.68        27.04.2011;1.6    
23.05.2011;1.503        28.04.2011;14.66        28.04.2011;1.6    
24.05.2011;1.483        29.04.2011;14.62        29.04.2011;1.6    
25.05.2011;1.457        02.05.2011;14.65        02.05.2011;1.6    
26.05.2011;1.491        03.05.2011;14.63        03.05.2011;1.6    
27.05.2011;1.509        04.05.2011;14.54        04.05.2011;1.5    
30.05.2011;1.496        05.05.2011;14.57        05.05.2011;1.5    
31.05.2011;1.503        06.05.2011;14.57        06.05.2011;1.5    
01.06.2011;1.509        09.05.2011;14.61        09.05.2011;1.6    
03.06.2011;1.412        10.05.2011;14.66        10.05.2011;1.6    
06.06.2011;1.380        11.05.2011;14.71        11.05.2011;1.7    
07.06.2011;1.379        12.05.2011;14.71        12.05.2011;1.7    
08.06.2011;1.372        13.05.2011;14.70        13.05.2011;1.7    
09.06.2011;1.366        16.05.2011;14.75        16.05.2011;1.7    
10.06.2011;1.405        17.05.2011;14.69        17.05.2011;1.6    
13.06.2011;1.400        18.05.2011;14.65        18.05.2011;1.6    
14.06.2011;1.414        19.05.2011;14.69        19.05.2011;1.6 
  • If I unpacked A and B, it would contain all values.
  • If I unpacked A, B and C, it would contain:

    [ ["09.05.2011", 1.561, 14.61, 1.6], ["10.05.2011", 1.572, 14.66, 1.6], ["11.05.2011", 1.603, 14.71, 1.7], ["12.05.2011", 1.566, 14.71, 1.7], ["13.05.2011", 1.563, 14.70, 1.7], ["16.05.2011", 1.537, 14.75, 1.7], ["17.05.2011", 1.528, 14.69, 1.6], ["18.05.2011", 1.543, 14.65, 1.6], ["19.05.2011", 1.537, 14.69, 1.6] ]

因此,每一日期都必须具有与档案(即A、B、C、......)相同的价值。

最佳回答
from collections import defaultdict
import itertools

d = defaultdict(list)
for i,j in itertools.chain.from_iterable(datas2):
    if not isinstance(j, str):
        d[i].append(j)

www.un.org/Depts/DGACM/index_spanish.htm 定论如下:

{ 01/01/2011 : [1, 2, 3],  03/03/2011 : [11, 22, 33]}

因此,您可以将其日后编成编号d.items()的标记。

请注意“22/2222”经认证为t,但很容易在<<>for loop>上加以验证。

问题回答

This code is written to work equally well on Python 2.x or Python 3.x. I tested it with Python 2.7 and Python 3.2.

from collections import defaultdict

datas2 = [
    [("01/01/2011", 1), ("02/02/2011", "No"), ("03/03/2011", 11)],
    [("01/01/2011", 2), ("03/03/2011", 22), ("22/22/2222", "no")],
    [("01/01/2011", 3), ("03/03/2011", 33), ("22/22/2222", "333")]
]


def want_value(val):
    """return true if val is a value we want to keep"""
    try:
        # detect numbers by trying to add to 0
        0 + val
        # no exception means it is a number and we want it
        return True
    except TypeError:
        # exception means it is the wrong type (a string or whatever)
        return False

result = defaultdict(list)

for lst in datas2:
    for date, val in lst:
        if want_value(val):
            result[date].append(val)

final_result = list(result.items())
print(final_result)




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签