如何给出从 0 到随机给定数字的唯一整数 ID?
How to give unique integer id starting from 0 to random given number?
我想在Python中实现的内容:
# 1. define int_factory
# 2. get new id of value 123 --> give 0 to 123
a = int_factory[123]
print(a) # = 0
# 3. get new id of value 12324 --> give 1 to 12324
a = int_factory[12324]
print(a) # = 1
# 4. Hey, what's 123's id?
a = int_factory[123]
print(a) # = 0
# 5. get new id of value 513 --> give next id, which is 2 to 513
a = int_factory[513]
print(a) # = 2
我想使用字典数据类型或 Python 中的 defaultdict
来实现它。
我尝试了什么:
In [2]: def func():
...: for i in range(10**15):
...: yield i
...:
In [3]: a = defaultdict(func)
In [4]: next(a[123])
但这行不通。
在 Python 中是否有更优雅的实现方式?
您只需重写 __missing__
方法,即可实现使用递增计数器的功能
class owndd(defaultdict):
def __init__(self):
super().__init__()
self.counter = 0
def default_factory(self):
self.counter += 1
return self.counter - 1
def __missing__(self, key):
self[key] = value = self.default_factory()
return value
然后使用
int_factory = owndd()
a = int_factory[123]
print(a) # = 0
a = int_factory[12324]
print(a) # = 1
a = int_factory[123]
print(a) # = 0
a = int_factory[513]
print(a) # = 2
for x in 'abcdef':
print(x, int_factory[x])
0
1
0
2
a 3
b 4
c 5
d 6
e 7
f 8
我会按照以下方式进行:
import collections
import itertools
cnt = itertools.count()
int_factory = collections.defaultdict(cnt.__next__)
print(int_factory[123])
print(int_factory[12324])
print(int_factory[123])
print(int_factory[513])
输出:
0
1
0
2
说明:您需要在创建 collections.default_dict
时传递可调用对象,这将 return
缺少键
所需的值
如果你想保持简单,你也可以自己做。
class X:
def __init__(self):
self.dict = []
def __getitem__(self, arg):
for i in range(len(self.dict)):
if self.dict[i] == arg:
return i
self.dict.append(arg)
return len(self.dict)-1
int_factory = X()
a = int_factory[123]
print(a)
a = int_factory[12324]
print(a)
a = int_factory[123]
print(a)
a = int_factory[513]
print(a)
我想在Python中实现的内容:
# 1. define int_factory
# 2. get new id of value 123 --> give 0 to 123
a = int_factory[123]
print(a) # = 0
# 3. get new id of value 12324 --> give 1 to 12324
a = int_factory[12324]
print(a) # = 1
# 4. Hey, what's 123's id?
a = int_factory[123]
print(a) # = 0
# 5. get new id of value 513 --> give next id, which is 2 to 513
a = int_factory[513]
print(a) # = 2
我想使用字典数据类型或 Python 中的 defaultdict
来实现它。
我尝试了什么:
In [2]: def func():
...: for i in range(10**15):
...: yield i
...:
In [3]: a = defaultdict(func)
In [4]: next(a[123])
但这行不通。
在 Python 中是否有更优雅的实现方式?
您只需重写 __missing__
方法,即可实现使用递增计数器的功能
class owndd(defaultdict):
def __init__(self):
super().__init__()
self.counter = 0
def default_factory(self):
self.counter += 1
return self.counter - 1
def __missing__(self, key):
self[key] = value = self.default_factory()
return value
然后使用
int_factory = owndd()
a = int_factory[123]
print(a) # = 0
a = int_factory[12324]
print(a) # = 1
a = int_factory[123]
print(a) # = 0
a = int_factory[513]
print(a) # = 2
for x in 'abcdef':
print(x, int_factory[x])
0
1
0
2
a 3
b 4
c 5
d 6
e 7
f 8
我会按照以下方式进行:
import collections
import itertools
cnt = itertools.count()
int_factory = collections.defaultdict(cnt.__next__)
print(int_factory[123])
print(int_factory[12324])
print(int_factory[123])
print(int_factory[513])
输出:
0
1
0
2
说明:您需要在创建 collections.default_dict
时传递可调用对象,这将 return
缺少键
如果你想保持简单,你也可以自己做。
class X:
def __init__(self):
self.dict = []
def __getitem__(self, arg):
for i in range(len(self.dict)):
if self.dict[i] == arg:
return i
self.dict.append(arg)
return len(self.dict)-1
int_factory = X()
a = int_factory[123]
print(a)
a = int_factory[12324]
print(a)
a = int_factory[123]
print(a)
a = int_factory[513]
print(a)