jackfrued / jackfrued/Python-100-Days
集合排序的问题
Nobody has claimed this yet.
- Dominant language
- Jupyter Notebook
- Stars
- 187k
- Forks
- 55.8k
- PR merge metrics
- No merged PRs in 30d
Description
在练习集合的时候,发现一个无法理解也无法解决的集合内元素的排序问题,望有大神帮忙解释一下
我从一个顺序的集合,通过平方的关系生成一个新的集合,为什么新集合的元素排序和原来的集合毫无关系?
代码如下
set1 = set(range(1, 10))
print(set1)
set2 = set()
for elem in set1:
print(elem)
set2.add(elem ** 2)
print(set2)
执行结果如下
{1, 2, 3, 4, 5, 6, 7, 8, 9}
1
{1}
2
{1, 4}
3
{1, 4, 9}
4
{16, 1, 4, 9}
5
{1, 4, 9, 16, 25}
6
{1, 4, 36, 9, 16, 25}
7
{1, 4, 36, 9, 16, 49, 25}
8
{64, 1, 4, 36, 9, 16, 49, 25}
9
{64, 1, 4, 36, 9, 16, 49, 81, 25}
可以看到即使set1是有序的,在生成set2的过程中,新元素的位置完全是随机的,这是什么原因?
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
No file or test is named. Start by reviewing the Python set documentation and the repository’s relevant lesson or explanatory material, then determine where this behavior should be explained; done means adding a clear explanation of set ordering and updating any relevant example or documentation.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- documentation
- Issue type
- Documentation
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 35/100