标签: sorted

使用LINQ获取第一个排序元素?(C#)

首先,看看这段代码:

Dictionary<int,int> dict = Dictionary<int,int>();
dict[3] = 1;
dict[2] = 2;
dict[1] = 3;

foreach(KeyValuePair<int,int> item in dict.OrderByDescending(p => p.Value))
{
    print(item.Value);
    break;
}
Run Code Online (Sandbox Code Playgroud)

此代码基本上打印具有最高值的字典中的条目的值.我想在不使用"破坏" foreach循环的情况下完成此操作.我怎么能这样做?

.net c# linq dictionary sorted

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

在python中排序,以及空字符串

嗨,我正在使用Python中的sorted()函数来订购一个二维数组(我想对列进行排序就像在经典电子表格中完成一样).

在下面的示例中,我使用itemgetter(0)根据第一列的内容对网格进行排序.

但排序在非空字符串之前返回空字符串.

    >>> import operator
    >>> res = [['charly','male','london'],
    ... ['bob','male','paris'],
    ... ['alice','female','rome'],
    ... ['','unknown','somewhere']]
    >>> sorted(res,key=operator.itemgetter(0))
    [['', 'unknown', 'somewhere'], ['alice', 'female', 'rome'], ['bob', 'male', 'paris'], ['charly', 'male', 'london']]
    >>> 
Run Code Online (Sandbox Code Playgroud)

虽然我需要它返回这个:

[['alice', 'female', 'rome'], ['bob', 'male', 'paris'], ['charly', 'male', 'london'], ['', 'unknown', 'somewhere']]
Run Code Online (Sandbox Code Playgroud)

有一个简单的方法吗?

python string sorted

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

我可以在Jython中使用__future__导入使用排序吗?

我在正在使用的计算机上使用Jython的较旧版本(2.2.1),但我需要排序的方法。我已经从未来导入了发电机,但是

from __future__ import sorted
Run Code Online (Sandbox Code Playgroud)

返回SyntaxError:未定义将来的功能。有可以导入的模块吗?

python jython sorted

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

获取排序组合数的算法?

假设有"n"个数字,我们从中选择"p"数字(p小于n),以便对所选的"p"数进行排序.可以重复选定的号码.我们如何计算我们可以选择的组合数量?例如,如果我们有一组数字说{1,2,3,4,5,6}(n = 6),我们将从集合中选择3个数字(p = 3)进行排序.所以我们可以{1,2,3},{1,1,2},{2,3,6},{4,5,5},{5,5,5} .......由于所有这些组合都已排序,因此它们是有效的.我们怎样才能找到我们可以得到的这种分类组合数量


我从排序的话,意思是说,当我们选择p从元素组数字ñ元素,所选择的p元素应该进行排序.

举一个小例子:

如果集合是{1,2,3,4}(所以n = 4)并且我们要选择3个元素(p = 3),那么我们可以选择p个元素(替换)的方式的数量4*4*4=64.所以选择将有{1,1,1},{1,1,2},{1,1,3}{1,1,4},{1,2,1}.....{3,1,1}...{4,4,4}.但在这些选择中,并非所有选择都已排序.在这个例子中,{1,2,1}{3,1,1}没有排序.

我想获得排序选择的数量.
谢谢.

algorithm combinations sorted

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

C#替代C++ STL集<T>

我正在寻找一个排序的数据结构,它类似于STL集(T).我找到了SortedList,但它需要(key,val),我正在寻找类似List(字符串)的东西 - 只有排序.

我在网上找到了Spring.Collections,但我的框架无法识别它.

我可以在常规基本框架中使用简单的SortedSet吗?

谢谢,加尔

c# sorted

3
推荐指数
2
解决办法
2693
查看次数

Python排序多个属性

我有一个如下字典.键值对或用户名:名称

d = {"user2":"Tom Cruise", "user1": "Tom Cruise"}
Run Code Online (Sandbox Code Playgroud)

我的问题是我需要按名称对这些进行排序,但如果多个用户包含与上面相同的名称,那么我需要按用户名对它们进行排序.我查找了已排序的函数,但我真的不了解cmp参数和lambda.如果有人可以解释这些并帮助我这将是伟大的!谢谢 :)

python sorting dictionary sorted

3
推荐指数
2
解决办法
1681
查看次数

Java Generics:按值排序地图

尝试编译以下函数来排序通用映射我得到此错误:

"The method compareTo(V) is undefined for the type V"
Run Code Online (Sandbox Code Playgroud)

请帮助完成这项工作!

public class CollectionsPlus<K,V> {

