How to write these two lines of Python code into one line of code?

thumbnail

Hello everyone, I'm Pippi.

I. Introduction

A few days ago, I asked a Python question in the Python strongest king exchange group [Liu Suqin]. The screenshot of the question is as follows:

The idea is still very good.

Second, the realization process

Here [dcpeng] gives a code, but it is still a bit problematic.

Based on the idea of ​​​​a line of code, and later opportunistically, use the following code to implement:

infos = {}

sz_list, ts_list = [dict(i).get('sz') for i in infos], [dict(i).get('ts') for i in infos]

You can get the expected effect.

Later he raised a new requirement as follows:

In fact, what he wrote is very close, and it came out after a little modification. The code is as follows:

infos = {'instId': 'BTC-USDT', 'side': 'buy', 'sz': '0.0005143', 'px': '22360.4', 'tradeId': '371566232', 'ts': '1663046320050'}

info_dict={k:v for k, v in infos.items() if k=='sz' or k=='ts'}

print(info_dict)

Solved the problem so smoothly.

  1. Summary ==========

Hello everyone, I'm Pippi. This article mainly takes stock of a Python-based problem. In response to this problem, the article gives specific analysis and code implementation to help fans solve the problem smoothly.

Finally, thanks to fans [Liu Suqin] for asking questions, [Jie], [DIY], [dcpeng] for their ideas and code analysis, and [dcpeng], [Zheng Yuzhe·Xiaopang] and others for participating in the study and exchange.

Latest Programming News and Information | GeekBar

Related Posts