python - Python steamlit 选择框菜单返回字符串,但我需要 dict 或 list
问题描述
在这种情况下堆栈,Python steamlit 选择框菜单返回字符串,但我需要 dict 或 list,以便在我的代码中进一步使用它。我想在下拉菜单中查看 company1、company2、company3,如果用户的选择是例如 'company2' get ['ID': 'zxc222’, 'NAME': 'company2','DESC': 'comp2']
。
BaseObject = [{
'ID': 'zxc123',
'NAME': 'company1',
'DESC': 'comp1'
}, {
'ID': 'zxc222',
'NAME': 'company2',
'DESC': 'comp2'
}, {
'ID': 'zxc345',
'NAME': 'company3',
'DESC': 'comp3'
}]
lenbo = len(BaseObject)
options = []
for i in range(0, lenbo):
options.append((BaseObject[i])['NAME'])
st.selectbox('Subdivision:', options)
解决方案
您可以在dict
之后进行转换selectbox
:
import streamlit as st
BaseObject = [{
'ID': 'zxc123',
'NAME': 'company1',
'DESC': 'comp1'
}, {
'ID': 'zxc222',
'NAME': 'company2',
'DESC': 'comp2'
}, {
'ID': 'zxc345',
'NAME': 'company3',
'DESC': 'comp3'
}]
lenbo = len(BaseObject)
options = []
for i in range(0, lenbo):
options.append((BaseObject[i])['NAME'])
choice = st.selectbox('Subdivision:', options)
chosen_base_object = None
for base_object in BaseObject:
if base_object["NAME"] == choice:
chosen_base_object = dict(base_object)
print(chosen_base_object) # {'ID': 'zxc345', 'NAME': 'company3', 'DESC': 'comp3'}
推荐阅读
- javascript - 在 Javascript 中,我可以使用 While 循环来等待 iframe htm 中的函数加载吗?
- python - 如何计算张量流中不可训练的权重数量?
- html - 固定在 Keith Clarks 视差组内的位置
- javascript - 包含来自 metro4 的 js 库后无法为 JSP 编译类
- javascript - 运行 mocha 时升级 VS Code(1.57.1 之后)后断点未命中
- javascript - 表单提交后重定向用户
- arrays - Mongodb数组和对象在数组中混淆了
- asp.net-core-mvc - 删除身份/帐户/注册
- docker - 从另一台服务器将节点添加到在 docker 上运行的弹性
- java - 如何将kafka connect与java集成,使其可以读取csv文件并将数据转发给消费者?