开发者

linked list in python [closed]

开发者 https://www.devze.com 2023-03-28 09:51 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form. For help clari
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

there are huge number of data, there are various groups. i want to check whether the new data fits in any group and if it does i want to put that data into that group. If datum doesn't fit to any of the group, i want to create 开发者_运维技巧new group. So, i want to use linked list for the purpose or is there any other way to doing so??

P.S. i have way to check the similarity between data and group representative(lets not go that in deatil for now) but i dont know how to add the data to group (each group may be list) or create new one if required. i guess what i needis linked list implementation in python, isn't it?


This sounds like a perfect use for a dictionary.


I would suggest using a dictionary (or defaultdict), where the key is the group and the value is a list of all data. Here is a simple example:

>>> from collections import defaultdict
>>> get_group = lambda x: x % 4
>>> d = defaultdict(list)
>>> for value in range(10):
...     d[get_group(value)].append(value)
... 
>>> dict(d)
{0: [0, 4, 8], 1: [1, 5, 9], 2: [2, 6], 3: [3, 7]}
0

精彩评论

暂无评论...
验证码 换一张
取 消