简体   繁体   English

如何将Python列表中的后续数字连接成双(或更多)数字

[英]How to join subsequent digits in a Python list into a double (or more) digit number

I have the following string: 我有以下字符串:

string = 'TAA15=ATT'

I make a list out of it: 我列出了一个清单:

string_list = list(string)
print(string_list)

and the result is: 结果是:

['T', 'A', 'A', '1', '5','=', 'A', 'T', 'T']

I need to detect subsequent digits and join them into a single number, as shown below: 我需要检测后续数字并将它们连接成一个数字,如下所示:

['T', 'A', 'A', '15','=', 'A', 'T', 'T']

I'm also quite concerned with performances. 我也很关心表演。 This string conversion is done thousand times. 这个字符串转换完成了一千次。

Thank you for any hints you can provide. 感谢您提供的任何提示。

Here is a very short solution 这是一个非常简短的解决方案

import re

def digitsMerger(source):
    return re.findall(r'\d+|.', source)
digitsMerger('TAA15=ATT')
['T', 'A', 'A', '15', '=', 'A', 'T', 'T']

Using itertools.groupby 使用itertools.groupby

Ex: 例如:

from itertools import groupby
string = 'TAA15=ATT'

result = []
for k, v in groupby(string, str.isdigit):
    if k:
        result.append("".join(v))
    else:
        result.extend(v)
print(result)

Output: 输出:

['T', 'A', 'A', '15', '=', 'A', 'T', 'T']

Another regexp: 另一个正则表达式:

import re

s = 'TAA15=ATT'

pattern = r'\d+|\D'

m = re.findall(pattern, s)

print(m)

You can use regular expressions, in Python the library re : 你可以在Python中使用正则表达式re

import re
string = 'TAA15=ATT'
num = re.sub('[^0-9,]', "", string)
pos = string.find(num)
str2 = re.sub('\\d+',"", string)
str2 = re.sub('=',"", str2)
print(str2)
l = list()
for el in str2:
    l.append(el)
l.insert(pos, num)
print(l)

Basically re.sub('[^0-9,]', "", string) is telling: take the string, match all the characters that are not ( ^ means negation) numbers ( 0-9 ) and substitute them with the second parameter, ie., an empty string. 基本上re.sub('[^0-9,]', "", string)告诉:取字符串,匹配所有不是( ^表示否定)数字( 0-9 )的字符并用它们替换它们第二个参数,即一个空字符串。 So basically what's left are only digits that you have to convert to an integer. 所以基本上剩下的只是你必须转换为整数的数字。

If the = is always after the digit instead of 如果=总是在数字之后而不是

str2 = re.sub('\\d+',"", string)
str2 = re.sub('=',"", str2)

you can do 你可以做

str2 = re.sub('\\d+=',"", string)

You can create a function that compares the last value seen and the next and use functools.reduce : 您可以创建一个比较上次看到的值和下一个值的函数,并使用functools.reduce

from functools import reduce

string_list = ['T', 'A', 'A', '1', '5', 'A', 'T', 'T']

def combine_nums(lst, nxt):
    if lst and all(map(str.isdigit, (lst[-1], nxt))):
        nxt = lst[-1] + nxt
    return lst + [nxt]

print(reduce(combine_nums, string_list, [])

Results: 结果:

['T', 'A', 'A', '1', '15', 'A', 'T', 'T']

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在python中,如何将一位数转换为两位数字符串? - in python how do I convert a single digit number into a double digits string? 我怎样才能得到与个位数一致的两位数的数字坐标列的最后一位 - How can i get the number coordinate column last digit of the double digits in line with the single digits Python - 5位随机数发生器,没有重复数字 - Python - 5 digit random number generator with no repeating digits 如何形成给定数字的所有可能组合以形成不重复数字的n位数字?(python) - How to form all possible combination of given digits to form a n-digit number without repetition of digits?(python) 如何使用 python 将两位数分隔为两位数? 例如 15 -> 1, 5 - How can I separate a double digit to two single digits with python? e.g. 15 -> 1, 5 Python 3:如何获得一个随机的4位长数字,该数字内没有重复的数字? - Python 3: How to get a random 4 digit long number with no repeated digits inside that number? 如何拆分> 1位整数列表并从该数字创建列表? 蟒蛇 - How to split >1digit integers list and create a list from that digits? Python 如何在python中反复查找每个T数的数字总和的数字总和,直到成为单个数字? - how to find the sum of digits of sum of digits of each of T numbers repeatedly in python till it gets to being a single digit number? Function 用于列表中的拆分数字和每个数字的唯一编号/代码 - Function for split digits in list and unique number/code for every digit 我如何使用所有数字而不是python中的第一位对列表进行排序? - how do i sort the list using all digits not just first digit in python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM