如何将空格键串、值对的唯一单词转换成di

2024-09-24 22:30:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个由空格分隔的单词组成的字符串(所有单词都是唯一的,没有重复的)。我将此字符串转换为列表:

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

并计算创建了多少个值:

^{pr2}$

然后我试着从列表中删除所有内容:

for x in out:
     out.remove(x)

然后再数数:

print len(out) # Says 96 

为什么有人会说96而不是0?在

更多信息

每行以“#”开头,实际上是一对空格分隔的单词:第一行是键,第二行是值。在

所以,我要做的是:

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) 

问题是我不能将所有键、值对都移到字典中,因为我只迭代96个项。在


Tags: 字符串in列表forgetindexout单词
3条回答

你没说具体的。你为什么要删除列表中的所有内容?如果你需要做的只是清除列表,为什么不直接这样做:

out = []

至于for循环中实际发生了什么:

From thePython for statement documentation:

The expression list is evaluated once; it should yield an iterable object. An iterator is created for the result of the expression_list. The suite is then executed once for each item provided by the iterator, in the order of ascending indices. Each item in turn is assigned to the target list using the standard rules for assignments, and then the suite is executed. When the items are exhausted (which is immediately when the sequence is empty), the suite in the else clause, if present, is executed, and the loopterminates.

我认为最好借助一个插图来展示。在

现在,假设您有一个iterable object(例如list),如下所示:

out = [a, b, c, d, e, f]

当你做for x in out时,它会创建内部索引器,如下所示(我用符号^来说明):

^{pr2}$

通常的情况是:当您完成循环的一个循环时,索引器向前移动,如下所示:

[a, b, c, d, e, f] #cycle 1
 ^  <-- here is the indexer

[a, b, c, d, e, f] #cycle 2
    ^  <-- here is the indexer

[a, b, c, d, e, f] #cycle 3
       ^  <-- here is the indexer

[a, b, c, d, e, f] #cycle 4
          ^  <-- here is the indexer

[a, b, c, d, e, f] #cycle 5
             ^  <-- here is the indexer

[a, b, c, d, e, f] #cycle 6
                ^  <-- here is the indexer

#finish, no element is found anymore!

As you can see, the indexer keeps moving forward till the end of your list, regardless of what happened to the list!

因此,当您执行remove操作时,内部会发生以下情况:

[a, b, c, d, e, f] #cycle 1
 ^  <-- here is the indexer

[b, c, d, e, f] #cycle 1 - a is removed!
 ^  <-- here is the indexer

[b, c, d, e, f] #cycle 2
    ^  <-- here is the indexer

[c, d, e, f] #cycle 2 - c is removed
    ^  <-- here is the indexer

[c, d, e, f] #cycle 3
       ^  <-- here is the indexer

[c, d, f] #cycle 3 - e is removed
       ^  <-- here is the indexer

#the for loop ends

请注意,那里只有3个周期,而不是6个周期(!!)(这是原始列表中元素的数目)。这就是为什么你留下了原来的len的一半,因为当你在每个循环中移除一个元素时,完成循环所需的周期数。在


如果要清除列表,只需执行以下操作:

if (out != []):
    out.clear()

或者,如果要逐个删除元素,则需要以相反的方式进行操作,即从末尾到开头。使用reversed

for x in reversed(out):
    out.remove(x)

现在,为什么reversed会起作用?如果索引器继续前进,那么reversed是否也不应该工作,因为元素的数量在每个循环中减少了一个?在

不,不是那样的

Because reversed method changes the way to the internal indexer works! What happened when you use reversed method is to make the internal indexer moves backward (from the end) instead of forward.

举例来说,这是通常发生的情况:

[a, b, c, d, e, f] #cycle 1
                ^  <-- here is the indexer

[a, b, c, d, e, f] #cycle 2
             ^  <-- here is the indexer

[a, b, c, d, e, f] #cycle 3
          ^  <-- here is the indexer

[a, b, c, d, e, f] #cycle 4
       ^  <-- here is the indexer

[a, b, c, d, e, f] #cycle 5
    ^  <-- here is the indexer

[a, b, c, d, e, f] #cycle 6
 ^  <-- here is the indexer

#finish, no element is found anymore!

因此,当每个周期执行一次删除操作时,它不会影响索引器的工作方式:

[a, b, c, d, e, f] #cycle 1
                ^  <-- here is the indexer

[a, b, c, d, e] #cycle 1 - f is removed
                ^  <-- here is the indexer

[a, b, c, d, e] #cycle 2
             ^  <-- here is the indexer

[a, b, c, d] #cycle 2 - e is removed
             ^  <-- here is the indexer

[a, b, c, d] #cycle 3
          ^  <-- here is the indexer

[a, b, c] #cycle 3 - d is removed
          ^  <-- here is the indexer

[a, b, c] #cycle 4
       ^  <-- here is the indexer

[a, b] #cycle 4 - c is removed
       ^  <-- here is the indexer

[a, b] #cycle 5
    ^  <-- here is the indexer

[a] #cycle 5 - b is removed
    ^  <-- here is the indexer

[a] #cycle 6
 ^  <-- here is the indexer

[] #cycle 6 - a is removed
 ^  <-- here is the indexer

希望这张图能帮助你了解内部的情况。。。在

我想你真的想要这样的东西:

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

这个代码在做什么?让我们把它分解。首先,我们按空格将s拆分为out。在

接下来我们迭代out中的对,称它们为“x, y”。这些对成为元组/对的listdict()接受大小为两个元组的列表,并将它们视为key, val。在

以下是我尝试的结果:

^{pr2}$

相关问题 更多 >