Display variable label instead of value - pytorch

I created two lists as dropdown menus in streamlit.
list = [2, 3, 5, 7, 11]
list1 = [1, 2, 5, 7, 11]
select_options = [list,list1]
dropdown = st.selectbox('Select Option', options=select_options)
Right now the dropdown menu displays 2,3,5,7,11 & 1,2,5,7,11. Is there a way to display the variable label instead so the dropdown menu would show list & list1?

You can have two select boxes, the first selectbox is to make the choice as to which list you want to use and the second selectbox will let you make the choice of the elements in the selected list.
Note: It's not appropriate to name a variable after list because it is a python built-in function. So I renamed your list variable to list1
import streamlit as st
list1 = [2, 3, 5, 7, 11]
list2 = [1, 2, 5, 7, 11]
get_list = st.selectbox('Select List Option', ("List1", "List2"))
select_options = []
if get_list == "List1":
select_options = list1
elif get_list == "List2":
select_options = list2
dropdown = st.selectbox(f'Select Option of {get_list}:', select_options)
Output:

Related

how to check items of one list in another in python with loops

I want to check for a certain condition list and then print it
suppose i want to check items of a in b
I want to check for values of list "a" in list "b" so i used this code
a=[1,2]
b=[1,2,3,4,5,6,7,8,9,]
c=[]
for i in a:
for j in b:
if i!=j:
c.append(j)
but i am getting infinite loop when i run this code code please help me.
It is unclear what behavior you would like to implement, but I guess you want to filter out elements in b that appear in a. If so, you can do this:
a = [1, 2]
b = [1, 2, 3, 4, 5, 6, 7, 8, 9]
c = []
for i in b:
if not i in a:
c.append(i)
print(c)
The result:
[3, 4, 5, 6, 7, 8, 9]
**If you want to recover
identical elements in each list then try this code:**
c = []
def list_contains(a, b):
for x in a:
for y in b:
if x == y:
c.append(x)
continue
return c
a = [1, 2]
b = [1, 2, 3, 4, 5, 6, 7, 8, 9, ]
print(list_contains(a, b))
Let's use some list comprehension!
I'll rename your lists:
list_a = [1, 2]
list_b = [1, 2, 3, 4, 5, 6, 7, 8, 9]
So you want a list of everything in list_b...
result = [elem for elem in list_b] # (this just copies the list)
...where the element is also in list_a:
result = [elem for elem in list_b if elem in list_a]
...where the element is not in list_a:
result = [elem for elem in list_b if elem not in list_a]
Alternatively, make a function and use filter:
def keep_b_if(b_elem):
return b_elem in list_a
# or
return b_elem not in list_a
result = filter(keep_b_if, list_b)
Also, if a is just a collection of values to preserve/lose from b, it can be a set instead of a list:
forbidden = {1, 2}
...
if elem not in forbidden:
...

How to convert the dictionary containing keys and list of list value?

How to convert the dictionary containing keys and list of list values to dictionary containing keys and list, drop duplicated in the newly created list ?
I tried running the following function and the computer got an 'memory error'
from collections import defaultdict
my_dict = defaultdict(list)
for k, v in zip(df_Group.guest_group, df_Group.list_guest):
for item in v:
v.append(item)
my_dict[k].append(set(v))
My origin dictionary created from 2 columns of one dataframe like: {Group1: [[1,2,3,4], [1, 2, 5, 6 ]]}
I want my dictionary like : {Group1: [1,2,3,4,5,6]}
From what I understood, what you essentially want to do is flatten your list while keeping unique items.
The unique items can be achieved by converting the list into set and then back to list.
The unpacking part is really well explained in this post. Here's a working code for you -
df_dict = {
'Group1': [[1,2,3,4], [1, 2, 5, 6 ]],
'Group2': [[1,2], [2,3],[2,3,4]]
}
final_dict = {}
for k, v in df_dict.items():
# flatten the list using double list comprehension
flat_list = [item for sublist in v for item in sublist]
final_dict[k] = list(set(flat_list))
This gives the final_dict as -
{'Group1': [1, 2, 3, 4, 5, 6], 'Group2': [1, 2, 3, 4]}
Please tell me if this answers your query.
Edit for Integer values in between the lists -
If we have a list with integer values in between, then you will get the int object not iterable error, to solve it we can check the instance to be int and make the item list by ourselves
Working code -
df_dict = {
'Group1': [[1,2,3,4], 3, [1, 2, 5, 6 ]],
'Group2': [[1,2], [2,3],[2,3,4]],
}
final_dict = {}
for k, v in df_dict.items():
# making a new list
new_list = []
for item in v:
# for int we will convert it to 1 length list
if isinstance(item, int):
item = [item]
for ele in item:
new_list.append(ele)
final_dict[k] = list(set(new_list))
final_dict
Final dict -
{'Group1': [1, 2, 3, 4, 5, 6], 'Group2': [1, 2, 3, 4]}
As expected

