简体   繁体   English

将列表项中的字母替换为数字

[英]Replace letters in list item to numbers

I am new here and also fairly new to programming.我是新来的,对编程也很陌生。 I recently started learning Python to learn how to automate processes in my everyday tasks.我最近开始学习 Python 来学习如何在我的日常任务中实现流程自动化。

I am working with comparing 2 lists that I created from converting 2 columns from 2 Excel files into lists.我正在比较通过将 2 个 Excel 文件中的 2 列转换为列表而创建的 2 个列表。 The lists mainly contain numbers, but some items contain both integers and letters, and this I believe results in becoming a string rather than an integer.列表主要包含数字,但有些项目同时包含整数和字母,我相信这会导致成为字符串而不是 integer。 I would like to convert the letters into numbers to have a list of integers to be able to manipulate, compare, etc. Is there any way I can do this?我想将字母转换为数字,以获得能够操作、比较等的整数列表。有什么办法可以做到这一点吗? I have been using openpyxl to access my Excel files.我一直在使用 openpyxl 来访问我的 Excel 文件。 Below is an example of what I would like to do.下面是我想做的一个例子。

Ex: input例如:输入

list1 = [8635, 6227, '8651FRT', '8651BK','8295INSERT', 8295]

output output

newlist1 = [8635, 6227, 865101, 865102,829503, 8295]

I would like to replace 'FRT' with 01, 'BK' with 02, and 'INSERT' with 03. I would really appreciate the help.我想用 01 替换“FRT”,用 02 替换“BK”,用 03 替换“INSERT”。非常感谢您的帮助。 Thanks.谢谢。

one way, is to define a dict with values to replace followed by re.sub to extract & replace the values.一种方法是定义一个带有要替换的值的dict ,然后是re.sub以提取和替换这些值。

import re

replace = {"FRT": "01", 'BK': "02", 'INSERT': "03"}

list1 = [8635, 6227, '8651FRT', '8651BK', '8295INSERT', 8295]

print(
    [int(re.sub(r"([A-Za-z]+)",
            lambda x: replace.get(x.group(1), ""), str(x))) for x in list1]
)
[8635, 6227, 865101, 865102, 829503, 8295]

You can use:您可以使用:

newlist1 = [int(str(item).replace('FRT', '01').replace('BK', '02').replace('INSERT','03')) for item in list1]

All the information needed was found using help(str) if you want more.如果您需要更多信息,请使用help(str)找到所需的所有信息。

You can write a function to replace one item (I can't see an easy shortcut for this), then you can loop over it in a list comprehension.你可以写一个 function 来替换一个项目(我看不到一个简单的快捷方式),然后你可以在列表理解中循环它。

import re

mappings = {'FRT': '01', 'BK': '02', 'INSERT': '03'}

def replace(val):
    
    if isinstance(val, int):
        return val
    
    m = re.match('(\d+)(\D+)$', val)
    if m:
        key = m.group(2)
        if key in mappings:
            return int(m.group(1) + mappings[key])

    raise ValueError('could not do replacement for {}'.format(val))


list1 = [8635, 6227, '8651FRT', '8651BK','8295INSERT', 8295]

print([replace(x) for x in list1])

gives:给出:

[8635, 6227, 865101, 865102, 829503, 8295]

Its not the best example.它不是最好的例子。 You could do it faster and better.你可以做得更快更好。 But I guess this is understandable how you could aproach it.但我想这是可以理解的,你如何接近它。 :) :)

list1 = [8635, 6227, '8651FRT', '8651BK','8295INSERT', 8295]
newlist = []
# Check every item in list
for item in list1:
  # If its a integer, add to newlist
  if isinstance(item, int):
    newlist.append(item)
    continue
  # Else check and replace
  if 'FRT' in item:
    item = item.replace('FRT', '01')


 # Check other exception here
  
   newlist.append(int(item))

You can iterate over your list of values.您可以遍历您的值列表。 When you come across a non-integer number, you can strip off the letters, and append an incrementing counter to it.当你遇到一个非整数时,你可以去掉字母,append 是一个递增计数器。 You then add this counter to a mapping dict to make sure you use the same value each time.然后将此计数器添加到映射字典中,以确保每次都使用相同的值。 This shouldn't require any pre-work or mapping.这不应该需要任何前期工作或映射。 This does assume the letters are at the end or are after your numbers.这确实假设字母在末尾或在您的数字之后。

import re
list1 = [8635, 6227, '8651FRT', '8651BK','8295INSERT', 8295,8295 , '8295INSERT', '8651BK' , '8295INSERT', '8651BK',6227 , ]
mapping = {}
counter_ = 1
output_list = []
for x in list1:
    if isinstance(x, int):
        output_list.append(x)
        pass
    else:
        output = re.split(r'(\d+)', x)
        try:
            val = mapping[output[2]]
            output[2] = val
            output_list.append(''.join(output[1:]))
        except:
            mapping[output[2]] = '0'+str(counter_)
            output[2] = '0'+str(counter_)
            output_list.append(''.join(output[1:]))
        counter_ += 1

newlist = [int(x) for x in output_list]

You can replace the string with the count of a variable.您可以将字符串替换为变量的计数。 Maybe you are looking for this.也许你正在寻找这个。

list1 = [8635, 6227, '8651FRT', '8651BK','8295INSERT', 8295]
mod = []
c = 1

for val in list1:
    if (str(val).isnumeric()): mod.append(val)
    else:
        n = len(val)
        for i in range(n):
            if (not val[i].isnumeric()):
                if (c<10): 
                    mod.append(int(val[0:i]+'0'+str(c)))
                    c+=1
                    break
                else: 
                    mod.append(int(val[0:i]+str(c)))
                    c+=1
                    break

print(mod)

Thank you谢谢

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM