简体   繁体   English

如何在Python中正确分割特定的字符串

[英]How to properly split a specific string in Python

I have a list of string values that represents the id.age of some users: 我有一个表示某些用户的id.age字符串列表

users = ["1.20", "2.35", "3", "4", "5.", "6.30", "7."]

How can I properly split it to get the id and age separately? 如何正确分割它以分别获得idage

Note that we have some data with the age information missing (eg "3" and "4" ), and even worse, we have some data only with an id and a point (eg "5." and "7." ). 请注意,我们有一些缺少age信息的数据(例如"3""4" ),更糟糕的是,我们有一些仅包含id和点的数据(例如"5.""7." )。

Sure I can use the split function, for example: 当然可以使用split函数,例如:

>>> "1.2".split('.')
['1', '2']
>>> "2".split('.')
['2']
>>> "3.".split('.')
['3', '']

But, then I will need to check each result. 但是,那么我将需要检查每个结果。 Maybe, something like this: 也许是这样的:

res = "3.".split('.')
id = int(res[0])
if len(res) > 1:
    if res[1] != "":
        age = int(res[1])

Another option is to use the rpartition function, for example: 另一种选择是使用rpartition函数,例如:

>>> "1.2".rpartition('.')
('1', '.', '2')
>>> "2".rpartition('.')
('', '', '2')
>>> "3.".rpartition('.')
('3', '.', '')

But I still need to check the results 'manually' and, in the second example, the value that should be the id is in the age position. 但是我仍然需要“手动”检查结果,在第二个示例中,应该作为id值在age位置。 (eg ('', '', '2') ). (例如('', '', '2') )。

Is there a built in function that I can get the result like this? 是否有内置函数可以得到这样的结果?

>>> "1.2".some_split_function('.')
('1', '.', '2')
>>> "2".some_split_function('.')
('2', None, None)
>>> "3.".some_split_function('.')
('3', '.', None)

So I can just call it in a loop like this: 所以我可以像这样在循环中调用它:

for user_info in users:
    id, _, age = user_info.some_split_function('.')
    print int(id)
    if age is not None:
        print int(age)

Yup, you just use partition instead of rpartition . 是的,您只使用partition而不是rpartition

for user_info in users:
    id, _, age = user_info.partition('.')
    if age.isdigit():
        print int(age)

You'll want to change that conditional from being None to just checking if you've pulled out a number appropriately. 您需要将该条件从“ None更改为仅检查是否已正确提取数字。 This will take care of empty strings etc... 这将解决空字符串等问题。

In general though, the way to avoid this problem is to not structure your data like that in the first place. 通常,避免此问题的方法是首先不要像这样构造数据。

Seeing some of the other answers, no reason to do anything so complex. 看到其他一些答案,没有理由做那么复杂的事情。 If you want a functional solution that maps id to age, then I would advocate for something like this: 如果您想要一个将id映射到age的功能解决方案,那么我建议使用以下方法:

>>> {id: age or None for id, _, age in [user.partition(".") for user in users]}
{'1': '20', '3': None, '2': '35', '5': None, '4': None, '7': None, '6': '30'}

Try the following, we split u only if it contains . 请尝试以下操作,我们仅在包含时才拆分u . , if not, u is the id and age is assigned None . 如果不是, u是id和年龄被分配None

users = ["1.20", "2.35", "3", "4", "5.", "6.30", "7."]
data = []

for u in users:
    id, age = u.split('.') if '.' in u else [u, None]
    age = None if age == '' else age
    data.append({id: age})

If you want your ids to be integers, just call int() function on id like this: 如果您想让id为整数,只需对id调用int()函数,如下所示:

data.append({int(id): age})

Output: 输出:

>>> data
[{'1': '20'}, {'2': '35'}, {'3': None}, {'4': None}, {'5': None}, {'6': '30'}, {'7': None}]

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

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