简体   繁体   English

将拆分值分配给多个变量

[英]Assign split values to multiple variables

I am currently writing a Python script to handle some logs and reformat certain parts.我目前正在编写一个 Python 脚本来处理一些日志并重新格式化某些部分。 Part of the script makes use of the following code (as an example):脚本的一部分使用了以下代码(作为示例):

var1,var2,var3=foo.split("|")

Which works fine.哪个工作正常。 However this perhaps doesn't look so nice (taking away Python's readability factor) if there are 39 values, for example:然而,如果有 39 个值,这可能看起来不太好(去掉 Python 的可读性因素),例如:

var1,var2,var3,...,var39=foo.split("|")

Is there are a better way to format this structure?有没有更好的方法来格式化这个结构?

You can assign to different variables.您可以分配给不同的变量。 Like in Perl, you just need to define them in an array, so assignation is done by matching position of variable and result.就像在 Perl 中一样,您只需要将它们定义在一个数组中,因此分配是通过匹配变量和结果的位置来完成的。

Here is something I tried in interactive python:这是我在交互式python中尝试过的东西:

>>> # this is a grep result, btw
... foo = 'config/some.conf:12:   title = "Super Me"'
>>> [ filename, line, text ] = foo.split(':')
>>> print text
   title = "Super Me"

I do like this rather than a dictionary or an array, especially when working in a for loop.我确实喜欢这个而不是字典或数组,尤其是在for循环中工作时。 It makes variable names more meaningful, even if local to the loop, or temporary.它使变量名称更有意义,即使是循环本地的或临时的。

Edit编辑
second edit to integrate codeforester's notes (Thanks).第二次编辑以整合 codeforester 的笔记(谢谢)。

To avoid searching for variables usage, unwanted values can be dummied to clearly state it will not be used.为避免搜索变量使用情况,可以使用不需要的值来明确说明不会使用。 Dummy variables are expected as _ by python linter python linter 期望虚拟变量为_

>>> [ _, line, text ] = foo.split(':')

If you are not sure about the tokens quantity, use the extended iterable如果您不确定代币数量,请使用扩展的可迭代对象

>>> [ filename, line, text, *_ ] = foo.split(':')

If you don't need the List properties with your variables, you can just remove the square brackets (variables are then managed as a tuple).如果您的变量不需要 List 属性,您可以删除方括号(变量然后作为元组进行管理)。

End of edit编辑结束

Readability for the win !可读性为赢!

lst = foo.split("|")
lst[0]
lst[1]
...

you can use a dictionary:你可以使用字典:

In [29]: strs="foo|bar|spam|eggs"

In [31]: d=dict(("var{0}".format(i),x) for i,x in enumerate(strs.split("|")))

In [32]: d
Out[32]: {'var0': 'foo', 'var1': 'bar', 'var2': 'spam', 'var3': 'eggs'}

In [33]: d['var1']
Out[33]: 'bar'

In [34]: d['var2']
Out[34]: 'spam'

Use a list to store the tokens obtained: -使用list来存储获得的令牌:-

>>> my_str = "Python|Splitting|the|string"
>>> my_tokens = my_str.split("|")
>>>
>>> my_tokens
['Python', 'Splitting', 'the', 'string']
>>> my_token[0]
'Python'

This might be helpful for you:这可能对您有帮助:

strings = "python,splitting,the,string"

var1,var2,var3,var4 = [str(i) for i in strings.split(",")]

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

相关问题 如何将字符串拆分为多个变量,如果值不够,则将变量分配为特定值? - How to split a string into multiple variables & if not enough values, assign the variable as a particular value? 如何给一个function中的多个变量赋多个值? - How to assign multiple values in multiple variables in a function? 如何为多个变量分配任意值 - how to assign arbitrary values to multiple variables 将多个值分配给同一类的子类中的变量 - assign multiple values to variables in a subclass of the same class 通过分隔符拆分列中的值并将值分配给 Pandas dataframe 中的多个列 - Split values in a column by delimiter and assign value to multiple columns in Pandas dataframe 从templatetag返回多个值后,在with语句中分配多个变量 - Assign multiple variables in a with statement after returning multiple values from a templatetag 将输出值分配给变量 - Assign output values to variables 是否可以使用循环将多个变量分配给python中的不同值 - Is it possible to use a loop to assign multiple variables to different values in python 编写拆分函数和分配变量的最短方法? - Shortest way to to write split function and assign variables? 将列表拆分为 N 个子列表并分配给 n 个变量 - split list to N sublist and assign to n variables
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM