标签: ord

如何为结构实现Ord?

我已经看到了一个与此类似的问题,但没有人告诉我如何实现Ord结构.例如,以下内容:

struct SomeNum {
    name: String,
    value: u32,
}

impl Ord for SomeNum {
    fn cmp(&self, other:&Self) -> Ordering {
        let size1 = self.value;
        let size2 = other.value;
        if size1 > size2 {
            Ordering::Less
        }
        if size1 < size2 {
            Ordering::Greater
        }
        Ordering::Equal
    }
}
Run Code Online (Sandbox Code Playgroud)

这给了我错误:

error: the trait `core::cmp::Eq` is not implemented for the type `SomeNum` [E0277]
Run Code Online (Sandbox Code Playgroud)

我该如何解决这个问题?我尝试将实现更改为:

impl Ord for SomeNum where SomeNum: PartialOrd + PartialEq + Eq {...}
Run Code Online (Sandbox Code Playgroud)

并添加适当的partial_cmpeq函数,但它给我的错误,这两个方法都不是成员Ord.

rust ord

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

为类型实现Ord很尴尬?

我有一个newtype,我想实现Ord:

use std::cmp::{Ord, Ordering};

struct MyType(isize);

impl Ord for MyType {
    fn cmp(&self, &other: Self) -> Ordering {
        let MyType(ref lhs) = *self;
        let MyType(ref rhs) = *other;
        lhs.cmp(rhs)
    }
}
Run Code Online (Sandbox Code Playgroud)

当我尝试比较我的类型的两个变量时,我得到错误:

error[E0277]: the trait bound `MyType: std::cmp::PartialOrd` is not satisfied
 --> src/main.rs:5:6
  |
