English 中文(简体)
“Date”使独裁者保持廉正,通过“Date”使一名Darztionary得以使用
原标题:Sorting a Python Dictionary by "Date" keeping the integrity of the dictionary [duplicate]
  • 时间:2024-04-02 00:07:59
  •  标签:
  • python

在网上进行一些研究时,似乎没有任何简单的方式来下令使用字典。

我的解职包括一份名单,每个名字都是关键人物。

dummy_data = {
    "Date":["01/24/2029","10/28/2027", "01/24/2024","03/24/2024"],
    "Company Name":["Mcdonalds", "Burger King", "KFC","Popeyes"],
    "File_name_location":["C/Documents/Files/invoice1.pdf", "C/Documents/Files/invoice1.pdf","C/Documents/Files/invoice1.pdf"],
}

As you see with the above I have a field called Date which list the dates in random order. I would like to get the least to greatest dates but then also ensuring the "Company Name" as well gets moved around

根据一些资料 我不得不将日期改为我发言的截止日期。

for index, result in enumerate(dummy_data["Date"]):
    dummy_data["Date"][index] = datetime.strptime(dummy_data["Date"][index], "%m/%d/%Y").date()

我知道,将日期改变为非常快的日期,时间与Mlambda相距甚远,但我没有太快地问到Mlambda。

无论如何,另一个问题是,如果可能的话,我也希望把我的日期保留在紧张之中,但似乎甚至是不可能的,因为日期()以这种格式(1.024-2029)** 返回,但现在我还必须在小体中总结这一日期,否则,日期清单将带有时间目的,而不是数据说明。

我的最终目标是:

  1. Sort by Date and making sure "Company Name" also keeps the correct index of the above
  2. If possible keep the dates with the slashes if not possible then no worries.

我知道有一个有秩序的分班,但我不熟悉这一点,因为我认为,这一班像一个马拉姆比达这样的某种功能是一种参数。

  • If it even matters why The reason I would like to sort by dates is because these dates are being pulled in by some invoices which I am using a PDFREADER to read the dates and then I would to ultimately merge the pdfs together but it will be merged by the date of the file meaning the file with the "least" date will be put on top or bottom depending on what I want. In any case if anyone feels like they have some sort of workaround please get back to me.

也不论出于何种原因 我确实在尝试了。

sorted_val = str(sorted(dummy_data["Date"]))

我把上述检查增加一倍,实际上没有发现任何东西。

最佳回答

平行名单往往造成混乱。 如果名单是独裁者名单,而不是名单的字典,则更加容易。

dummy_data = [
    {
        "Date": "01/24/2029",
        "Company Name": "Mcdonalds"
    },
    {
        "Date": "10/28/2027",
        "Company Name": "Burger King"
    },
    {
        "Date": "01/24/2024",
        "Company Name": "KFC"
    },
    {
        "Date": "03/24/2024",
        "Company Name": "Popeyes"
    }
]

因此,我们现在可以采用一种简单的<代码>str.split。

sorted(
    dummy_data, 
    key=lambda x: ((y := [int(z) for z in x[ Date ].split( / )])[2], y[0], y[1])
)

我们得到:

[{ Date :  01/24/2024 ,  Company Name :  KFC }, 
 { Date :  03/24/2024 ,  Company Name :  Popeyes }, 
 { Date :  10/28/2027 ,  Company Name :  Burger King }, 
 { Date :  01/24/2029 ,  Company Name :  Mcdonalds }]

你们只需要说明如何从你的数据结构转向这一结构。 这应当非常简单。

Breaking down the lambda

[int(z) for z in x[ Date ].split( / )]

这是一份谅解清单。 我们把日期分为<条码>/,但每一条都是有条不紊的,因此,我们就有了其中的印本。 www.un.org/spanish/ecosoc 例如,我们现在有一个名单<代码>[10,3,2023]。

然后,我们利用“航海经营者”将这份名单调往<代码>y,同时把该名单退回。 我们只能写一下:

([int(z) for z in x[ Date ].split( / )][2], [int(z) for z in x[ Date ].split( / )][0], [int(z) for z in x[ Date ].split( / )][1])

但是,这既要做太多的工作,又要去看。 航海操作员意味着我们只需要一次打电话<条码>。

然后,我们制定了第3项内容的图表(根据指数2,因为Pyth0on清单为零指数),这是第一个要素,也是第二个要素。 这是年月。

By using this as the key we sort the dictionaries first on year, then month, then date.

问题回答

暂无回答




相关问题
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 ]="...

热门标签