gpt4 book ai didi

python - 如果有 4 个字符串元素的列表。几个元素是 - 分隔的字符串。如果我只想打印 - 分隔词的一部分

转载 作者:行者123 更新时间:2023-12-05 04:23:43 25 4
gpt4 key购买 nike

list2 = ['BIA-660', 'Web', 'Analytics']

问题:如何从list2中提取“660”

list2 = ['BIA-660', 'Web', 'Analytics']
c=list2[0]
x=c.split("-")
print(x[1])

我得到了答案,但想知道是否有另一种更有效的方法来实现该解决方案。

最佳答案

您可以使用正则表达式从字符串中获取任何数字

import re

list2 = ['BIA-660', 'Web', 'Analytics']

for strg in list2:
found = re.findall('\d+', strg)
if found != []:
for num in found:
print(num)

输出:

660

关于python - 如果有 4 个字符串元素的列表。几个元素是 - 分隔的字符串。如果我只想打印 - 分隔词的一部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73665132/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com