Streamlit 多选框,选项为列表

huangapple go评论72阅读模式
英文:

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)

Streamlit 多选框,选项为列表

Streamlit 多选框,选项为列表

Streamlit 多选框,选项为列表

英文:

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)

Streamlit 多选框,选项为列表

Streamlit 多选框,选项为列表

Streamlit 多选框,选项为列表

huangapple
  • 本文由 发表于 2023年6月29日 23:37:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76582587.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定