我有一个字典列表
[{"id":1, "name": "jane", "contact": {"email": "jane@gmail.com", "phone": "8179482938"}},
{"id":2, "name": "john", "contact": {"email": "john@gmail.com", "phone": "8179482937"}},
{"id":3, "name": "june", "contact": {"email": "june@gmail.com", "phone": "8179482936"}}]
如何仅根据 jane 的电话号码 (8179482938) 找到她的索引?
答案 0 :(得分:1)
比较简单。您遍历列表,直到找到您想要的那个。
def get_name(phone):
for i, row in enumerate(list_of_dicts):
if row['contact']['phone'] == phone:
return i
答案 1 :(得分:0)
这个:
book = [{"id":1, "name": "jane", "contact": {"email": "jane@gmail.com", "phone": "8179482938"}},
{"id":2, "name": "john", "contact": {"email": "john@gmail.com", "phone": "8179482937"}},
{"id":3, "name": "june", "contact": {"email": "june@gmail.com", "phone": "8179482936"}}]
for item in book:
if item['contact']['phone'] == '8179482938':
print(item['id'])
答案 2 :(得分:0)
要获取索引,您可以执行以下操作( const classes = useStyles();
return (
<main className={classes.content}>
<div className={classes.toolbar} />
<Grid container justify ="center" spacing={3}>
{() => products.map((product) => (
<Grid item key={product.id}xs ={3} sm={6} md={4}>
<Product product={product}/>
</Grid>
))}
</Grid>
</main>
)
}
export default Products
是您的问题列表):
lst
打印:
idx = next(i for i, d in enumerate(lst) if d["contact"]["phone"] == "8179482938")
print(idx)