简体   繁体   English

从可变长度字符串中解析值的最佳方法是什么?

[英]What's the best way to parse values from a variable length string?

Suppose I have a string of integers separated by commas of variable length. 假设我有一个由可变长度的逗号分隔的整数字符串。 What is the best way to split the string and update variables with values if they exist? 拆分字符串并使用值更新变量的最佳方法是什么?

Currently, I have the following. 目前,我有以下几点。

a, b, c = 10, 10, 1    #default values
mylist = [int(x) for x in input.split(',')]
if len(mylist) == 2: a, b = mylist
else: a, b, c = mylist

Is there a more efficient way of doing this? 有更有效的方法吗?

a, b, c = 10, 10, 1    #default values
mylist = [int(x) for x in input.split(',')]
a, b, c = mylist + [a, b, c][len(mylist):]

I think the reason this is ugly is that it's non-Pythonic to treat local variables in aggregate; 我认为这个丑陋的原因是它总是处理局部变量是非Pythonic的; instance members would be more appropriate. 实例成员会更合适。

You could use a helper function: 你可以使用辅助函数:

def f(a=10, b=10, c=1):
    return a, b, c

a, b, c = f(*map(int, input.split()))

This won't be faster – it's just a different way to do it that just crossed my mind. 这不会更快 - 它只是一种不同的方式来做到这一点,只是我的想法。

defaults=[10,10,1]
mylist=[int(x) for x in ipt.split(',')]
defaults[:len(mylist)]=mylist
a,b,c=defaults

This changes defaults though... You to avoid that, something like this would work: 这改变了defaults ...你要避免这种情况,这样的事情会起作用:

defaults=[10,10,1]
mylist=[int(x) for x in ipt.split(',')]
temp_defaults=defaults[:]
temp_defaults[:len(mylist)]=mylist
a,b,c=temp_defaults

Also, be careful using input as a variable name. 另外,请注意使用input作为变量名称。 It's the name of a python built-in so you're removing your easy access to that function. 它是内置python的名称,因此您可以轻松访问该功能。

Use slicing to combine the user input with the list of default arguments: 使用切片将用户输入与默认参数列表组合在一起:

>>> defaults = [10, 10, 1]
>>> user_input = '15 20'
>>> user_ints = map(int, user_input.split())
>>> combined = user_ints + defaults[len(user_ints):]
>>> a, b, c = combined

izip_longest allows to take the length of the default values if longer: 如果更长, izip_longest允许采用默认值的长度:

>>> from itertools import izip_longest
>>> inp = '3, 56'
>>> a, b, c = [i if i else j for i, j in izip_longest([int(x) for x in inp.split(',')], (10, 10, 1))]
>>> a, b, c
(3, 56, 1)
a, b, c = (map(int, user_input.split(',')) + [20,20,10])[:3]

or 要么

def parse(user_input, *args):
    return (map(int, user_input.split(',')) + list(args))[:len(args)]

>>> a, b, c = parse('1,2', 20, 20, 10)
>>> a, b, c
(1, 2, 20)
>>> a, b, c, d = parse('1,2,3,4,5', 0, 0, 0, 0)
>>> a, b, c, d
(1, 2, 3, 4)

暂无
暂无

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

相关问题 在Python中将可变长度字符串拆分为变量的最佳方法是什么? - What is the best way to split a variable length string into variables in Python? 从请求库中解析 JSON 响应的最佳方法是什么? - What's the best way to parse a JSON response from the requests library? 从 Python 中的字符串解析元组的最佳方法是什么? - What is the best way to parse a tuple from a string in Python? 在 Python 中跳过迭代变量的 N 个值的最佳方法是什么? - What's the best way of skip N values of the iteration variable in Python? 处理具有可变长度且还需要翻转字节序的多字节数据字段的最佳方法是什么? - What's the best way to handle multi byte data fields with variable length that also need to have endianness flipped? 从函数返回多个值的最佳方法是什么? - What's the best way to return multiple values from a function? 将字符串拆分为固定长度的块并在 Python 中使用它们的最佳方法是什么? - What's the best way to split a string into fixed length chunks and work with them in Python? 在 Python 中生成特定长度的随机字符串的最佳方法是什么? - What's the best way to generate random strings of a specific length in Python? Python中字符串变量赋值的复杂性是什么? 字符串的常量或长度 - What's the complexity of variable assignment of a string in Python? Constant or length of string 用 pyparsing 解析冒号分隔的字符串的最佳方法是什么 - What is the best way to parse a colon separated string with pyparsing
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM