相关疑难解决方法(0)

Python 3.3中的哈希函数在会话之间返回不同的结果

我已经在python 3.3中实现了BloomFilter,并且每次会话都得到了不同的结果.深入研究这种奇怪的行为让我进入了内部的hash()函数 - 它为每个会话返回相同字符串的不同哈希值.

例:

>>> hash("235")
-310569535015251310
Run Code Online (Sandbox Code Playgroud)

-----打开一个新的python控制台-----

>>> hash("235")
-1900164331622581997
Run Code Online (Sandbox Code Playgroud)

为什么会这样?为什么这有用?

python security hash hash-collision python-3.3

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

如何散列*args**kwargs进行函数缓存?

我正在使用xlwt哪个有4k限制可以在Excel文档中定义多少样式.

通常,一个人创建如下样式:

style = xlwt.easyxf("font: bold 1")
Run Code Online (Sandbox Code Playgroud)

我简单地换成了

def cached_easyxf(self, format):
    return self._cache.setdefault(format, xlwt.easyxf(format))
Run Code Online (Sandbox Code Playgroud)

哪个效果很好.现在,我发现我需要传递关键字参数,这有时让我思考:我应该如何散列args/kwargs签名?

我应该根据str(值)创建缓存键吗?泡菜?什么是最强大的?

对于我的情况,看起来我可以将键/值转换为字符串并将其添加到我的键...但我现在好奇一个通用的方法来处理这个与不可用的类型,如 arg=[1, 2, 3]

def cached_call(*args, **kwargs):
    return cache.get(what_here)
cached_call('hello')
cached_call([1, 2, 3], {'1': True})
Run Code Online (Sandbox Code Playgroud)

python

18
推荐指数
1
解决办法
3698
查看次数

我是否通过这种重复数据删除功能重新发明了轮子?

我正在寻找一种set()类似于重复删除列表的方法,除了原始列表中的项目不可清除(它们是dicts).

我花了一段时间寻找足够的东西,最后我写了这个小功能:

def deduplicate_list(lst, key):
    output = []
    keys = []
    for i in lst:
        if not i[key] in keys:
            output.append(i)
            keys.append(i[key])

    return output
Run Code Online (Sandbox Code Playgroud)

如果a key是正确给出的并且是a string,则此函数可以很好地完成其工作.毋庸置疑,如果我了解一个允许相同功能的内置或标准库模块,我很乐意放弃我的小程序,转而采用更标准,更健壮的选择.

你知道这样的实施吗?

- 注意

从这个答案找到以下单行,

[dict(t) for t in set([tuple(d.items()) for d in l])]
Run Code Online (Sandbox Code Playgroud)

聪明,不会工作,因为我必须使用项目作为嵌套dicts.

- 例子

为清楚起见,以下是使用此类例程的示例:

