获取字符串中的整数值,该字符串可能不总是包含数字。

huangapple go评论58阅读模式
英文:

How to get the integer value out of a string that might not always contain a number

问题

以下是您提供的代码的翻译部分:

# 我有一个列表推导式,现在包含以下滑稽的列表推导式。
[int([y or 0 for y in [" ".join(re.findall(r'[0-9*]', x))]][0]) for x in [acc['id_number']]][0]

# Python中的'int'函数效果不是很好。

# 问题是我希望以更干净的方式从'acc['id_number']'中获取一个整数。 代码运行正常。

# 我想将数据插入SQL数据库,因此我希望'acc['id_number']'严格为整数。

# 'id_number'可以是'1234','na','gh1234'。我希望从这些字符串中获取数字,并且在字符串不包含数字的情况下为0,例如'na'的情况。

# 如果'int'函数能够处理字符串,例如'gh000'甚至'',那么这行代码就不会那么长。

import re

raw_accounts = [
    {'account_name': "someone", 'id_number': "2432"},
    {'account_name': "another person", 'id_number': "na"},
    {'account_name': "some other person", 'id_number': "gh134"},
    {'account_name': "another one", 'id_number': "24124"}
]

data = [
    (
        acc['account_name'],
        [int([y or 0 for y in [" ".join(re.findall(r'[0-9*]', x))]][0]) for x in [acc['id_number']]][0]
    ) for acc in raw_accounts
]

print(data)

请注意,我已将HTML编码转义符(如"')还原为常规引号以进行更好的代码理解。

英文:

I have a list comprehension which now contain this hillarious list comprehension below.

[int([y or 0 for y in ["".join(re.findall(r'[0-9*]', x))]][0]) for x in [acc['id_number']]][0]

The int function in python is not that effective.

The issue is I wish there is a cleaner way to get an integer out of the acc['id_number']. The code works fine.

I want to insert the data in an SQL database so I want the acc['id_number'] to be strictly an integer.

id_number can be '1234', 'na', 'gh1234'. I want the number out of these strings and 0 where the string contains no number as in the case of 'na'

If the int function worked on the strings as 'gh000' or even ''. This line wouldn't get that long.

import re

raw_accounts = [
    {'account_name': "someone", 'id_number': "2432"},
    {'account_name': "another person", 'id_number': "na"},
    {'account_name': "some other person", 'id_number': "gh134"},
    {'account_name': "another one", 'id_number': "24124"}
]


data = [
    (
        acc['account_name'],
        [int([y or 0 for y in ["".join(re.findall(r'[0-9*]', x))]][0]) for x in [acc['id_number']]][0]
    ) for acc in raw_accounts
]

print(data)

答案1

得分: 3

不需要列表理解。使用正则表达式从字符串中提取整数。如果不匹配,则使用0。

regex = re.compile(r'\d+')
data = [
    (
        acc['account_name'],
        int(match.group()) if (match := regex.search(acc['id_number'])) else 0
        
    ) for acc in raw_accounts
]
英文:

There's no need for the list comprehension. Use a regexp to extract the integer from the string. If it doesn't match, use 0.

regex = re.compile(r'\d+')
data = [
    (
        acc['account_name'],
        int(match.group()) if (match := regex.search(acc['id_number'])) else 0
        
    ) for acc in raw_accounts
]

答案2

得分: 1

一个选项:

数据 = [
(
acc['account_name'],
int('0' + ''.join(filter(str.isdigit, acc['id_number'])))
) for acc in raw_accounts
]



<details>
<summary>英文:</summary>

One option:

data = [
(
acc['account_name'],
int('0' + ''.join(filter(str.isdigit, acc['id_number']))
) for acc in raw_accounts
]




</details>



huangapple
  • 本文由 发表于 2023年5月26日 01:03:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76334702.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定