    /**
     * Sort map by value
     * @param map
     * @return
     */
    public static<K,V> Map<K, V> sortMapByValue(Map<K, V> map) {
        List<Map.Entry<K, V>> list = new LinkedList<Map.Entry<K, V>>(
                map.entrySet());
        Collections.sort(list,
                new Comparator<Map.Entry<K, V>>() {
                    public int compare(Map.Entry<K, V> o1,
                            Map.Entry<K, V> o2) {
                        return (o2.getValue().compareTo(o1.getValue()));
                    }
                });

        Map<K, V> result = new LinkedHashMap<K, V>();
        for (Iterator<Map.Entry<K, V>> it = list.iterator(); it.hasNext();) {
            Map.Entry<K, V> entry = …
Run Code Online (Sandbox Code Playgroud)

java generics sorted map

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

将两个已排序的链接列表合并到python中的一个链接列表中

这是我的代码:

def merge_lists(head1, head2):
    if head1 is None and head2 is None:
        return None
    if head1 is None:
        return head2
    if head2 is None:
        return head1
    if head1.value < head2.value:
        temp = head1
    else:
        temp = head2
    while head1 != None and head2 != None:
        if head1.value < head2.value:
            temp.next = head1
            head1 = head1.next
        else:
            temp.next = head2
            head2 = head2.next
    if head1 is None:
        temp.next = head2
    else:
        temp.next = head1
    return temp
    pass
Run Code Online (Sandbox Code Playgroud)

这里的问题被困在无限循环中.任何人都告诉我问题是什么

例子是:

 assert [] …
Run Code Online (Sandbox Code Playgroud)

python linked-list list sorted

3
推荐指数
2
解决办法
1万
查看次数

在C++中从排序的整数向量中搜索和删除元素的最快方法

我有一个大的排序整数向量.我需要快速查找并从数组中删除八个值.

例如,矢量a包括元素

{1, 4, 7, 15, 16, 19, 24, 26, 31, 53, 67, 68, 73, 75, 77, 82}
Run Code Online (Sandbox Code Playgroud)

向量b包括八个值

{4, 15, 19, 24, 67, 68, 73, 75}
Run Code Online (Sandbox Code Playgroud)

完成操作后,矢量a应该具有

{1, 7, 16, 26, 31, 53, 77, 82}
Run Code Online (Sandbox Code Playgroud)

我的旧解决方案很慢:

for (vector<int>::iterator val = b.begin(); val != b.end(); val++)
        a.erase(remove(a.begin(), a.end(), *val), a.end());
Run Code Online (Sandbox Code Playgroud)

有更快的方法吗?

编辑:

实际上,我的"A"向量比我的"B"向量大很多.也许最好通过二分搜索搜索单个元素并删除它们?

EDIT2:

也许矢量不是这种操作的好容器.我不认为我可以使用forward_list,因为我无法用C++ 11编译.也许我可以使用不同的容器,然后将结果复制到矢量中?

c++ stl vector sorted

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

为什么在合并两个排序列表时会得到两个不同的输出(Python)

我很困惑为什么在更改关系运算符时我得到两个不同的输出:

这是不正确的版本:

listOne = [1,3,6,9,11]
listTwo = [2,4,5,7,8,10,12]

def mergeTwo(l1,l2):
  output = []
  while l1 and l2:
    if l1[0] > l2[0]:
        output.append(l2.pop(0))
    output.append(l1.pop(0))

  if l1:
    output.extend(l1)
  elif l2:
    output.extend(l2)
  print output
Run Code Online (Sandbox Code Playgroud)

输出是: [1, 2, 3, 4, 6, 5, 9, 7, 11, 8, 10, 12]

但是当我这样做时它会起作用:

listOne = [1,3,6,9,11]
listTwo = [2,4,5,7,8,10,12]

def mergeTwo(l1,l2):
  output = []
  while l1 and l2:
    if l1[0] < l2[0]:
        output.append(l1.pop(0))
    output.append(l2.pop(0))

  if l1:
    output.extend(l1)
  elif l2:
    output.extend(l2)
  print output
Run Code Online (Sandbox Code Playgroud)

我将运算符更改为<和弹出的元素的顺序,我得到此输出:

[1, 2, 3, 4, 5, …
Run Code Online (Sandbox Code Playgroud)

python algorithm merge sorted

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

标签 统计

sorted ×10

python ×5

algorithm ×2

c# ×2

dictionary ×2

.net ×1

c++ ×1

combinations ×1

generics ×1

java ×1

jython ×1

linked-list ×1

linq ×1

list ×1

map ×1

merge ×1

sorting ×1

stl ×1

string ×1

vector ×1