如何从Python中的嵌套列表中获取特定值

huangapple go评论63阅读模式
英文:

How to get specific values from a nested list in Python

问题

以下是翻译好的部分:

我尝试使用for循环和列表切片,但都没有成功
如果你们中的任何人有解决办法,那将不胜感激。
输出应为:
  'Russia', 19553
  'China',  193
  'Africa', 0
  'USA',    5531
  'UK',     0
在上面的输出中,Africa和UK的d_id值已被替换为0

请注意,其中包括代码的文本没有被翻译,保持不变。

英文:

Could you help me getting the specific values I wanted in the below list

list=[['Russia',[{'id': 250282,'d_id': 19553,'p_id': 1796,'value': 'silver'},
                {'id': 250212,'d_id': 19553,'p_id': 1896,'value': 'gold'},
                {'id': 250242,'d_id': 19553,'p_id': 1396,'value': 'iron'},
                {'id': 250082,'d_id': 19553,'p_id': 1496,'value': 'cobalt'}]],
      ['China',[{'id': 210282,'d_id': 193,'p_id': 1196,'value': 'silver'},
                {'id': 220212,'d_id': 193,'p_id': 1396,'value': 'iron'},
                {'id': 240242,'d_id': 193,'p_id': 1586,'value': 'iron'},
                {'id': 250082,'d_id': 193,'p_id': 1492,'value': 'gold'}]],
      ['Africa',[]],
      ['USA',[{'id': 200282,'d_id': 5531,'p_id': 1093,'value': 'iron'},
                {'id': 253212,'d_id': 5531,'p_id': 1843,'value': 'gold'},
                {'id': 255242,'d_id': 5531,'p_id': 1323,'value': 'iron'},
                {'id': 257082,'d_id': 5531,'p_id': 1409,'value': 'cobalt'}]],
      ['UK',[]]]


output should be:
  'Russia', 19553
  'China',  193
  'Africa', 0  
  'USA',    5531
  'UK',     0

I am trying to get countries and unique values of d_id because it will be the same for all records and impute missing values with 0

I tried for loops and slicing of lists but nothing worked out
If anyone of you have a solution for this that would be much appreciated.

output should be:
'Russia', 19553
'China', 193
'Africa', 0
'USA', 5531
'UK', 0

In the above output Africa and UK d_id values are imputed with 0

答案1

得分: 0

{l[0]: set([_d['d_id'] for _d in l[1]]) if len(l[1]) > 0 else set([0]) for l in your_list}

英文:

Next time you should include the code that you've tried with your question.

{l[0]: set([_d['d_id'] for _d in l[1]]) if len(l[1]) > 0 else set([0]) for l in your_list}

答案2

得分: 0

您的代码已经提供,我会忽略代码部分,只返回翻译的部分:

# Your inner lists have 2 values - a country name and a list of records.
# 内部列表包含2个值 - 国家名称和记录列表。

# You could iterate the list, using tuple expansion to get those two values.
# 您可以迭代列表,使用元组扩展来获取这两个值。

# If the list of records is not empty, grab the first value, otherwise use zero.
# 如果记录列表不为空,获取第一个值,否则使用零。

list=[['Russia',[{'id': 250282,'d_id': 19553,'p_id': 1796,'value': 'silver'}, ... ['UK',[]]]

output = []
for name, records in list:
    if records:
        d_id = records[0]['d_id']
    else:
        d_id = 0
    output.append((name, d_id))

for name, d_id in output:
    print(f"    '{name}': {d_id}")

希望这个翻译对您有所帮助。

英文:

Your inner lists have 2 values - a country name and a list of records. You could iterate the list, using tuple expansion to get those two values. If the list of records is not empty, grab the first value, otherwise use zero.

list=[['Russia',[{'id': 250282,'d_id': 19553,'p_id': 1796,'value': 'silver'},
                {'id': 250212,'d_id': 19553,'p_id': 1896,'value': 'gold'},
                {'id': 250242,'d_id': 19553,'p_id': 1396,'value': 'iron'},
                {'id': 250082,'d_id': 19553,'p_id': 1496,'value': 'cobalt'}]],
      ['China',[{'id': 210282,'d_id': 193,'p_id': 1196,'value': 'silver'},
                {'id': 220212,'d_id': 193,'p_id': 1396,'value': 'iron'},
                {'id': 240242,'d_id': 193,'p_id': 1586,'value': 'iron'},
                {'id': 250082,'d_id': 193,'p_id': 1492,'value': 'gold'}]],
      ['Africa',[]],
      ['USA',[{'id': 200282,'d_id': 5531,'p_id': 1093,'value': 'iron'},
                {'id': 253212,'d_id': 5531,'p_id': 1843,'value': 'gold'},
                {'id': 255242,'d_id': 5531,'p_id': 1323,'value': 'iron'},
                {'id': 257082,'d_id': 5531,'p_id': 1409,'value': 'cobalt'}]],
      ['UK',[]]]


output = []
for name, records in list:
    if records:
        d_id = records[0]['d_id']
    else:
        d_id = 0
    output.append((name, d_id))

for name, d_id in output:
    print(f"    '{name}': {d_id}")

答案3

得分: 0

for item in list:
country = item[0]
metal = item[1]
d_id = []
for details in metal:
for ids in details:
if ids == 'd_id':
d_id.append(details[ids])
d_id = set(d_id)

if len(d_id):
print(f"{country},{d_id}")
else:
print(f"{country},0")

英文:
for item in list:
    country = item[0]
    metal = item[1]
    d_id = []
    for details in metal:
        for ids in details:
            if ids == 'd_id':
                d_id.append(details[ids])
    d_id = set(d_id)

if len(d_id):
    print(f"{country},{d_id}")
else:
    print(f"{country},0")

huangapple
  • 本文由 发表于 2023年2月6日 04:58:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75355459.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定