标签: sorting

如何按时间戳对数据帧进行排序

我需要按时间戳对以下数据帧进行排序。我知道这很简单,也许一行代码,但我对 sort_values 或排序索引的所有尝试都失败了。我必须按 id 和时间戳对数据帧进行排序。这是数据框的片段:

 id        timestamp        x_value
2854    2018-01-03 00:05:35 1
2854    2018-01-03 00:05:36 1
2854    2018-01-03 00:05:37 1
2854    2018-01-03 00:05:38 1
2854    2018-01-03 00:05:39 1
2854    2018-01-03 00:05:41 2
2854    2018-01-03 00:05:15 1 
2854    2018-01-03 00:05:42 1
2854    2018-01-03 00:05:14 1
2854    2018-01-03 00:05:12 2
2854    2018-01-03 00:04:47 1
2854    2018-01-03 00:04:48 1
2854    2018-01-03 00:04:49 1
2854    2018-01-03 00:04:51 2
2854    2018-01-03 00:04:52 1
.
.
.
3245    2018-01-02 16:42:25 3
3245    2018-01-02 16:42:22 2
3245 …
Run Code Online (Sandbox Code Playgroud)

python sorting dataframe

1
推荐指数
1
解决办法
9334
查看次数

按字典值的值对字典进行排序

我目前有一些如下所示的 JSON:

{
    "Chile": {
        "num_of_unique_ips": 1,
        "num_of_unique_asns": 1,
        "asns": {
            "AS16629 CTC. CORP S.A. (TELEFONICA EMPRESAS)": 1
        }
    },
    "China": {
        "num_of_unique_ips": 1,
        "num_of_unique_asns": 1,
        "asns": {
            "AS4808 China Unicom Beijing Province Network": 1
        }
    }, # this goes on and on for ever country
}
Run Code Online (Sandbox Code Playgroud)

我通过运行将其转换为字典:

import json
login_by_country = json.loads(open('login_by_country.json', 'r'))
Run Code Online (Sandbox Code Playgroud)

我将如何按照每个国家/地区的值对这本词典进行排序num_of_unique_ips?

python sorting dictionary

1
推荐指数
1
解决办法
90
查看次数

Python:对不适合内存的大列表进行排序

我有一个大小合适的.tsv文件,其中包含以下格式的文档

ID  DocType NormalizedName  DisplayName Year    Description
12648   Book    a fancy title   A FaNcY-Title   2005    This is a short description of the book
1867453 Essay   on the history of humans    On the history of humans    2016    This is another short description, this time of the essay
...
Run Code Online (Sandbox Code Playgroud)

该文件的压缩版本大小约为 67 GB,压缩后约为 22 GB。

我想根据 ID(大约 3 亿行)按升序对文件的行进行排序。每行的 ID 都是唯一的,范围为 1 - 2147483647(正数部分long),可能存在间隙。

不幸的是,我最多只有 8GB 可用内存,所以我无法一次加载整个文件。

对该列表进行排序并将其写回磁盘的最省时的方式是什么?

python sorting python-3.x

1
推荐指数
1
解决办法
2888
查看次数

MongoDb - 按数组字段的最高值元素排序

date我有一个包含如下文档的集合,我想根据它们在数组中包含的最高值对文档进行排序status:

[{
  "_id": "1",
  "name": "Tea",
  "status": [
    {
      "state": "a",
      "date": "2019-08-22"
    },
    {
      "state": "b",
      "date": "2019-07-12"
    },
  ],
  "_id": "2",
  "name": "Coffee",
  "status": [
    {
      "state": "c",
      "date": "2019-05-01"
    },
    {
      "state": "b",
      "date": "2019-12-31"
    }
  ]
}]
Run Code Online (Sandbox Code Playgroud)

因此,在示例中,用于排序的值应类似于:

  • 茶:最高状态日期2019-08-22 (状态:a)
  • 咖啡:最高状态日期2019-12-31 (状态:b)

有任何想法吗?

arrays sorting mongodb aggregation-framework

1
推荐指数
1
解决办法
1512
查看次数

Python 3 中排序函数与比较函数的等效项

这在 Python 2.7 中完美运行

def some_compare_func(x, y):
  ....

a = sorted(some_list, lambda x, y: some_compare_func(x, y))
Run Code Online (Sandbox Code Playgroud)

然而,在 Python 3.x 中同样会出现此错误。

TypeError: sorted expected 1 arguments, got 2
Run Code Online (Sandbox Code Playgroud)

有没有一种方法可以在 Python 2.7 和 3.x 中使用排序函数进行排序?

python sorting python-2.7 python-3.x

1
推荐指数
1
解决办法
7856
查看次数

如何按时间戳对数组进行排序?

我正在尝试array按时间戳值对此进行排序。我想按升序对它们进行排序,如果其中任何一个具有不确定的属性,请将其放在最后。我目前遇到错误Cannot read property 'first_release_date' of undefined。怎么解决这个问题呢?

var array = 
[
  {
    "id": 1969,
    "cover": {
      "id": 1960,
      "url": "image.jpg"
    },
    "first_release_date": 1083542400,
    "name": "Item 1"
  },
  {
    "id": 113242,
    "name": "Item 2"
  },
  {
    "id": 25076,
    "first_release_date": 1540512000,
    "name": "Item 3"
  },
  {
    "id": 1969,
    "cover": {
      "id": 1960,
      "url": "image.jpg"
    },
    "name": "Item 4"
  },
  {
    "id": 9245,
    "first_release_date": 1292976000,
    "name": "Item 5"
  }
];