How to randomly select elements from an array in python?

I need to randomly select elements from a list. Currently, I will sometimes select too many copies of an element from the original list eg:
Original List: [0, 1, 2, 3, 4]
3 Randomly Selected Elements: [4, 4, 4]
I do not want multiple 4s selected if there was only 1 in the original list.
What should I do to not take more copies of a value than exists in the first array?
A solution is to remove the elements from the original list when you select them.
import random
original_list = [1, 2, 2, 3, 3, 3]
number_of_random_selections = 4
random_selections = []
for i in range(0, len(original_list)):
random_index = random.randint(0, number_of_random_selections)
random_selection = original_list[random_index]
random_selections.append(random_selection)
original_list.remove(random_selection)
print(random_selections)

List comprehension for appending 2 elements of 2 different lists next to each other

Is it possible to create a list comprehension, to create a list where 2 elements of 2 different lists are appended next to each other?
For instance, if you have 2 lists:
suits = ["Hearts", "Spades", "Clubs", "Diamonds"]
value_suits = ["Ace", 2, 3, 4, 5, 6, 7, 8, 9, 10, "Jack", "Queen", "King"]
# 1 element at a time for 1 list is possible in a list comprehension:
deck = [value for value in value_suits]
#but can you also do something like this(with correct syntax):
deck = [suit,value for suit,value in suits,value_suits]
#such that you get the same output as:
deck = []
for suit in ["Hearts", "Spades", "Clubs", "Diamonds"]:
for value in ["Ace", 2, 3, 4, 5, 6, 7, 8, 9, 10, "Jack", "Queen", "King"]:
deck.append(str(value)+" "+suit)
You can have nested list comprehensions:
deck = ['{} {}'.format(value, suit) for suit in suits for value in value_suits]
You can also use itertools.product:
from itertools import product
deck = ['{1} {0}'.format(*sv) for sv in product(suits, value_suits)]

How to replace every element in a given list to its square and not create a new list with the squares

I want to make a function that takes a list of integers and replaces the elements in the list with their respective squares.
I tried reassigning every element by virtue of its position (index) in the list, but for some reason the second element in the list gets squared twice.
def square_list(list1):
for i in list1:
list1[list1.index(i)] = i**2
print(list1)
square_list([1, 2, 3, 4, 5])
I expect the printed list to be [1, 4, 9, 16, 25] since the list I'm testing the function with is [1, 2, 3, 4, 5].
If a function is required to square list elements in-place, use it:
def square_list(list_1):
for i in range(len(list_1)):
list_1[i] = list_1[i]**2
my_list = [1, 2, 3, 4, 5]
square_list(my_list)
print(my_list)
Since the function doesn't return anything, square_list([1, 2, 3, 4, 5]) is useless.
Python's map built-in function is good for this, so you don't really have to write your own function.
l = [1, 2, 3, 4, 5]
l = list(map(lambda x: x**2, l))

Resources