Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to split strings into dictionaries in Python

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

In this issue, the editor will bring you about how to split strings into the dictionary in Python. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

1.1 split strings into dictionaries

#! / usr/bin/python str = "key1=value1;key2=value2;key3=value3" d = dict (x.split ("=") for x in str.split (";")) for k, v in d.items (): print (k, v)

Output quantity

Key1 value1key2 value2key3 value3

1.2 convert two lists into dictionaries

#! / usr/bin/python str1 = "key1 | key2 | key3" str2 = "value1 | value2 | value3" keys = str1.split ("|") values = str2.split ("|") d = {} for k in keys: K = k.strip () # key trim for v in values: d [k] = v.strip () # value trim for k, v in d.items (): print (k, v)

Output quantity

Key1 value1key2 value2key3 value3

1.3 zip example

#! / usr/bin/pythonstr1 = "key1 | key2 | key3" str2 = "value1 | value2 | value3" keys = str1.split ("|") values = str2.split ("|") d = dict (zip (keys, values) for k, v in d.items (): print (k, v)

Output quantity

Key1 value1key2 value2key3 value3

The above is how to split the string into the dictionary in the Python shared by the editor. If you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 283

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report