根据Python中第一个列表的ID从第二个列表获取值

发布于 2024-12-11 06:38:19 字数 1432 浏览 0 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

浮光之海 2024-12-18 06:38:19

如果文件不太大(几兆字节),您可以使用 Market_ID 作为键将每个项目存储在 dict() 中。

这看起来像是数据库中的数据 - 如果您熟悉的话
关系数据库及其使用,您可以将每个文件插入到
分离表,然后执行查询。 Python 有一个接口
sqlite 在其标准库中。

为了快速解决这个问题,我建议使用字典。
作者:sleeplessnerd

有关字典的更多信息
有关 sqlite 的更多信息

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

More about dictionaries,
More about sqlite

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()
凉城凉梦凉人心 2024-12-18 06:38:19

如果文件不太大(几兆字节),您可以使用 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.

半边脸i 2024-12-18 06:38:19

嗯,您能提供更多详细信息吗?

当您说 file(list) 时,您的意思是您已经创建了一个包含第一个文件中所需的所有数据的列表吗?那第二个文件呢?

如果它们都是列表,那么这样的东西会起作用:

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 ;)

故笙诉离歌 2024-12-18 06:38:19

我不熟悉我的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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文