英文:
Streamlit Multiselectbox with lists as options
问题
我想创建一个多选框,用户可以在多个列表之间选择,而不是字符串。
我尝试实现的快速示例:
list_1 = ['1', '2', '3', '4']
list_2 = ['a', 'b', 'c', 'd']
list_3 = ['!', '?', '€', '%']
choice = st.multiselect('选择列表', [list_1, list_2, list_3])
df = df[df['symbol'].isin(choice)]
尽管上面的示例基本上可以工作,但它在逻辑上会显示在选择框内的列表。
但我想要选择一个字符串,使“choice”等于所选列表。
如果选择多个选项,我还需要合并这些列表。
所以例如,如果我选择了 "list_1"
choice = ['1', '2', '3', '4']
如果我选择了 "list_1" 和 "list_3"
choice = ['1', '2', '3', '4', '!', '?', '€', '%']
在这里,我需要一点提示,最佳的方法是什么。
英文:
I want to create a multiselectbox where a user can choose between multiple lists instead of strings.
A quick example what i’m trying to achieve:
list_1 = ['1','2','3','4']
list_2 = ['a','b','c','d']
list_3 = ['!','?','€','%']
choice = st.multiselect('Choose List',[list_1,list_2,list_3])
df = df[df['symbol'].isin(choice )]
While the example above sort of works it will logically display the list inside the selectbox.
But i would like to select a string so “choice” is equal to the selected list.
I would also need to join the lists if multiple options are selected.
So for example if i select "list_1"
choice = ['1','2','3','4']
And if i would select "list_1" and "list_3"
choice = ['1','2','3','4','!','?','€','%']
I need a little hint here what would be the best way to do this.
答案1
得分: 1
如果我正确理解你的想法,你可以使用一个字典,其中键是你的列表的名称,值等于每个列表。
import streamlit as st
list_1 = ['1','2','3','4']
list_2 = ['a','b','c','d']
list_3 = ['!','?','€','%']
lists = {
'List 1': list_1,
'List 2': list_2,
'List 3': list_3
}
choice = st.multiselect('选择列表', lists.keys())
st.write('你选择了:', [lists[x] for x in choice if x in lists.keys()])
selected = []
for i in choice:
selected.extend(lists[i])
st.write("合并你选择的列表", selected)
英文:
If I understand your idea correctly, you can use a dictionary that the keys are the names of your lists and values are equal to each list.
import streamlit as st
list_1 = ['1','2','3','4']
list_2 = ['a','b','c','d']
list_3 = ['!','?','€','%']
lists = {
'List 1': list_1,
'List 2': list_2,
'List 3': list_3
}
choice = st.multiselect('Choose List',lists.keys())
st.write('You selected:', [lists[x] for x in choice if x in lists.keys()])
selected = []
for i in choice:
selected.extend(lists[i])
st.write("Combine lists that you selected", selected)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论