tuples_通过字典赋值

内容来自:python for information

将items方法、元组赋值与for循环结合起来,你可以拥有一个良好的代码模式,使用单循环就可以遍历字典中的键与值。

for key, val in d.items():
    print val, key

这个循环中存在两个迭代变量。由于items返回一个元组列表,变量key和val通过字典的键值对进行迭代,继而得到赋值。

循环中的每次迭代都会使得key和value被赋予下一个字典键值对(仍然以哈希顺序)。

此循环的输出结果如下:

10 a
22 c
1 b

再次强调,这是哈希键顺序,也就是没有特定顺序。

将两种方法结合,按照每个键值对中的值来排序,输出字典的内容。

要做到这一点,首先创建一个元组列表,其中每个元组为(value, key)。通过items方法得到(key, value)元组列表。此时,我们想要根据value排序,而不是key。(value, key)元组列表一旦生成,排序就变得简单了,按照反向次序对列表排序,输出已排序的新列表。

>>> d = {'a':10, 'b':1, 'c':22}
>>> l = list()
>>> for key, val in d.items() :
...     l.append( (val, key) )
...
>>> l
[(10, 'a'), (22, 'c'), (1, 'b')]
>>> l.sort(reverse=True)
>>> l
[(22, 'c'), (10, 'a'), (1, 'b')]
>>>

创建元组列表时要非常谨慎,确保每个元组的第一个元素是值,这样就能对元组列表进行排序,获得所需的字典内容,该字典已按值进行排序。

 

转载于:https://www.cnblogs.com/fivecats/p/8653712.html

[清洗前] 当前内存使用: 376.12 MB 发现 566 行包含无穷大值,正在清理... 发现 566 个NaN值,使用前向填充处理... 运行过程中出错: incompatible index of inserted column with frame index Traceback (most recent call last): File "D:\Anaconda\Lib\site-packages\pandas\core\frame.py", line 12304, in _reindex_for_setitem reindexed_value = value.reindex(index)._values ^^^^^^^^^^^^^^^^^^^^ File "D:\Anaconda\Lib\site-packages\pandas\core\series.py", line 4981, in reindex return super().reindex( ^^^^^^^^^^^^^^^^ File "D:\Anaconda\Lib\site-packages\pandas\core\generic.py", line 5521, in reindex return self._reindex_axes( ^^^^^^^^^^^^^^^^^^^ File "D:\Anaconda\Lib\site-packages\pandas\core\generic.py", line 5544, in _reindex_axes new_index, indexer = ax.reindex( ^^^^^^^^^^^ File "D:\Anaconda\Lib\site-packages\pandas\core\indexes\base.py", line 4438, in reindex target = self._wrap_reindex_result(target, indexer, preserve_names) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\Anaconda\Lib\site-packages\pandas\core\indexes\multi.py", line 2602, in _wrap_reindex_result target = MultiIndex.from_tuples(target) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\Anaconda\Lib\site-packages\pandas\core\indexes\multi.py", line 220, in new_meth return meth(self_or_cls, *args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\Anaconda\Lib\site-packages\pandas\core\indexes\multi.py", line 615, in from_tuples arrays = list(lib.tuples_to_object_array(tuples).T) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "lib.pyx", line 2983, in pandas._libs.lib.tuples_to_object_array ValueError: Buffer dtype mismatch, expected 'Python object' but got 'long long' The above exception was the direct cause of the following exception: Traceback (most recent call last): File "d:\股票量化数据库\股票量化数据库\untitled7.py", line 863, in run self.perform_clustering(n_clusters=self.config.get('n_clusters', 5)) File "d:\股票量化数据库\股票量化数据库\untitled7.py", line 688, in perform_clustering self.clean_data() File "d:\股票量化数据库\股票量化数据库\untitled7.py", line 566, in clean_data self.data[self.feature_cols] = self.data.groupby('stock_code', observed=True)[self.feature_cols].apply( ~~~~~~~~~^^^^^^^^^^^^^^^^^^^ File "D:\Anaconda\Lib\site-packages\pandas\core\frame.py", line 4079, in __setitem__ self._setitem_array(key, value) File "D:\Anaconda\Lib\site-packages\pandas\core\frame.py", line 4123, in _setitem_array self[k1] = value[k2] ~~~~^^^^ File "D:\Anaconda\Lib\site-packages\pandas\core\frame.py", line 4091, in __setitem__ self._set_item(key, value) File "D:\Anaconda\Lib\site-packages\pandas\core\frame.py", line 4300, in _set_item value, refs = self._sanitize_column(value) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\Anaconda\Lib\site-packages\pandas\core\frame.py", line 5036, in _sanitize_column return _reindex_for_setitem(value, self.index) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\Anaconda\Lib\site-packages\pandas\core\frame.py", line 12311, in _reindex_for_setitem raise TypeError( TypeError: incompatible index of inserted column with frame index
最新发布
08-10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值