python:将字符串解压缩到列表

编程入门 行业动态 更新时间:2024-10-19 11:37:01
本文介绍了python:将字符串解压缩到列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

关于配置文件中多值元素的问题的答案(这完全适合我需求)建议从配置中解压缩字符串".我阅读了解压缩参数列表的文档但是我不明白这与我的问题有什么关系.

The answer to a question on multiple-value elements in a config file (which exactly fits my needs) suggests to "unpack the string from the config". I read the doc for unpacking arguments lists suggested in several places but I fail to understand how this relates to my problem.

我确信这很明显:拥有一个字符串str = "123,456",如何将其转换为列表[123,456](字符串中用逗号分隔的元素数量可能会有所不同)

I am sure this must be obvious: having a string str = "123,456", how can I transform it into the list [123,456] (the number of elements separated by a comma in the string may vary)

谢谢.

推荐答案

您要使用字符串列表还是整数列表?

Do you want a list of strings or a list of ints?

如果只需要一个字符串列表,这很简单:

If you just want a list of strings, it's very simple:

my_list = my_string.split(',')

如果要将这些转换为整数,则需要:

If you want to convert these to ints, you need:

my_list = map(int, my_string.split(','))

(此外,不要使用str作为变量名,因为它会遮盖内置的str()函数.)

(Also, don't use str as a variable name as it shadows the built-in str() function.)

更多推荐

python:将字符串解压缩到列表

本文发布于:2023-11-05 08:11:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1560322.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:解压缩   字符串   列表   python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!