大家好,我是皮皮。
前几天在Python最强王者交流群【Chloe】问了一道Python
处理的问题,如下图所示。
原始数据如下:
f('Both of these issues are fixed by postponing the evaluation of annotations. Instead of compiling code which executes expressions in annotations at their definition time, the compiler stores the annotation in a string form equivalent to the AST of the expression in question. If needed, annotations can be resolved at runtime using typing.get_type_hints(). In the common case where this is not required, the annotations are cheaper to store (since short strings are interned by the interpreter) and make startup time faster.')
怎么按照values排序? 并且取倒数第二个值?
这里大家给出一个思路,如下所示:
下次遇到这种词频的需求,都可以考虑使用Counter
来实现,事半功倍。
这里【月神】给了一份示例代码,如下所示:
from collections import Counter
c = Counter('lost lost an apple apple '.split())
c.most_common(2).pop()[0]
运行之后,结果如下图所示:
后来【瑜亮老师】给了一份具体的代码,如下所示:
from collections import Counter
ss = 'Both of these issues are fixed by postponing the evaluation of annotations. Instead of compiling code which executes expressions in annotations at their definition time, the compiler stores the annotation in a string form equivalent to the AST of the expression in question. If needed, annotations can be resolved at runtime using typing.get_type_hints(). In the common case where this is not required, the annotations are cheaper to store (since short strings are interned by the interpreter) and make startup time faster.'
data = Counter(ss.split())
print(data)
result = data.most_common(2)[1][0]
print(result)
这个most_common()
函数完美地解决了粉丝的问题!
后来【Chloe】自己也提供了一个方法,也是可行的,条条大路通罗马。
def f(s):
d = {}
for i in s.split():
if i in d:
d[i] += 1
else:
d[i] = 1
# print(d)
return d
result = f('Both of these issues are fixed by postponing the evaluation of annotations. Instead of compiling code which executes expressions in annotations at their definition time, the compiler stores the annotation in a string form equivalent to the AST of the expression in question. If needed, annotations can be resolved at runtime using typing.get_type_hints(). In the common case where this is not required, the annotations are cheaper to store (since short strings are interned by the interpreter) and make startup time faster.')
print(sorted(zip(result.values(), result.keys()), reverse=True)[1])
for data in sorted(zip(result.values(), result.keys()), reverse=True):
print(data)
运行结果如下图所示:
她还提供了另外一个方法,如下所示:
def f(s):
d = {}
for i in s.split():
if i in d:
d[i] += 1
else:
d[i] = 1
d = {v: k for k, v in d.items()}
print(d.items())
d = sorted(d.items())
print(d)
print(d[-2])
f(
'Both of these issues are fixed by postponing the evaluation of annotations. Instead of compiling code which executes expressions in annotations at their definition time, the compiler stores the annotation in a string form equivalent to the AST of the expression in question. If needed, annotations can be resolved at runtime using typing.get_type_hints(). In the common case where this is not required, the annotations are cheaper to store (since short strings are interned by the interpreter) and make startup time faster.')
运行结果如下图所示:
大家好,我是皮皮。这篇文章主要盘点了一道使用Python
处理数据的问题,文中针对该问题给出了具体的解析和代码实现,帮助粉丝顺利解决了问题。
最后感谢粉丝【Chloe】提问,感谢【月神】、【瑜亮老师】给出的思路和代码解析,感谢【dcpeng】、【冯诚】、【老松鼠】等人参与学习交流。