file2 = open("/path/to/file2", "r")
market_names = {}
for line in file2:
fields = line.split(",")
market_names[int(fields[0])] = fields[1]
file2.close()
file1 = open("/path/to/file1", "r")
for line in file1:
fields = line.split(",")
market_name = market_names[int(fields[1])]
# do what ever you want with the Market_Name associated with
# the Market_ID contained in this line
file1.close()
If the files are not too big (a few megabytes) you could store each item in a dict() using the Market_ID as key.
This looks like data drom a database - if you are familiar with relational databases and their use you could insert each file into a separate table and then perform queries. Python has an interface to sqlite in its standard library.
For solving this quickly I suggest to use dicts though. BY -- sleeplessnerd
file2 = open("/path/to/file2", "r")
market_names = {}
for line in file2:
fields = line.split(",")
market_names[int(fields[0])] = fields[1]
file2.close()
file1 = open("/path/to/file1", "r")
for line in file1:
fields = line.split(",")
market_name = market_names[int(fields[1])]
# do what ever you want with the Market_Name associated with
# the Market_ID contained in this line
file1.close()
If the files are not too big (a few megabytes) you could store each item in a dict() using the Market_ID as key.
This looks like data drom a database - if you are familiar with relational databases and their use you could insert each file into a separate table and then perform queries. Python has an interface to sqlite in its standard library.
For solving this quickly I suggest to use dicts though.
for market_id in flie1_list:
for pair in file2_list:
if pair[0] == market_id:
market_name = pair[1]
break
<keep doing whatever it is you need to do in first loop>
注意-我也是一个初学者,这是我在这个网站上的第一个答案。放轻松;)
Hmm, could you please supply more details?
When you say file(list) do you mean that you've created a list that contains all the data you need from the first file? what about the second file?
If they are both lists than something like this would work:
for market_id in flie1_list:
for pair in file2_list:
if pair[0] == market_id:
market_name = pair[1]
break
<keep doing whatever it is you need to do in first loop>
Note - I'm fairly a beginner as well, and it's my first answer on this site. Go easy ;)
我不熟悉我的Python,但从概念上讲,您可能想要做的是将第二个文件读入 字典先,然后在字典中查找id以获取关联的名称。或者,如果 id 是数字并且或多或少密集,您可以将它们读入常规数组。
I'm not up on my python, but conceptually, what you'll probably want to do is read the second file into a dictionary first and then look up the id in the dictionary to get the associated name. Alternatively, if the ids are numeric and more-or-less dense, you could just read them into a regular array.
发布评论
评论(4)
有关字典的更多信息,
有关 sqlite 的更多信息
More about dictionaries,
More about sqlite
如果文件不太大(几兆字节),您可以使用 Market_ID 作为键将每个项目存储在 dict() 中。
这看起来像数据库中的数据 - 如果您熟悉关系数据库及其使用,您可以将每个文件插入到单独的表中,然后执行查询。 Python 在其标准库中有一个 sqlite 接口。
为了快速解决这个问题,我建议使用字典。
If the files are not too big (a few megabytes) you could store each item in a dict() using the Market_ID as key.
This looks like data drom a database - if you are familiar with relational databases and their use you could insert each file into a separate table and then perform queries. Python has an interface to sqlite in its standard library.
For solving this quickly I suggest to use dicts though.
嗯,您能提供更多详细信息吗?
当您说 file(list) 时,您的意思是您已经创建了一个包含第一个文件中所需的所有数据的列表吗?那第二个文件呢?
如果它们都是列表,那么这样的东西会起作用:
注意-我也是一个初学者,这是我在这个网站上的第一个答案。放轻松;)
Hmm, could you please supply more details?
When you say file(list) do you mean that you've created a list that contains all the data you need from the first file? what about the second file?
If they are both lists than something like this would work:
Note - I'm fairly a beginner as well, and it's my first answer on this site. Go easy ;)
我不熟悉我的Python,但从概念上讲,您可能想要做的是将第二个文件读入
字典
先,然后在字典中查找id以获取关联的名称。或者,如果 id 是数字并且或多或少密集,您可以将它们读入常规数组。I'm not up on my python, but conceptually, what you'll probably want to do is read the second file into a
dictionary
first and then look up the id in the dictionary to get the associated name. Alternatively, if the ids are numeric and more-or-less dense, you could just read them into a regular array.