Python 获取列表中字符串的最后一部分

Python 获取列表中字符串的最后一部分,python,Python,我有一个包含以下字符串元素的列表 myList=['120$My life cycle 3$121$My daily routine 2'] 我执行.split$操作并获得以下新列表 templast=strmyList.split$ 我希望能够存储此模板列表中的所有整数值,这些数值在拆分后处于偶数索引。我希望返回一个整数列表 Expected output: [120, 121] 更新版本: 为什么不直接使用re呢 re是python中用于常规表达式的库。它们帮助你找到模式 import r

我有一个包含以下字符串元素的列表

myList=['120$My life cycle 3$121$My daily routine 2'] 我执行.split$操作并获得以下新列表

templast=strmyList.split$ 我希望能够存储此模板列表中的所有整数值,这些数值在拆分后处于偶数索引。我希望返回一个整数列表

Expected output: [120, 121]
更新版本:

为什么不直接使用re呢

re是python中用于常规表达式的库。它们帮助你找到模式

import re
myList = ['120$My life cycle 3$121$My daily routine 2']
numbers = re.findall(r'\d+$', myList[0]) 
'''
.finall() returns list of all occurences of a pattern in a given string.
The pattern says map all digits in the string. If they are next to each
other count them as one element in final list. We use index 0 of the 
myList as thats the string we want to work with.
'''
results = list(map(int, numbers)) # this line performs an int() operation on each of the elements of numbers. 
print(results)

你是这样做的吗

a = ['100$My String 1A$290$My String 1B']
>>> for x in a:
...   [int(s) for s in x.split("$") if s.isdigit()]
...
[100, 290] 


您可以按$拆分并使用数字列表:

mylist = ['120$My life cycle$121$My daily routine','some$222$othr$text$42']

# split each thing in mylist at $, for each split-result, keep only those that
# contain only numbers and convert them to integer
splitted = [[int(i) for i in p.split("$") if i.isdigit()] for p in mylist] 

print(splitted) # [[120, 121], [222, 42]]
这将生成一个列表列表,并将字符串数转换为整数。它仅适用于不带符号的正数字符串-使用符号,您可以将isdigit交换为另一个函数:

def isInt(t):
    try:
        _ = int(t)
        return True
    except:
        return False

mylist = ['-120$My life cycle$121$My daily routine','some$222$othr$text$42']
splitted = [[int(i) for i in p.split("$") if isInt(i) ] for p in mylist] 

print(splitted) # [[-120, 121], [222, 42]]
无论myList中有多少字符串,都要获取展开列表:


首先,我们用“$”作为分隔符拆分字符串。然后我们只需迭代新列表中的其他结果,将其转换为整数并将其附加到结果中

myList = ['120$My life cycle 3$121$My daily routine 2']
myList = myList[0].split('$')
results = []
for i in range(0,len(myList),2):
    results.append(int(myList[i]))
print(results)
# [120, 121]

那么,你只想让所有数字都在一个字符串中?为什么要在$?@LauraSmith上拆分,如何应用拆分,是否使用myList[0]。拆分$?@lmiguelvargasf查看我的编辑。我使用str然后拆分它们在0-index处没有120,它由¬$still you have&分隔,介于120和我的生命周期之间。拆分“$”不起作用,将列表转换为str将保留括号和引号char您是否在示例输入上尝试过,是否获得了输出?它似乎不起作用:在字符串中有“&”而不是“$”,所以我只得到[121]作为输出。问题中的字符串有问题吗?那是一个输入错误。更新以反映修复我刚将您的字符串从数组中取出,然后运行此[ints for s in mystr.split$if s.isdigit],它工作了例如,如果我的输入是['100$my string 1A$290$my string 1B',我只得到290与您一起解决方案?您能验证一下吗?@LauraSmith它是否始终是您要提取的3位数字,并且例程后面的数字不会是3位数字?r'\d+$将只在每行末尾查找数字。'$'是一个具有特殊含义的特殊正则表达式字符-您需要对其进行转义。改为使用“r”\d+?:\$\124;$。@Matej Novosad对更改后的用法有更好的回答case@PatrickArtner是的,OP在回答停留期间将问题更改了3次,所以我决定写一个新的问题,因为这个问题不再是我POV中的最佳解决方案。@MatthewGaiser您的答案完全正确,您的测试用例与OP相同。她刚刚编辑了3次D
intlist = list(map(int,( d for d in '$'.join(myList).split("$") if isInt(d))))
print(intlist) # [-120, 121, 222, 42]
myList = ['120$My life cycle 3$121$My daily routine 2']
myList = myList[0].split('$')
results = []
for i in range(0,len(myList),2):
    results.append(int(myList[i]))
print(results)
# [120, 121]