Object.keys(array).forEach((key) => {
  console.log(`Before: ${array[key].name}`)
});

array.sort((a,b) => a.array.first_release_date > …
Run Code Online (Sandbox Code Playgroud)

javascript arrays sorting jquery ecmascript-6

1
推荐指数
1
解决办法
785
查看次数

PowerShell小数排序问题

我有一个包含十进制数字的数组,如下所示:

赖恩福尔格
###########
100000.00001
1000000.00001
101000.00001
101010.00001
101020.10001

如果我不带参数对其进行排序,我会得到上面的顺序。我希望第二个数字是最后一个条目。我还尝试按 int 或版本或正则表达式进行排序,但直到现在我还没有找到我期望的方式。

$script:array = $script:array | Sort-Object reihenfolge

$script:array = $script:array | Sort-Object { [regex]::Replace($_.reihenfolge, '\d+', { $args[0].Value.PadLeft(20) }) }

$script:array = $script:array | Sort-Object { $_.reihenfolge -as [version] }

$script:array = $script:array | Sort-Object { $_.reihenfolge -as [int] }
Run Code Online (Sandbox Code Playgroud)

sorting powershell

1
推荐指数
1
解决办法
1550
查看次数

如何使用 O(n) 算法在 C 中就地分区数组?

我想对整数数组进行分区,以便负值位于正值的左侧,并且我想就地执行此操作(无需额外的辅助内存)。请注意,数组不能从小到低排序,只需将负数收集到左侧,将正数收集到右侧。

我创建了一个时间复杂度为 O(n^2) 的朴素函数,但想知道如何在线性时间内解决它?我的算法使用两个指针,开始指向数组的最左边和最右边的元素。当 ptr1 遇到正值,而 ptr2 遇到负值时,我们执行交换,指针向右/向左迭代,当它们相遇时,函数返回,因为数组已完成收集。

你能帮我想出一个更简单的算法来进行这种收集吗?

int* swaps(int* array){

    int* ptr1;
    ptr1 = &array[0];
    int* ptr2;
    ptr2 = &array[N-1];
    int tmp;


    for(ptr1; ptr1 <= &array[N-1]; ptr1++){
         if(ptr1==ptr2){
             return array; 
         }
         if(*ptr1 >= 0){
             for(ptr2; ptr2 >= &array[0]; ptr2--){
                if(ptr1==ptr2){
                  return array; 
                }

                // swap elements
                if(*ptr2 < 0){
                tmp = *ptr2;
                *ptr2 = *ptr1;
                *ptr1 = tmp;
                ptr2--;
                if(ptr1==ptr2){
                   return array; 
                }
                break;
                }
             }
         }
    }
}
Run Code Online (Sandbox Code Playgroud)

c arrays sorting algorithm complexity-theory

1
推荐指数
1
解决办法
427
查看次数

按值对字典进行自定义排序

我有一个简单的字典和一个列表,我想按字典中的值排序。

data_dict = {'foo' : 'one', 'bar' : 'two', 'foobar' : 'three', 'notinsort' : 'four'}

custom_sort = ['three','one','two'] 
Run Code Online (Sandbox Code Playgroud)

我自己的尝试是在排序中使用带有自定义键的字典理解:

{k:v for k,v in sorted(data_dict.items(), key=lambda i : custom_sort.index(i[1]) )}
Run Code Online (Sandbox Code Playgroud)

这将正确地返回一个ValueError: 'four' is not in list

没问题,我可以使用 lambda 中的 if-else 语句过滤掉它吗?因为我仍然希望最初按自定义排序对值进行排序,所以可以使用自然排序。

{
    k: v
    for k, v in sorted(
        data_dict.items(),
        key=lambda i: custom_sort.index(i[1])
        if [k for k in data_dict.values() if k in custom_sort] 
        else sorted(data_dict.items()),
    )
}
Run Code Online (Sandbox Code Playgroud)

这返回相同的 ValueError,我尝试过的任何变化最终都会给我一个自然的排序,忽略我的自定义键。

我想要的上述输入输出是:

data_dict = {'foobar' : 'three', 'foo' : 'one', 'bar' : …
Run Code Online (Sandbox Code Playgroud)

python sorting dictionary

1
推荐指数
1
解决办法
236
查看次数

如何通过嵌套列表的值和仅嵌套列表的第一个值的reverse=True条件对列表的python列表进行排序?

我试图按嵌套列表的值对此列表进行排序,但我只想要按相反顺序排列的字母位置和按升序排列的数字。

sample_list = [
  ['y', 4], 
  ['x', 2], 
  ['y', 1], 
  ['z', 2], 
  ['x', 5], 
  ['y', 2]]

sorted_list = sorted(sample_list, key=lambda nested: (nested[0], nested[1]), reverse=True)
Run Code Online (Sandbox Code Playgroud)

输出:

[['z', 2], 
 ['y', 4], 
 ['y', 2], 
 ['y', 1], 
 ['x', 5], 
 ['x', 2]]
Run Code Online (Sandbox Code Playgroud)

预期输出:

[['z', 2], 
 ['y', 1], 
 ['y', 2], 
 ['y', 4], 
 ['x', 2], 
 ['x', 5]]
Run Code Online (Sandbox Code Playgroud)

python sorting python-3.x

1
推荐指数
1
解决办法
1077
查看次数