Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在使用函数 Argmax 来获取字符串数组中最大大小的字符串,当字符串包含单个字符的重复时,结果会变得很奇怪。
例如:
x = ["ABC", "AAAA"] argmax(x) # 1 # The return of argmax is 1, is that correct ? x = ["ABC", "AAAABBBBCCCCDDDD"] argmax(x) # = 1 x = ["ABC", "AAAABBBBCCCCDDDD", "ABCD"] argmax(x) # = 3
字符串在这里按字典顺序进行比较,而不是按它们的长度。
由于这个原因,“ABC”被认为大于“AAAA”,因此数组中的最大元素["ABC", "AAAA"]确实在索引 1 处。
["ABC", "AAAA"]
如果您的目标是按长度比较字符串,则可以将该length函数应用于数组中的每个字符串,然后使用argmax. 例如:
length
argmax
julia> x = ["ABC", "AAAABBBBCCCCDDDD", "ABCD"] julia> argmax(length.(x)) 2