Nested for loop taking exact list items – Python

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

Nested for loop taking exact list items - Python

问题

<Partners>
  <ExternalCode>aaa</ExternalCode>
  <ShortName>ddd</ShortName>
  <Name>ggg</Name>
  <PartnerGroupCode>jjj</PartnerGroupCode>
</Partners>
<Partners>
  <ExternalCode>aaa</ExternalCode>
  <ShortName>ddd</ShortName>
  <Name>ggg</Name>
  <PartnerGroupCode>kkk</PartnerGroupCode>
</Partners>
...
(共计 9 个类似的块)
英文:

I am trying to write a code for building a XML file which has to have about 1000 lines of data about partners.

I want to take the loops to take for example from list_a first item and match it with first item of list_b. second with second and so on.

list_a = ['aaa', 'bbb', 'ccc']
list_b = ['ddd', 'eee', 'fff']
list_c = ['ggg', 'hhh', 'iii']
list_d = ['jjj', 'kkk', 'lll']

here is the code I have tried. Cannot find the right breaks from the loop so the code can give proper XML text.

for item_a in list_a:  
  print('<Partners>')
  print('  <ExternalCode>', end='')
  print(item_a, end='')
  print('</ExternalCode>')
  for item_b in list_b:
    print('  <ShortName>', end='')
    print(item_b, end='')
    print('</ShortName>')
    for item_c in list_c:
      print('  <Name>', end='')
      print(item_c, end='')
      print('</Name>')
      for item_d in list_d:
        print('  <PartnerGroupCode>', end='')
        print(item_d, end='')
        print('</PartnerGroupCode>')
        print('</Partners>')

The code has to give an XML text like this:

<Partners>
  <ExternalCode>String 33</ExternalCode>
  <ShortName>String 34</ShortName>
  <Name>String 35</Name>
  <PartnerGroupCode>String 36</PartnerGroupCode>
</Partners>
<Partners>
  <ExternalCode>String 37</ExternalCode>
  <ShortName>String 38</ShortName>
  <Name>String 39</Name>
  <PartnerGroupCode>String 40</PartnerGroupCode>
</Partners>

答案1

得分: 1

如果我理解正确,您想以以下方式将列表中的相应元素分组?

Output
<Partners>
  <ExternalCode>aaa</ExternalCode>
  <ShortName>ddd</ShortName>
  <Name>ggg</Name>
  <PartnerGroupCode>jjj</PartnerGroupCode>
</Partners>
<Partners>
  <ExternalCode>bbb</ExternalCode>
  <ShortName>eee</ShortName>
  <Name>hhh</Name>
  <PartnerGroupCode>kkk</PartnerGroupCode>
</Partners>
<Partners>
  <ExternalCode>ccc</ExternalCode>
  <ShortName>fff</ShortName>
  <Name>iii</Name>
  <PartnerGroupCode>lll</PartnerGroupCode>
</Partners>
英文:

if my understanding is correct you would like to group all the corresponding elements in a list in the below manner ?

list_a = ['aaa', 'bbb', 'ccc']
list_b = ['ddd', 'eee', 'fff']
list_c = ['ggg', 'hhh', 'iii']
list_d = ['jjj', 'kkk', 'lll']

for i in range(len(list_a)):
    print('<Partners>')
    print('  <ExternalCode>' + list_a[i] + '</ExternalCode>')
    print('  <ShortName>' + list_b[i] + '</ShortName>')
    print('  <Name>' + list_c[i] + '</Name>')
    print('  <PartnerGroupCode>' + list_d[i] + '</PartnerGroupCode>')
    print('</Partners>')

Output

<Partners>
  <ExternalCode>aaa</ExternalCode>
  <ShortName>ddd</ShortName>
  <Name>ggg</Name>
  <PartnerGroupCode>jjj</PartnerGroupCode>
</Partners>
<Partners>
  <ExternalCode>bbb</ExternalCode>
  <ShortName>eee</ShortName>
  <Name>hhh</Name>
  <PartnerGroupCode>kkk</PartnerGroupCode>
</Partners>
<Partners>
  <ExternalCode>ccc</ExternalCode>
  <ShortName>fff</ShortName>
  <Name>iii</Name>
  <PartnerGroupCode>lll</PartnerGroupCode>
</Partners>

</details>



# 答案2
**得分**: 1

Here is the translated code:

```python
另一种通用的从一组列表中获取第i个元素的方法是使用Python的zip函数。

for a, b, c, d in zip(list_a, list_b, list_c, list_d):
    print('<Partners>')
    print(f'  <ExternalCode>{a}</ExternalCode>')
    print(f'  <ShortName>{b}</ShortName>')
    print(f'  <Name>{c}</Name>')
    print(f'  <PartnerGroupCode>{d}</PartnerGroupCode>')
    print('</Partners>')

Please note that I have translated the code portion as requested, and there are no additional contents or answers to translation questions.

英文:

Another general approach to taking the i-th element from a group of lists is the python zip function.

for a, b, c, d in zip(list_a, list_b, list_c, list_d):
    print(&#39;&lt;Partners&gt;&#39;)
    print(f&#39;  &lt;ExternalCode&gt;{a}&lt;/ExternalCode&gt;&#39;)
    print(f&#39;  &lt;ShortName&gt;{b}&lt;/ShortName&gt;&#39;)
    print(f&#39;  &lt;Name&gt;{c}&lt;/Name&gt;&#39;)
    print(f&#39;  &lt;PartnerGroupCode&gt;{d}&lt;/PartnerGroupCode&gt;&#39;)
    print(&#39;&lt;/Partners&gt;&#39;)

huangapple
  • 本文由 发表于 2023年8月11日 01:27:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76878037.html
匿名

发表评论

匿名网友

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

确定