小编tca*_*chy的帖子

按值排序字典然后键

我可以按键或值排序,但我需要按值排序,然后按键排序,在一行中.为了更好地解释这一点,我可以告诉你我的问题:

dict = {'apple': 2, 'banana': 3, 'almond':2 , 'beetroot': 3, 'peach': 4}
Run Code Online (Sandbox Code Playgroud)

我希望我的输出按其值降序排序,然后按键(按字母顺序)升序(AZ).导致这样的清单:

随着输出: ['peach', 'banana', 'beetroot', 'almond', 'apple']

到目前为止我知道如何做的唯一方法是:

[v[0] for v in sorted(dict.items(), key=lambda(k,v): (v,k))]
Run Code Online (Sandbox Code Playgroud)

随着输出: ['almond', 'apple', 'banana', 'beetroot', 'peach']

因此,它按升序对值进行排序,按升序(AZ)按字母顺序对键进行排序.所以如果我扭转这个:

[v[0] for v in sorted(dict.items(), key=lambda(k,v): (v,k), reverse=True)]
Run Code Online (Sandbox Code Playgroud)

随着输出: ['peach', 'beetroot', 'banana', 'apple', 'almond']

它按降序对值进行排序,按字母顺序按降序排序(ZA).

有没有办法我可以按降序对值进行排序,按升序排列键(即AZ)并获得上面显示的输出?

python sorting dictionary

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

按长度和值对列表列表进行排序

我有一份清单清单:

>>> a = [['3D'], ['3D', '4D', '5D'], ['4C'], ['2C'],['4C', '4D'], ['4D'], ['5D'], \
... ['JC'], ['JC', 'JS'], ['JS']]
Run Code Online (Sandbox Code Playgroud)

您可能会注意到这是卡片值,即C = Clubs等.J = Jack等我还有一个参考列表:

>>> confrom = {'3':3, '4':4, '5':5, '6':6, '7':7, '8':8, '9':9, \
... '0':10, 'J':11, 'Q':12, 'K':13, 'A':14, '2':15}
Run Code Online (Sandbox Code Playgroud)

正在玩一个最大值为2的纸牌游戏.要按列表长度排序,我会:

>>> a = sorted(a, key = lambda x: len(x))
>>> a
... [['3D'], ['4C'], ['4D'], ['2C'], ['5D'], ['JC'], ['JS'], ['4C', '4D'], ['JC', 'JS'], ['3D', '4D', '5D']]
Run Code Online (Sandbox Code Playgroud)

我还需要根据它们的字典值对它们进行排序,这样我的结果列表就是:

>>> [['3D'], ['4C'], ['4D'], ['5D'], ['JC'], ['JS'], ['2C'], ['4C', '4D'], …
Run Code Online (Sandbox Code Playgroud)

python list

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

c中的分叉和管道工艺

所以我有一个项目要做,但我完全被难倒了.我已经花了十个小时而且无处可去.我没有特别想要代码的答案,但一些伪代码和正确方向的良好提示将有助于堆!

它分叉了许多进程,k - 一个命令行参数,通过管道连接 - 每个进程连接到下一个进程,最后一个进程连接到第一个进程.进程号k将其消息发送到进程号(k + 1)%n.

进程0从中读取一行stdin.然后它将它发送到进程1.每个其他进程读取该行,将字符串的第一个字节递增1,然后将该行中继到下一个进程.在中继时,它会打印状态消息(如下所示).

当消息返回到进程0时,它也会输出到标准输出.当进程收到EOF(来自管道,如果它的进程不是0,或来自stdin进程0),它会打印最终的字符串.这将关闭所有管道.

预期的产出是:

$ ./ring 4
hello
process #0 (32768) sending message: hello
process #1 (32769) relaying message: iello
process #2 (32770) relaying message: jello
process #3 (32767) relaying message: kello
I hear kello
^C
$
Run Code Online (Sandbox Code Playgroud)

我到目前为止所写的内容:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

#define BUFFER_SIZE 80
#define READ_END 0
#define WRITE_END 1

int main(int argc, char *argv[])
{
    char readmsg[BUFFER_SIZE], readmsg2[BUFFER_SIZE], final[BUFFER_SIZE];
    int pid, …
Run Code Online (Sandbox Code Playgroud)

c linux pipe process

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

检查字符串的字符是否按字母顺序升序,并且其上升是均匀间隔的python

因此需要检查字符串的字符是否按字母顺序升序,以及该上升是否均匀分布.

a = "abc"
b = "ceg"
Run Code Online (Sandbox Code Playgroud)

所以a按字母顺序递增,间距为1(如果转换为序数值,则为97,98,99).b也按字母顺序递增,间距为2(99,101,103).

我坚持使用以下代码:

a = 'jubjub'
words1 = []
ords = [ord(letter) for letter in a]
diff = ords[1] - ords[0]
for ord_val in range(1, len(ords)-1):
    if diff > 0:
        if ords[ord_val + 1] - ords[ord_val] == diff:
            if a not in words1:
                words1.append((a, diff))
print words1
Run Code Online (Sandbox Code Playgroud)

为什么'jubjub'有效,'ace'有效,但'catcat'不适用?

python

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

将字符串的第一个字节递增1

我有一个main程序:

int main() {
    char *str = "hello";
    printf("%s\n", str);
    /* Shift first byte 1 to get "iello" */

    /* Have tried
    str[0] >>= 8; */
    printf("%s\n", str);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

基本向上串的第一字节偏移的字节从去hello到iello或Hello以Iello

c string pointers

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

标签 统计

python ×3

c ×2

dictionary ×1

linux ×1

list ×1

pipe ×1

pointers ×1

process ×1

sorting ×1

string ×1