- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有一个由空格分隔的单词的字符串(所有单词都是唯一的,没有重复的)。我把这个字符串变成列表:
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 theelse
clause, if present, is executed, and theloop
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 个循环(!!)(这是原始列表中元素的数量)。这就是为什么您留下原始 len
的 half 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 usereversed
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/
我有这个代码来查找这个模式:201409250200131738007947036000 - 1,在文本内 final String patternStr = "(\\d{
我正在尝试使用正则表达式清除一些用户输入,以删除 [ 和 ] 并删除任何大于 1 个空格的空格。但我似乎无法实现我想要的效果。这是我第一次使用正则表达式,所以我对如何写出来有点困惑。 (preg_re
我正在尝试构建这个简单的正则表达式来匹配 Java 中的单词+空格,但我在尝试解决它时感到困惑。该网站上有很多类似的示例,但答案大多给出了正则表达式本身,而没有解释它是如何构造的。 我正在寻找的是形成
好吧,我已经阅读了很多建议如何消除多余空间的帖子,但无论出于何种原因,我似乎无法将这些建议应用到我的系统中,所以我在这里寻求您的帮助。 这些是我代码的最后几行: for line in rli
所以我正在我的测试存储上学习网页抓取,但我不确定如何正确地从“sizes”数组中删除空的新行。 const $ = cheerio.load(body) $('div.lis
这个问题已经有答案了: How to prevent invalid characters from being typed into input fields (8 个回答) 已关闭 9 年前。 是
有人知道如何让扫描仪忽略空间吗?我想输入名字和第二个名字,但扫描仪不让我输入,我想保存全名 String name; System.out.print("Enter name: "); name =
这个问题在这里已经有了答案: Make Vim show ALL white spaces as a character (23 个回答) 关闭 8 年前。 VIM(使用 Solarized Dar
我想使用 StreamTokenizer 从 java 文件中提取名称。我已将空格设置为逗号 inputTokenizer.whitespaceChars(',', ','); 但是,
我正在使用此代码逐行读取 txt 文件。 // Open the file that is the first command line parameter FileInputStream fstre
我似乎无法弄清楚我需要的正则表达式。这就是我想要实现的目标: {ANY CHAR} + @javax.persistence.Column(name = "{ANY 30 CHARS}") + {AN
我正在运行 StyleCop(顺便说一句,如果你想提供高质量的代码,我完全推荐它)... 我有这条线 [System.Xml.Serialization.XmlRootAttribute(Namesp
我刚刚更新到 PhpStorm 2016,我突然注意到,每次我按 Ctrl + S 保存文件时,它都会删除我在测试这段代码后按下以继续编写的空格/制表符。 请帮忙,这对我来说很烦人,因为我在每一行代码
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 7 年前。 Improve th
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求提供代码的问题必须表现出对所解决问题的最低限度的了解。包括尝试的解决方案、为什么它们不起作用以及预期结果
我已经看过几十个关于这个主题的问题和答案,但我仍然无法解决我的问题。 我在我的代码中使用了一个外部 ffmpeg 转换器,我将文件路径作为参数传递,如下所示: OutputPackage oo = c
谁能详细解释一下它们是什么以及它们之间的区别。提前致谢。 最佳答案 转义序列是代表其他内容的字符序列。例如(“\n” = 新行,“\?” = 问号等)。有关更详细的列表,请检查:https://en.
我无法从我的 javascript 文本中删除换行符。这是我正在处理的数据示例: 0: "Christian Pulisic" 1: "↵" 2: "From Wikipedia, the free
我有一个问题 - 我似乎无法从字符串的开头/结尾删除新行/空格。我在正则表达式的开头和结尾使用 \s ,甚至在获取字符串后使用 .trim() ,但无济于事。 public void extractI
我是 php 的新手,我正在尝试将一系列变量添加到 html 超链接中。但是,任何返回空格的变量都会弄乱超链接。 Grants Test
我是一名优秀的程序员,十分优秀!