myDict = \
{
"R1": [10, 20],
"R2": [20, 30],
"K2": [20, 30],
"N2": [20, 30],
"N1": [20, 30],
}假设我用python编写了这本字典。
我想要:
x, y = enumerate(myDict["R1"])
#doing some job over data
#at here x will be 10 and y will 20
x, y = enumerate(myDict["R2"])
#doing some job over data这只是一个小判决。但是我有太多的数据了。因此,与其为R1、R2、R3编写代码,还不如...有没有像这样的方法?
for x,y someCodeForR1toRn:
#doing some job over data发布于 2020-09-26 19:01:51
添加一个循环遍历数组的外部循环和一个仅使用R*的条件
for key, value in myDict.items():
if not key.startswith("R"):
continue
print("Doing stuff for", key)
for x, y in enumerate(value):
print(x, y)发布于 2020-09-26 19:04:58
只需循环遍历字典项:
myDict = {"R1": [10, 20],"R2": [150, 250],"N2": [150, 250]}
def key_is_interesting(k):
return k[0] == 'R'
for k,v in myDict.items():
if key_is_interesting(k):
print(f'Doing some work with {k} and {v}')发布于 2020-09-26 19:05:40
尝试:
for key, (x, y) in myDict.items():
print(f'The key {key} has values x={x} and y={y}')https://stackoverflow.com/questions/64076850
复制相似问题