5 | impl Ord for MyType {
  |      ^^^ can't compare `MyType` with `MyType`
  |
  = help: the trait `std::cmp::PartialOrd` is not implemented for `MyType`

error[E0277]: the trait bound `MyType: std::cmp::Eq` is not satisfied …
Run Code Online (Sandbox Code Playgroud)

traits rust ord

10
推荐指数
3
解决办法
2258
查看次数

Rust 中的 Python 的 chr() 和 ord()

ord()在Python中,您可以使用and将整数转换为字符,并将字符转换为整数chr()

>>>a = "a"
>>>b = ord(a) + 1
>>>b = chr(b)
Run Code Online (Sandbox Code Playgroud)

我正在寻找一种方法在 Rust 中做同样的事情,但我还没有找到类似的东西。

chr rust ord

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

Python - 字符串中的字符数增加1

我已经在python中搜索了如何做到这一点,我找不到答案.如果你有一个字符串:

>>> value = 'abc' 
Run Code Online (Sandbox Code Playgroud)

如何将字符串中的所有字符增加1?所以我正在寻找的输入是:

>>> value = 'bcd' 
Run Code Online (Sandbox Code Playgroud)

我知道我可以使用ord和chr用一个角色做到这一点:

>>> value = 'a'
>>> print (chr(ord(value)+1)) 
>>> b
Run Code Online (Sandbox Code Playgroud)

ord()chr()只能采取一个字符.如果我使用上面的相同语句,并且字符串包含多个字符.我会得到错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: ord() expected a character, but string of length 3 found 
Run Code Online (Sandbox Code Playgroud)

有没有办法做到这一点?

python chr ord

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

ord()函数的名称代表什么?

官方Python文档解释道 ord(c)

ord(c):
给定一个表示一个Unicode字符的字符串,返回一个表示该字符的Unicode代码点的整数.例如,ord('a')返回整数97,ord('€')(欧元符号)返回8364.这是chr()的反函数.

它没有指明含义ord,谷歌搜索没有帮助.

它的起源是什么?

python unicode built-in ordinal ord

7
推荐指数
2
解决办法
1632
查看次数

使用ord()将字母转换为int(非常基本)

Python初学者在这里.尝试通过在这里和那里阅读代码来学习.在一个旨在打开python中的Excel文件的程序中遇到了这个问题.这个函数做了一个简单的工作 - 使用ord()将Excel列字母标签('Z','BB'或'CCC')转换为int.在我看到转换代码的这一部分之前,我理解得很好:

if clen == 1:
    return ord(column[0]) - 64
elif clen == 2:
    return ((1 + (ord(column[0]) - 65)) * 26) + (ord(column[1]) - 64)
Run Code Online (Sandbox Code Playgroud)

是什么目的(1 +(ORD(列[0]) - 65)与只使用(ORD(列[0]) - 64)再次将"1 +"似乎是多余的,这是否是有目的的.?

这是完整的功能:

def column_index_from_string(column, fast=False):
    """Convert a column letter into a column number (e.g. B -> 2)"""

    column = column.upper()

    clen = len(column)

    if not fast and not all('A' <= char <= 'Z' for char in column):
        msg = 'Column string must contain …
Run Code Online (Sandbox Code Playgroud)

python excel ord

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

如何在 perl v5.24 中获取 unicode 代码点?

我想记录作为参数剪切并粘贴到 bash 中的字符串的十六进制 unicode 代码点。ord 不这样做;ord 似乎只在 ascii 范围内工作。

\n\n

我发现的关于 ord 的大部分内容至少已有六年或更久了,并且不再相关,因为我使用的是 v5.24,我读过的 v5.24 内置了 unicode 支持。\n在 python 中,它是琐碎的:

\n\n
\nfor i in unicode(sys.argv[1], \'utf-8\'):\n    print i.encode("utf_16_be").encode("hex")\n
Run Code Online (Sandbox Code Playgroud)\n\n

它在 bash 中工作。\n我认为问题在于 ord 函数本身,它似乎没有针对 unicode 进行更新。

\n\n
\n# ord.pl does not provide the unicode code point for a pasted variable.\nuse strict;\nuse warnings;\n#use charnames (); #nope.\n#use feature \'unicode_strings\'; #nope.  Already automatically using as of v5.12.\n#use utf8; #nope.\n#binmode(STDOUT, ":encoding(UTF-8)"); #nope.\n\nmy $arg = "";\n\nforeach $arg  (@ARGV) {\n  print $arg . " is " …
Run Code Online (Sandbox Code Playgroud)

unicode perl ord

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

python如何像zlib.adler32一样快速地计算一个简单的校验和

我希望计算一个简单的校验和:只需添加所有字节的值.

我发现最快的方法是:

checksum = sum([ord(c) for c in buf])
Run Code Online (Sandbox Code Playgroud)

但对于13 Mb数据buf,需要4.4 s:太长(在C中,需要0.5 s)

如果我使用:

checksum = zlib.adler32(buf) & 0xffffffff
Run Code Online (Sandbox Code Playgroud)

需要0.8秒,但结果不是我想要的.

所以我的问题是:是否有任何函数,或者包含在python 2.6中的lib或C来计算一个简单的校验和?

谢谢提前,埃里克.

python byte checksum sum ord

4
推荐指数
2
解决办法
7390
查看次数

Python ord函数中的多个字符

编程初学者在这里.(Python 2.7)

是否有解决方法为Python的ord函数使用多个字符?

例如,我有一个十六进制字符串'\ xff\x1a',我想要十进制值,以便我可以将其与其他十六进制字符串相加.但是ord只接受一个十六进制字符串字符.

谢谢!

python hex ord

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

如何在 elisp 中获取字符的代码点(以及其他方式)?

我很惊讶在 elisp 手册或 SO 中找不到这个。我只想要许多语言的 chr() 和 ord() 或类似的等效项:在实际字符及其(unicode)代码点值之间进行转换。

Emacs Lisp:获取字符的 ascii 值解释了对于 elisp,一个 char就是的代码点。但是如果我需要将该 char~int 表示为一系列 ASCII 十进制数字怎么办?

例如,如果我想在缓冲区中生成一个显示等效项的可读表?

谢谢!

unicode elisp chr ord

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

标签 统计

ord ×10

python ×5

chr ×3

rust ×3

unicode ×3

built-in ×1

byte ×1

checksum ×1

elisp ×1

excel ×1

hex ×1

ordinal ×1

perl ×1

sum ×1

traits ×1