[英]How can a non-integer value be used as the index of a (2D) list?
If we would like to address a location in a 2D list in python with non-integer values, for example with a string in the form of a tuple, how can the indexing be handled?如果我们想用非整数值来寻址 python 中二维列表中的位置,例如使用元组形式的字符串,如何处理索引? I am using dictionary as a solution, but I am not sure if it is the best solution.
我正在使用字典作为解决方案,但我不确定它是否是最佳解决方案。
Exampe:示例:
mylist = {'a1':{"('p1','p2')":10, "('p1','p3')":11}, 'a2':{"('p4','p5')":12, "('p4','p6')":13}}
and now:现在:
mylist['a1']["('p1','p2')"] = 20
I think it is not an efficient solution.我认为这不是一个有效的解决方案。 Any other solution please?
请问还有什么解决办法吗?
What I understand is that you're trying to make the second dimension tuple-shaped.我的理解是,您正在尝试使第二维元组形状。 So, what if you just turn the strings into tuples?
那么,如果你只是把字符串变成元组呢?
For example:例如:
>>> mylist = {'a1': {('p1','p2'): 10, ('p1','p3'): 11}, 'a2': {('p4','p5'): 12, ('p4','p6'): 13}}
>>> mylist['a1'][('p1','p2')]
10
I cannot comment on your posts at this time because my reputation score is not enough.我目前无法评论您的帖子,因为我的声誉分数不够。 Oops.
哎呀。 :)
:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.