通过Lambda X功能改进(缩短)我的列表理解?
!!!我知道我可以在这里使用counter(),但这不是重点!
给出以下代码:
test =[['a',1],['a',2],['b',3], ['c',5],['a',7],['c',9]]
{i:[test[x][0] for x in range(len(test))].count(i) for i in [test[x][0] for x in range(len(test))]}
output: {'a': 3, 'b': 1, 'c': 2}
有没有办法避免在不创建另一个变量的情况下重复序列:
[test[x][0] for x in range(len(test))]
!!! I know I could use Counter() here, but that's not the point !!!
Giving the code below:
test =[['a',1],['a',2],['b',3], ['c',5],['a',7],['c',9]]
{i:[test[x][0] for x in range(len(test))].count(i) for i in [test[x][0] for x in range(len(test))]}
output: {'a': 3, 'b': 1, 'c': 2}
Is there a way to avoid repeating the sequence without creating another variable:
[test[x][0] for x in range(len(test))]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)