更清洁的文字处理成语? [英] Cleaner idiom for text processing?
问题描述
我有一些数据文件,其中的行以空格分隔< name> < value>
格式。每行有多个名称 - 值对。
是否有比以下更清晰的习语用于读取每一行
一个关联数组用于按名称访问值?
$ inf in line in infile:
tokens = line.split()
dict = {}
for i in range(0,len(tokens),2)dict [tokens [i]] = tokens [i + 1]
do_something_with_values(dict [''foo] ''],dict [''bar''])
谢谢!
Mike Ellis
Hi,
I have some data files with lines in space-delimited <name> <value>
format. There are multiple name-value pairs per line.
Is there a cleaner idiom than the following for reading each line into
an associative array for the purpose of accessing values by name?
for line in infile:
tokens = line.split()
dict = {}
for i in range(0, len(tokens),2) dict[tokens[i]] = tokens[i+1]
do_something_with_values(dict[''foo''],dict[''bar''])
Thanks!
Mike Ellis
推荐答案
Michael Ellis写道:
Michael Ellis wrote:
我有一些数据文件,其中的行以空格分隔< name> < value>
格式。每行有多个名称 - 值对。
为了按名称访问值,读取每一行进入关联数组是否有比以下更清晰的习语?
for infile:
tokens = line.split()
dict = {}
for i in range(0,len(tokens),2)dict [tokens] [i]] =代币[i + 1]
do_something_with_values(dict [''foo''],dict [''bar''])
I have some data files with lines in space-delimited <name> <value>
format. There are multiple name-value pairs per line.
Is there a cleaner idiom than the following for reading each line into
an associative array for the purpose of accessing values by name?
for line in infile:
tokens = line.split()
dict = {}
for i in range(0, len(tokens),2) dict[tokens[i]] = tokens[i+1]
do_something_with_values(dict[''foo''],dict[''bar''])
for infile:
tokens = line.split()
d = dict(zip(tokens [:: 2],tokens [1 :: 2]))
do_something_with_values(...)
顺便说一句,不要使用dict作为变量名称。已经
a内置工厂功能来创建字典。
-Peter
for line in infile:
tokens = line.split()
d = dict(zip(tokens[::2], tokens[1::2]))
do_something_with_values(...)
By the way, don''t use "dict" as a variable name. It''s already
a builtin factory function to create dictionaries.
-Peter
I '''''''''''''''''''''`''''''''''''''''''''''''''''''''''''''''''''''''''''''''' br />
def process_tokens(f):
$ 9 $ b for infile:
tokens = line.split()
d = {}
for i in range(0,len(tokens),2):d [tokens [i]] = tokens [i + 1]
yield d
然后,
for d in process_tokens(infile):
do_something_with_values(d [''foo''],d [''bar' '])
如果你想从每一行得到的特定键是循环的常量,
让process_tokens按顺序产生这些项目:
def process_tokens2(f,keys):
$ 9 $ b for infile:
tokens = line.split()
d = { }
for i in range(0,len(tokens),2):d [tokens [i]] = toke ns [i + 1]
收益[密钥中k的[d [k]]
$ f $ b表示foo,bar表示process_tokens(infile," foo" ,bar):
do_something_with_values(foo,bar)
Jeff
I''d move the logic that turns the file into the form you want to
process, under the assumption that you''ll use this code from multiple
places.
def process_tokens(f):
for line in infile:
tokens = line.split()
d = {}
for i in range(0, len(tokens), 2): d[tokens[i]] = tokens[i+1]
yield d
Then,
for d in process_tokens(infile):
do_something_with_values(d[''foo''], d[''bar''])
If the specific keys you want from each line are constant for the loop,
have process_tokens yield those items in sequence:
def process_tokens2(f, keys):
for line in infile:
tokens = line.split()
d = {}
for i in range(0, len(tokens), 2): d[tokens[i]] = tokens[i+1]
yield [d[k] for k in keys]
for foo, bar in process_tokens(infile, "foo", "bar"):
do_something_with_values(foo, bar)
Jeff
Michael Ellis写道:
Michael Ellis wrote:
我有一些数据文件,其中的行以空格分隔< name> < value>
格式。每行有多个名称 - 值对。
为了按名称访问值,读取每一行进入关联数组是否有比以下更清晰的习语?
for infile:
tokens = line.split()
dict = {}
for i in range(0,len(tokens),2)dict [tokens] [i]] =代币[i + 1]
do_something_with_values(dict [''foo''],dict [''bar''])
I have some data files with lines in space-delimited <name> <value>
format. There are multiple name-value pairs per line.
Is there a cleaner idiom than the following for reading each line into
an associative array for the purpose of accessing values by name?
for line in infile:
tokens = line.split()
dict = {}
for i in range(0, len(tokens),2) dict[tokens[i]] = tokens[i+1]
do_something_with_values(dict[''foo''],dict[''bar''])
另一种创建字典的方法:
Yet another way to create the dictionary:
import itertools
nv = iter(" foo) 1 bar 2 baz 3 \ n" .split())
dict(itertools.izip(nv,nv))
{''baz'':''3'',''foo' ':''1'',''bar'':'''2''}
import itertools
nv = iter("foo 1 bar 2 baz 3\n".split())
dict(itertools.izip(nv, nv)) {''baz'': ''3'', ''foo'': ''1'', ''bar'': ''2''}
Peter
Peter
这篇关于更清洁的文字处理成语?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!