英文:
Given a list of elements that evaluate to boolean values, how do I print the element name and not the value?
问题
假设我有一些变量,它们的值要么为true,要么为false:
firstValue = true
secondValue = false
thirdValue = true
然后我把它们放在一个列表中:
`def list = [firstValue, secondValue, thirdValue]`
我想要遍历列表,并检查每个值是否为true或false,如果值为true,我想要能够打印出变量名,例如firstValue
for (item in list){
if (item == true){
println("${item} is true")
}
}
这只会打印出"true is true"。
英文:
Lets say I have some variables that evaluate to either true or false:
firstValue = true
secondValue = false
thirdValue = true
And I put them in a list:
def list = [firstValue, secondValue, thirdValue]
I want to iterate through the list and check if each value returns true or false, if the value is true I want to be able to print the name i.e. firstValue
for (item in list){
if (item == true){
println("${item} is true")
}
}
This is just printing "true is true"
答案1
得分: 0
你的list
只包含值(true/false),所以很难获取键。
考虑使用一个字典,它包含一些键,你可以使用迭代来显示键和值。
data = {
"firstValue": True,
"secondValue": False,
"thirdValue": True
}
for key, value in data.items():
if value:
print(key, "是真的")
firstValue 是真的
thirdValue 是真的
在线尝试
如果你真的需要一个单独的键列表,你可以使用以下方法代替调用items()
。
listOfKeys = list(data.keys())
for key in listOfKeys:
if data[key]:
英文:
Your list
only holds the values (true/false), so it's 'hard' to get the key back.
Consider using a dictionary, which holds some keys that you can iterate to show both the key and value
data = {
"firstValue": True,
"secondValue": False,
"thirdValue": True
}
for key, value in data.items():
if value:
print(key, "is true")
firstValue is true
thirdValue is true
[Try it online!](https://tio.run/##TY3BCoMwEETP3a9YclIQL70JvfYLSu@hSXCpTWR3FaT47WmsFTqnYR4zMy7ap3jO2Vm1eME3YJEJxKJ3O0zedHjjyTd7Lv6RojvA1Q5yEO2J3X8DVoCQGJ9@aXDeAFLE7aYl9S@p6g5OFHZULP40MkWtvi1DglqmTA05fwA "Python 3 – Try It Online")
If you really need a separate list of keys, you can use the following instead of calling items()
listOfKeys = list(data.keys())
for key in listOfKeys:
if data[key]:
答案2
得分: 0
不确定为什么接受了其他答案,即使它完全用Python
编写(???),但在Groovy中有两种选项:
- 在
Script
中,您可以通过它们的名称获取变量,如下所示:
firstValue = true
secondValue = false
thirdValue = true
def list = this.binding.variables.grep{ 'out' != it.key }
for (item in list){
if (item.value){
println "$item.key is true"
}
}
- 您可以在
Map
(Java中正式称为字典)中声明数据并对其进行迭代:
def map = [ firstValue:true, secondValue:false, thirdValue:true ]
for (item in map){
if (item.value){
println "$item.key is true"
}
}
在这两种情况下,您将获得以下输出:
firstValue is true
thirdValue is true
英文:
Not sure, why the other answer was accepted even though it's written all the way in Python
(???), but in Groovy you have 2 options:
- in a
Script
you can grab the variables by their names like so:
firstValue = true
secondValue = false
thirdValue = true
def list = this.binding.variables.grep{ 'out' != it.key }
for (item in list){
if (item.value){
println "$item.key is true"
}
}
- You declare your data in a
Map
(how the dict is officually called in java) and iterate over those:
def map = [ firstValue:true, secondValue:false, thirdValue:true ]
for (item in map){
if (item.value){
println "$item.key is true"
}
}
In both cases you'll get the following printed:
firstValue is true
thirdValue is true
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论