如何打印元素名称而不是值,给定一个评估为布尔值的元素列表?

huangapple go评论63阅读模式
英文:

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中有两种选项:

  1. 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"
    }
}
  1. 您可以在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:

  1. 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"
    }
}
  1. 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

huangapple
  • 本文由 发表于 2023年6月14日 23:48:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76475380.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定