简体   繁体   English

如何将非整数值用作(2D)列表的索引?

[英]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.

相关问题 具有非整数索引的Python Numpy 2d数组 - Python Numpy 2d array with non-integer index Python:通过非整数因子下采样2D numpy数组 - Python: downsample 2D numpy array by a non-integer factor 排序非整数2D numpy数组 - Sorting a non-integer 2D numpy array 尝试从yahoo股票数据中打印出第一个高价值,得到“基于非整数索引的基于索引的索引只能具有非整数索引器” - Trying to print the first high value from yahoo stock data, getting “At based indexing on an non-integer index can only have non-integer indexers” 如果索引是非整数值,如何使用 .loc 或 .iloc 函数提取整行 - how to extract whole row using .loc or .iloc function if index is non-integer value 类型错误:无法使用非整数键按位置索引进行索引 - TypeError: Cannot index by location index with a non-integer key 无法使用非整数键按位置索引进行索引 - Cannot index by location index with a non-integer key TypeError:无法在 for 循环中使用非整数键按位置索引进行索引 - TypeError: Cannot index by location index with a non-integer key in for loop 如何在 jit 编译的 jax 代码中执行非整数索引运算? - How to perform non-integer index arithmetic in jit-compiled jax code? 如何在 1d numpy 数组中执行非整数平均池化? - How to perform non-integer average pooling one a 1d numpy array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM