gpt4 book ai didi

python - 如何将空格分隔的键字符串,唯一单词的值对转换为字典

转载 作者:太空狗 更新时间:2023-10-29 19:31:46 26 4
gpt4 key购买 nike

我有一个由空格分隔的单词的字符串(所有单词都是唯一的,没有重复的)。我把这个字符串变成列表:

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

并计算创建了多少个值:

print len(out) # Says 192 

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

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 个项目。

最佳答案

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

From the Python 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 loop terminates.

我认为最好借助插图 来展示它。

现在,假设您有一个像这样的可迭代对象(例如list):

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

当您执行 for x in out 时发生的事情是它创建内部索引器,它是这样的(我用符号 ^ 来说明它>):

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

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

[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 个循环(!!)(这是原始列​​表中元素的数量)。这就是为什么您留下原始 lenhalf len 的原因,因为这是您删除时完成循环所需的周期数每个循环从中提取一个元素。


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

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

或者,要逐个删除元素,您需要相反的方式 - 从末尾到开头。使用反转:

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

希望插图能帮助您了解内部发生的事情...

关于python - 如何将空格分隔的键字符串,唯一单词的值对转换为字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35618307/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com