[英]How can I check if number exists in list
I wish to check if a certain number is in a list, but I'm struggling with the if sentence.我想检查某个数字是否在列表中,但我在 if 语句中苦苦挣扎。 I have
我有
possibilities = []
node = 1
edges = [['1', '1', '10'], ['1', '6', '7'], ['1', '16', '5'], ['1', '18', '6'], ['1', '19', '6'], ['2', '2', '10'], ['2', '5', '3']]
for i in edges:
if node in i[:-1]:
possibilities.append(i)
print(possibilities)
But I'm not getting any entries in possibilities
.但我没有得到任何
possibilities
的条目。 Is there something I'm missing, because I thought this should work.有什么我想念的吗,因为我认为这应该可行。
You are confusing the data type int with data type string.您将数据类型 int 与数据类型 string 混淆了。 Change the node to "1" instead of 1 and you are good to go.
将节点更改为“1”而不是 1,您就可以使用 go。
You are trying to compare int
data type with str
data type.您正在尝试将
int
数据类型与str
数据类型进行比较。 Try this:尝试这个:
possibilities = []
node = 1
edges = [['1', '1', '10'], ['1', '6', '7'], ['1', '16', '5'], ['1', '18', '6'], ['1', '19', '6'], ['2', '2', '10'], ['2', '5', '3']]
for i in edges:
if str(node) in i:
possibilities.append(i)
print(possibilities)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.