如果 arr = [4,3,2,1] 并且我想将第一个值与数组的最小值交换,如果我在 python 上使用它的话
arr[0] , arr[arr.index(min(arr))] = min(arr) , arr[0]
#or
arr[0] , arr[arr.index(min(arr))] = arr[arr.index(min(arr))] , arr[0]
他们不工作,但如果我这样做
b = arr.index(min(arr))
#and then
arr[0] , arr[b] = arr[b] , arr[0]
这很好用。谁能解释为什么?
最佳答案
这与操作的顺序有关。
我们可以对列表进行子类化以对其进行检测以显示它们在做什么。
class TracingList(list):
def __getitem__(self, key):
value = super().__getitem__(key)
print(self, "reading", key, "=", value)
return value
def __setitem__(self, key, value):
print(self, "writing", key, "=", value)
super().__setitem__(key, value)
def index(self, value):
index = super().index(value)
print(self, "finding index of", value, "=", index)
return index
arr = TracingList([4, 3, 2, 1])
arr[0], arr[arr.index(min(arr))] = min(arr), arr[0]
print(arr)
print("===")
arr = TracingList([4, 3, 2, 1])
arr[0], arr[arr.index(min(arr))] = arr[arr.index(min(arr))], arr[0]
print(arr)
print("===")
arr = TracingList([4, 3, 2, 1])
b = arr.index(min(arr))
arr[0], arr[b] = arr[b], arr[0]
print(arr)
打印出来
[4, 3, 2, 1] reading 0 = 4
[4, 3, 2, 1] writing 0 = 1
[1, 3, 2, 1] finding index of 1 = 0
[1, 3, 2, 1] writing 0 = 4
[4, 3, 2, 1]
===
[4, 3, 2, 1] finding index of 1 = 3
[4, 3, 2, 1] reading 3 = 1
[4, 3, 2, 1] reading 0 = 4
[4, 3, 2, 1] writing 0 = 1
[1, 3, 2, 1] finding index of 1 = 0
[1, 3, 2, 1] writing 0 = 4
[4, 3, 2, 1]
===
[4, 3, 2, 1] finding index of 1 = 3
[4, 3, 2, 1] reading 3 = 1
[4, 3, 2, 1] reading 0 = 4
[4, 3, 2, 1] writing 0 = 1
[1, 3, 2, 1] writing 3 = 4
[1, 3, 2, 4]
关于python - 在 python 中交换列表元素,其中表达式包含函数调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51883429/
关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。
为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar
在我的应用程序中,我需要能够找到所有数字子字符串,然后扫描每个子字符串,找到第一个匹配范围(例如5到15之间)的子字符串,并将该实例替换为另一个字符串“X”。我的测试字符串s="1foo100bar10gee1"我的初始模式是1个或多个数字的任何字符串,例如,re=Regexp.new(/\d+/)matches=s.scan(re)给出["1","100","10","1"]如果我想用“X”替换第N个匹配项,并且只替换第N个匹配项,我该怎么做?例如,如果我想替换第三个匹配项“10”(匹配项[2]),我不能只说s[matches[2]]="X"因为它做了两次替换“1fooX0barXg
是否有类似“RVMuse1”或“RVMuselist[0]”之类的内容而不是键入整个版本号。在任何时候,我们都会看到一个可能包含5个或更多ruby的列表,我们可以轻松地键入一个数字而不是X.X.X。这也有助于rvmgemset。 最佳答案 这在RVM2.0中是可能的=>https://docs.google.com/document/d/1xW9GeEpLOWPcddDg_hOPvK4oeLxJmU3Q5FiCNT7nTAc/edit?usp=sharing-知道链接的任何人都可以发表评论
我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re
我正在尝试用ruby中的gsub函数替换字符串中的某些单词,但有时效果很好,在某些情况下会出现此错误?这种格式有什么问题吗NoMethodError(undefinedmethod`gsub!'fornil:NilClass):模型.rbclassTest"replacethisID1",WAY=>"replacethisID2andID3",DELTA=>"replacethisID4"}end另一个模型.rbclassCheck 最佳答案 啊,我找到了!gsub!是一个非常奇怪的方法。首先,它替换了字符串,所以它实际上修改了
我有一个包含多个键的散列和一个字符串,该字符串不包含散列中的任何键或包含一个键。h={"k1"=>"v1","k2"=>"v2","k3"=>"v3"}s="thisisanexamplestringthatmightoccurwithakeysomewhereinthestringk1(withspecialcharacterslike(^&*$#@!^&&*))"检查s是否包含h中的任何键的最佳方法是什么,如果包含,则返回它包含的键的值?例如,对于上面的h和s的例子,输出应该是v1。编辑:只有字符串是用户定义的。哈希将始终相同。 最佳答案
我有一些代码在几个不同的位置之一运行:作为具有调试输出的命令行工具,作为不接受任何输出的更大程序的一部分,以及在Rails环境中。有时我需要根据代码的位置对代码进行细微的更改,我意识到以下样式似乎可行:print"Testingnestedfunctionsdefined\n"CLI=trueifCLIdeftest_printprint"CommandLineVersion\n"endelsedeftest_printprint"ReleaseVersion\n"endendtest_print()这导致:TestingnestedfunctionsdefinedCommandLin
这个问题在这里已经有了答案:关闭10年前。PossibleDuplicate:Pythonconditionalassignmentoperator对于这样一个简单的问题表示歉意,但是谷歌搜索||=并不是很有帮助;)Python中是否有与Ruby和Perl中的||=语句等效的语句?例如:foo="hey"foo||="what"#assignfooifit'sundefined#fooisstill"hey"bar||="yeah"#baris"yeah"另外,类似这样的东西的通用术语是什么?条件分配是我的第一个猜测,但Wikipediapage跟我想的不太一样。
查看我的Ruby代码:h=Hash.new([])h[0]=:word1h[1]=h[1]输出是:Hash={0=>:word1,1=>[:word2,:word3],2=>[:word2,:word3]}我希望有Hash={0=>:word1,1=>[:word2],2=>[:word3]}为什么要附加第二个哈希元素(数组)?如何将新数组元素附加到第三个哈希元素? 最佳答案 如果您提供单个值作为Hash.new的参数(例如Hash.new([]),完全相同的对象将用作每个缺失键的默认值。这就是您所拥有的,那是你不想要的。您可以改用