如何按给定的一组索引对数组进行排序并优先考虑该索引中的值。截至目前,我无法使用排序方法按整个数组中的特定索引进行排序,因为 0 值会导致问题。
例如,首先按索引 1 排序,然后按索引 0,然后是 2
tmpList = [[0,-10,0],[0,10,0],[0,5,0],[1,0,0],[0,0,-1],[0,0,0],[0,0,5]]
Res = sorted(tmpList, key=lambda x: x[1] )
>>[[0, -10, 0], [1, 0, 0], [0, 0, -1], [0, 0, 0], [0, 0, 5], [0, 5, 0], [0, 10, 0]]
Run Code Online (Sandbox Code Playgroud)
我需要在这种排序中具有更大的灵活性,以便我可以将索引中除零以外的值作为优先级进行优先级排序,因此它将排序为:
[[0,-10,0],[0,5,0],[0,10,0],[1,0,0],[0,0,-1],[0,0,5],[0,0,0]]
如何在具有多个条件的python中排序?有一个类似的任务,但零问题仍然存在
这段代码应该按升序对数字进行排序,但它垂直输出,如何水平输出它们?
示例输出:
1st array: 1 2 3
2nd array: 3 4 5
3rd array: 3 4 6
Run Code Online (Sandbox Code Playgroud)
int[] i1 = new int[]{3, 2, 1};
Arrays.sort(i1);
System.out.print("1st array : ");
for(int index=0; index < i1.length ; index++)
System.out.print(" " + i1[index]);
int[] i2 = new int[]{5, 4, 3};
Arrays.sort(i2);
System.out.println("2nd array : ");
for(int index=0; index < i2.length ; index++)
System.out.print(" " + i1[index]);
int[] i3 = new int[]{6, 3, 4};
Arrays.sort(i3);
System.out.print("3nd array : ");
for(int index=0; index < i3.length …Run Code Online (Sandbox Code Playgroud) 我有一张这样的表:
// mytable
+----+---------+-------------------------------+
| id | title | content |
+----+---------+-------------------------------+
| 1 | hello | how are you? |
| 2 | you | it is content2 |
| 3 | what | hello is a word for greating |
| 4 | mouse | it is content4 |
+----+---------+-------------------------------+
Run Code Online (Sandbox Code Playgroud)
好吧,我想优先考虑title比content. 我的意思是,我想显示title列(第一个)的所有结果,然后显示content列(第二个)的所有结果。这里也是我的查询:
select * from mytable where match(title, content) against($word);
Run Code Online (Sandbox Code Playgroud)
这里还有两个例子:
示例 1:
$word = 'you';
Run Code Online (Sandbox Code Playgroud)
我想要这个输出:(专注于排序)
+----+---------+-------------------------------+
| id …Run Code Online (Sandbox Code Playgroud) for (int i = 1; i < data.Count; i++)
{
int j = i;
while (j > 0)
{
if (numarray[j - 1] > numarray[j])
{
int temp = numarray[j - 1];
numarray[j - 1] = numarray[j];
numarray[j] = temp;
j--;
}
else
break;
}
}
Run Code Online (Sandbox Code Playgroud)
有人可以帮我确定上面代码的排序算法是什么吗?我知道冒泡排序效率不高。如果我要改用插入排序算法,我该如何改进上面的代码。谢谢!
我正在阅读别人的代码,我看到如下内容:
sort(myvec.begin(), myvec.begin());
Run Code Online (Sandbox Code Playgroud)
我写了一些代码来测试它,它似乎和
sort(myvec.begin(), myvec.end());
Run Code Online (Sandbox Code Playgroud)
他们真的做同样的事情吗?有记录吗?有没有办法理解为什么,或者以这种方式实现它只是一个随意的选择?
回答后更新
是的,这是一个无操作。我的测试代码有一个错误。
{'goofy': '818-399-2763', 'mickey': '213-333-2341', 'minnie': '510-540-2390', 'donald': '415-638-4433'}
Run Code Online (Sandbox Code Playgroud)
上面是字典phoneBook。如果我打印电话簿,我会得到整个字典,如下所示
>>>print (phoneBook)
{'goofy': '818-399-2763', 'mickey': '213-333-2341', 'minnie': '510-540-2390', 'donald': '415-638-4433'}
Run Code Online (Sandbox Code Playgroud)
但是在我对字典使用 sorted()phoneBook对其键进行排序并打印排序后的字典后,它不会返回带有排序键的整个字典,而是返回一个包含所有键的列表。
>>>print (sorted(phoneBook))
['donald', 'goofy', 'mickey', 'minnie']
Run Code Online (Sandbox Code Playgroud)
所以我的问题是:除了键的顺序改变了之外,排序的字典还是字典吗?如果是这样,我们在打印 sorted(phoneBook) 后不应该得到如下输出吗?
phoneBook = {'donald': '415-638-4433', 'goofy': '818-399-2763', 'mickey': '213-333-2341', 'minnie': '510-540-2390'}
Run Code Online (Sandbox Code Playgroud)
而不是键列表?
在 C#.NET 上工作。我有这个清单:
List<Tuple<string, float>> sort = new List<Tuple<string, float>>();
Run Code Online (Sandbox Code Playgroud)
我想按浮点值对这个列表进行排序。例如,如果列表是这样的:
a,45
b,2
s,32
se,83.21
te,84
s3,9.5
f,7
Run Code Online (Sandbox Code Playgroud)
我希望它按降序排序,如下所示:
te,84
se,83.21
a,45
s,32
s3,9.5
f,7
b,2
Run Code Online (Sandbox Code Playgroud) 我的插入排序对每个数字进行排序,但第一个数字除外。它从第二个元素到最后一个元素排序,但它从不包括第一个元素。我的插入排序有什么问题。我根据 CLRS 一书的伪代码编写了这段代码,我无法调试它的问题。
#include <iostream>
void InsertSort(int data[], int length)
{
//std::cout<<length<<std::endl;
for(int j = 1; j < length; j++)
{
int key = data[j];
int i = j - 1;
while(i > 0 && data[i] > key)
{
data[i + 1] = data[i];
i--;
}
data[i+1] = key;
}
for(int x = 0; x < length; x++)
{
std::cout<<data[x]<<" ";
}
std::cout<<std::endl;
}
int main(int argc, const char * argv[])
{
// insert code here...
//std::cout << "Hello, World!\n"; …Run Code Online (Sandbox Code Playgroud) def check():
dict_choice_a = {(a, b) : value, (b, a) : value} #(a, b) and (b, a) refer to the same value but repeted
dict_choice_b = {tuple(sorted((a, b)) : value} #not repetitive but unreadable
dict_choice_a[(a, b)] = new_value #need to do twice to change value but more readable than dict_choice_b
dict_choice_a[(b, a)] = new_value
#value of both keys are always the same
Run Code Online (Sandbox Code Playgroud)
我想创建一个dictionary引用其值的元组键,该键需要可交换,(a, b) = (b, a)并且它们只引用相同的值。
这里的问题是:使密钥的 tulpe 元素可交换但也引用相同值的最佳方法是什么。
此外,字符串也应该在解决方案中起作用。
我在 javascript 中有一个这样的数组
[A,AA,CC,DD,B,C]
Run Code Online (Sandbox Code Playgroud)
我希望它像这样排序
[A,B,C,AA,CC,DD]
Run Code Online (Sandbox Code Playgroud) sorting ×10
arrays ×4
python ×3
c# ×2
c++ ×2
dictionary ×2
.net ×1
bubble-sort ×1
integer ×1
java ×1
javascript ×1
list ×1
mysql ×1
python-3.x ×1
sql-order-by ×1
std ×1
tuples ×1