with_duplicates = [
    {
        "type": "users",
        "attributes": {
            "first-name": "John",
            "email": "john.smith@gmail.com",
            "last-name": "Smith",
            "handle": "jsmith"
        },
        "id": "1234"
    },
    {
        "type": "users",
        "attributes": {
            "first-name": "John", …
Run Code Online (Sandbox Code Playgroud)

python duplicates python-3.x

8
推荐指数
1
解决办法
373
查看次数

从词典列表中删除重复项

我有以下词典列表:

d = [
{ 'name': 'test', 'regions': [{'country': 'UK'}] },
{ 'name': 'test', 'regions': [{'country': 'US'}, {'country': 'DE'}] },
{ 'name': 'test 1', 'regions': [{'country': 'UK'}], 'clients': ['1', '2', '5'] },
{ 'name': 'test', 'regions': [{'country': 'UK'}] },
]
Run Code Online (Sandbox Code Playgroud)

从列表中删除重复条目的最简单方法是什么?

我看到了有效的解决方案,但前提是项目没有嵌套的dicts或列表

python dictionary list

6
推荐指数
1
解决办法
6721
查看次数

排序和比较字典列表 Python

我试图找到一种方法来排序和比较 Python 3.6 中的两个字典列表。我最终只是想list_dict_alist_dict_b有比较==和评价True

下面是数据的样子:

list_dict_a = [
{'expiration_date': None, 'identifier_country': None, 'identifier_number': 'Male', 'identifier_type': 'Gender', 'issue_date': None},
{'expiration_date': None, 'identifier_country': 'VE', 'identifier_number': '1234567', 'identifier_type': 'Foo No.', 'issue_date': None}]

list_dict_b = [
{'identifier_country': 'VE', 'expiration_date': None, 'identifier_type': 'Foo No.', 'issue_date': None, 'identifier_number': '1234567'},
{'identifier_country': None, 'expiration_date': None, 'identifier_type': 'Gender', 'issue_date': None, 'identifier_number': 'Male'}]
Run Code Online (Sandbox Code Playgroud)

数据是相同的,但它的顺序不同(我对初始顺序没有任何控制权)。

当我尝试将它们进行比较时,在执行以下操作时会得到一个错误值: print("does this match anything",list_dict_a == list_dict_b)

这甚至可以做到吗?

python sorting dictionary list python-3.x

6
推荐指数
1
解决办法
2873
查看次数

如何在python中创建不可变的字典?

我想subclass dict在python中使子类的所有字典都是不可变的.

我不明白如何__hash__影响不变性,因为在我的理解中它只是表示对象的相等不相等!

那么,可以__hash__用来实现不变性吗?怎么样 ?

更新:

目标是来自API的常见响应可用作dict,必须作为全局变量共享.那么,无论如何都需要完好无损?

python class subclass immutability

4
推荐指数
5
解决办法
5574
查看次数

处理Python字典中的哈希冲突

我有一堆 python 字典,每个字典都包含用户信息,例如:

NewUserDict={'name': 'John', 'age':27}
Run Code Online (Sandbox Code Playgroud)

我将所有这些用户信息字典收集在一个更大的字典容器中,使用每个字典的哈希值作为键(哈希字典?)。

将新的唯一用户添加到字典时,处理哈希冲突的最佳方法是什么?我打算手动将字典与冲突的哈希值进行比较,然后将一些随机数添加到更新的哈希值中,例如:

if new_hash in larger_dictionary:
    if larger_dictionary[new_hash] != NewUserDict:
        new_hash = new_hash + somerandomnumber
Run Code Online (Sandbox Code Playgroud)

处理这个问题的标准方法是什么?或者,我如何知道我是否应该首先担心碰撞?

python hash dictionary

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

Python的两个字典列表的交集

我有2个像dic的列表

list1 = [{'count': 351, 'evt_datetime': datetime.datetime(2015, 10, 23, 8, 45), 'att_value': 'red'},
     {'count': 332, 'evt_datetime': datetime.datetime(2015, 10, 23, 8, 45), 'att_value': 'red'},
     {'count': 336, 'evt_datetime': datetime.datetime(2015, 10, 23, 8, 45), 'att_value': 'red'},
     {'count': 359, 'evt_datetime': datetime.datetime(2015, 10, 23, 8, 45), 'att_value': 'red'},
     {'count': 309, 'evt_datetime': datetime.datetime(2015, 10, 23, 8, 45), 'att_value': 'red'}]


list2 = [{'count': 359, 'evt_datetime': datetime.datetime(2015, 10, 23, 8, 45), 'att_value': 'red'},
     {'count': 351, 'evt_datetime': datetime.datetime(2015, 10, 23, 8, 45), 'att_value': 'red'},
     {'count': 381, 'evt_datetime': datetime.datetime(2015, 10, …
Run Code Online (Sandbox Code Playgroud)

python python-3.4

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

重复散列任意 Python 元组

我正在编写一个专门的单元测试工具,需要保存测试结果以便将来进行比较。因此,我需要能够一致地将传递给每个测试的参数映射到使用每个版本的这些参数运行测试函数的测试结果。我希望有一种方法可以对元组进行散列并使用该散列来命名存储测试结果的文件。

我的第一个冲动只是调用hash()参数元组,但是这当然行不通,因为hash现在在解释器实例之间是随机的。

我很难想出一种适用于元组中可能存在的任意元素的方法(我想将其限制为整数、浮点数、字符串和这三个元素的列表\元组的混合就可以了) 。有任何想法吗?

我想过使用repr元组的或对其进行酸洗,但 repr 不能保证为相同的输入产生逐字节相同的输出,而且我认为酸洗也不是(是吗?)

我已经看到了这一点,但答案都是基于相同的假设,该假设不再成立,并且无论如何也不会真正转化为这个问题,很多讨论都是关于使哈希不依赖于订单项出现,我确实希望哈希取决于顺序。

python hash

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

使用pickle.dumps来散列可变对象

我理解为什么将可变对象放在字典中是危险的.但是,将所有列表/集转换为元组/ frozensets是昂贵的; 对于许多类型,根本没有容易获得的不可变版本.因此,有时可能需要直接对可变对象进行哈希处理,并采取适当的预防措施以确保永远不会修改相关对象.

在我开始为可变对象实现非常复杂的自定义散列函数之前,我想检查使用pickle.dumps散列函数是否有任何缺点- 无论是在性能还是碰撞方面还是其他任何方面.

python hash python-3.x

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

How to hash a dictionary?

I wanted to know if you could hash a dictionary? Currently playing around with Blockchain! Here's the code I would like to hash:

def add_transactions():

    transaction = {

      "previous_block_hash": previous_block_hash() ,

      "index": increase_index(),

      "item": item(),

      "timestamp": datetime.datetime.now(),

      "sender": get_sender(),

      "receiver": get_receiver()

    }
Run Code Online (Sandbox Code Playgroud)

Wanted to know the best way to apply hashlib to get 256 value a4337bc45a8fc544c03f52dc550cd6e1e87021bc896588bd79e901e2

python

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