<legend id='mCGdB'><style id='mCGdB'><dir id='mCGdB'><q id='mCGdB'></q></dir></style></legend>

      <tfoot id='mCGdB'></tfoot>
          <bdo id='mCGdB'></bdo><ul id='mCGdB'></ul>

        <small id='mCGdB'></small><noframes id='mCGdB'>

      1. <i id='mCGdB'><tr id='mCGdB'><dt id='mCGdB'><q id='mCGdB'><span id='mCGdB'><b id='mCGdB'><form id='mCGdB'><ins id='mCGdB'></ins><ul id='mCGdB'></ul><sub id='mCGdB'></sub></form><legend id='mCGdB'></legend><bdo id='mCGdB'><pre id='mCGdB'><center id='mCGdB'></center></pre></bdo></b><th id='mCGdB'></th></span></q></dt></tr></i><div id='mCGdB'><tfoot id='mCGdB'></tfoot><dl id='mCGdB'><fieldset id='mCGdB'></fieldset></dl></div>

        如何将空格分隔的键值对字符串转换为字典

        时间:2023-10-19
          <tbody id='3iLiT'></tbody>

              <tfoot id='3iLiT'></tfoot>
              <i id='3iLiT'><tr id='3iLiT'><dt id='3iLiT'><q id='3iLiT'><span id='3iLiT'><b id='3iLiT'><form id='3iLiT'><ins id='3iLiT'></ins><ul id='3iLiT'></ul><sub id='3iLiT'></sub></form><legend id='3iLiT'></legend><bdo id='3iLiT'><pre id='3iLiT'><center id='3iLiT'></center></pre></bdo></b><th id='3iLiT'></th></span></q></dt></tr></i><div id='3iLiT'><tfoot id='3iLiT'></tfoot><dl id='3iLiT'><fieldset id='3iLiT'></fieldset></dl></div>
                <legend id='3iLiT'><style id='3iLiT'><dir id='3iLiT'><q id='3iLiT'></q></dir></style></legend>

                <small id='3iLiT'></small><noframes id='3iLiT'>

                  <bdo id='3iLiT'></bdo><ul id='3iLiT'></ul>
                  本文介绍了如何将空格分隔的键值对字符串转换为字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  I've got a string with words that are separated by spaces (all words are unique, no duplicates). I turn this string into list:

                  s = "#one cat #two dogs #three birds"
                  out = s.split()
                  

                  And count how many values are created:

                  print len(out) # Says 192 
                  

                  Then I try to delete everything from the list:

                  for x in out:
                       out.remove(x)
                  

                  And then count again:

                  print len(out) # Says 96 
                  

                  Can someone explain please why it says 96 instead of 0?

                  MORE INFO

                  Each line starts with '#' and is in fact a space-separated pair of words: the first in the pair is the key and second is the value.

                  So, what I am doing is:

                  for x in out:
                       if '#' in x: 
                            ind = out.index(x) # Get current index 
                            nextValue = out[ind+1] # Get next value 
                            myDictionary[x] = nextValue
                            out.remove(nextValue)
                            out.remove(x) 
                  

                  The problem is I cannot move all key,value-pairs into a dictionary since I only iterate through 96 items.

                  解决方案

                  I think you actually want something like this:

                  s = '#one cat #two dogs #three birds'
                  out = s.split()
                  entries = dict([(x, y) for x, y in zip(out[::2], out[1::2])])
                  

                  What is this code doing? Let's break it down. First, we split s by whitespace into out as you had.

                  Next we iterate over the pairs in out, calling them "x, y". Those pairs become a list of tuple/pairs. dict() accepts a list of size two tuples and treats them as key, val.

                  Here's what I get when I tried it:

                  $ cat tryme.py
                  
                  s = '#one cat #two dogs #three birds'
                  out = s.split()
                  entries = dict([(x, y) for x, y in zip(out[::2], out[1::2])])
                  
                  from pprint import pprint
                  pprint(entries)
                  
                  $ python tryme.py
                  {'#one': 'cat', '#three': 'birds', '#two': 'dogs'}
                  

                  这篇关于如何将空格分隔的键值对字符串转换为字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:这段代码中 list[:] 的含义是什么? 下一篇:Python:在迭代时将元素添加到列表中

                  相关文章

                  <i id='hCQl6'><tr id='hCQl6'><dt id='hCQl6'><q id='hCQl6'><span id='hCQl6'><b id='hCQl6'><form id='hCQl6'><ins id='hCQl6'></ins><ul id='hCQl6'></ul><sub id='hCQl6'></sub></form><legend id='hCQl6'></legend><bdo id='hCQl6'><pre id='hCQl6'><center id='hCQl6'></center></pre></bdo></b><th id='hCQl6'></th></span></q></dt></tr></i><div id='hCQl6'><tfoot id='hCQl6'></tfoot><dl id='hCQl6'><fieldset id='hCQl6'></fieldset></dl></div>

                  <small id='hCQl6'></small><noframes id='hCQl6'>

                • <tfoot id='hCQl6'></tfoot>

                  <legend id='hCQl6'><style id='hCQl6'><dir id='hCQl6'><q id='hCQl6'></q></dir></style></legend>
                    • <bdo id='hCQl6'></bdo><ul id='hCQl6'></ul>