int cmp = fnmatch(pattern.c_str(), pl_name.c_str(), FNM_CASEFOLD); if (cmp == 0) return true; else if (cmp != FNM_NOMATCH) { // never see that, i think that it is fatal error. ROS_FATAL("Plugin list check error! fnmatch('%s', '%s', FNM_CASEFOLD) -> %d", pattern.c_str(), pl_name.c_str(), cmp); ros::shutdown(); }
时间: 2023-06-16 14:02:17 浏览: 255
这段代码中,首先使用了 `fnmatch` 函数比较 `pattern` 和 `pl_name` 两个字符串,如果相等则返回 `true`。如果比较结果不是 `FNM_NOMATCH`,则输出一条致命错误信息,同时调用 `ros::shutdown()` 函数关闭 ROS 节点。`fnmatch` 函数用于比较文件名是否符合指定的模式,其中 `FNM_CASEFOLD` 的含义是不区分大小写进行比较。
相关问题
str_nums.sort(key=functools.cmp_to_key(compare))
`str_nums.sort(key=functools.cmp_to_key(compare))` 是Python中对列表(list)进行排序的一种方法,其中 `str_nums` 是你要排序的一个包含字符串元素的列表。`sort()` 方法默认按照元素的字典顺序进行升序排列,但是当元素比较复杂时(比如字符串),可以提供一个自定义的关键字函数 `key`。
`functools.cmp_to_key(compare)` 将传统的比较函数 `compare` 转换为 Python 3 中的可迭代关键字函数。这是因为,在Python 3中,内置的 `cmp` 函数已经被移除,直接传递比较函数给 `sort` 或 `sorted` 可能会报错。通过 `cmp_to_key`,你可以将以前的两数比较操作转换成可以适应于 `key` 参数的形式。
`compare` 函数是一个接受两个参数并返回负数、零或正数的函数,这决定了它们在排序过程中的相对位置。例如,如果你有一个包含数字的字符串列表,你想按照整数值排序,`compare` 函数会解析这两个字符串为数字再进行比较。
完整的示例:
```python
import functools
def compare(a, b):
return int(a) - int(b)
str_nums = ['3', '2', '5', '1', '4']
str_nums.sort(key=functools.cmp_to_key(compare))
print(str_nums) # 输出:['1', '2', '3', '4', '5']
```
key=functools.cmp_to_key
The `functools.cmp_to_key` function is a utility function in Python's `functools` module that helps convert a comparison function to a key function.
In Python, sorting a list of objects requires a way to compare the objects. This is done by providing a comparison function that takes two objects as input and returns `-1`, `0`, or `1` depending on whether the first object is less than, equal to, or greater than the second object.
However, some sorting functions in Python (such as the `sorted` function) require a key function instead of a comparison function. A key function takes a single object as input and returns a value that can be used for sorting.
The `cmp_to_key` function helps convert a comparison function to a key function by returning a new function that takes a single object as input and returns a tuple `(comparison_result, object)` where `comparison_result` is the result of calling the original comparison function with the input object and another object, and `object` is the input object itself. This tuple can then be used for sorting.
Here's an example of how to use `cmp_to_key`:
```
from functools import cmp_to_key
def compare_length(str1, str2):
if len(str1) < len(str2):
return -1
elif len(str1) > len(str2):
return 1
else:
return 0
strings = ['cat', 'dog', 'elephant', 'a', 'zebra']
sorted_strings = sorted(strings, key=cmp_to_key(compare_length))
print(sorted_strings)
# Output: ['a', 'cat', 'dog', 'zebra', 'elephant']
```
In this example, we define a comparison function `compare_length` that compares the lengths of two strings. We then use `cmp_to_key` to convert this comparison function to a key function, and pass it to the `sorted` function to sort a list of strings by length. The resulting sorted list is `['a', 'cat', 'dog', 'zebra', 'elephant']`.
阅读全文
相关推荐

















