-2

The title is kind of hard to understand, but here's basically what I'm trying to do. I'm trying to make something that takes a list of variables like this:

 no beta stupid motorbike yes big_boom

and convert it into something like this:

 list = ['no', 'beta', 'stupid', 'motorbike', 'yes', 'big_boom']

The end goal is to search this list of variables and to find ones that conflict with each other. For example:

if exist 'yes' and 'no' in list:
    fail
else:
    pass

Another example, seeing it more in action:

list = [no beta stupid motorbike yes big_boom]
if 'no' and 'yes' exist:
    myVar = 1
#some code to convert list into something like new_list
new_list = ['no', 'beta', 'stupid', 'motorbike', 'yes', 'big_boom']
if myVar = 1:
    #code to delete 'no' from the list, or multiple variables from list
final_list = ['beta', 'stupid', 'motorbike', 'yes', 'big_boom']

#some code to convert back into something like the original list
okay_this_is_the_last_list = [beta stupid motorbike yes big_boom]

print(okay_this_is_the_last_list)

#this should print to the console:
#beta stupid motorbike yes big_boom

It is really complicated, but I appreciate any and all help!

Questions or comments:

https://docs.google.com/document/d/1tRHFHr8bp6_E31lhgYBKrwWb-2qscHoUWb6lVLlY4cc/edit?usp=sharing

  • Under the hood, python treat all `variable` as objects, are you trying to get `references` for those objects? Because if you pass in a list of variables, some of the variables *will* have multiple `reference`, and python wouldn't know what `reference` to choose from. – Rocky Li Oct 23 '18 at 14:40
  • What is this for? I don't normally like to say this but it sounds like you have an [X-Y problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). It can be done by getting [variable names as strings](https://stackoverflow.com/questions/18425225/getting-the-name-of-a-variable-as-a-string/18425523) but you shouldn't really be doing that. There's probably something upstream that you're doing which can be changed so you don't have to do this. – Farhan.K Oct 23 '18 at 14:49
  • Farhan, they are examples, like strings. I basically want to copy and paste a whole bunch of text, and if two things conflict (i.e. big and small) it will default to one of them (exclude small and keep big in the list). – Platinum_Lynx Oct 23 '18 at 14:53
  • Are you sure your list looks like `list = [no beta stupid motorbike yes big_boom]`? Or did you mean `list = "[no beta stupid motorbike yes big_boom]"`? Because the first isn't valid syntax and even it was `list = [no, beta, stupid, motorbike, yes, big_boom]` you're probably trying to solve an XY problem. – Farhan.K Oct 23 '18 at 14:56
  • I'm just gonna make a google doc to try to explain what I'm doing. You can comment on what you're confused on, because now _I'm_ confused. https://docs.google.com/document/d/1tRHFHr8bp6_E31lhgYBKrwWb-2qscHoUWb6lVLlY4cc/edit?usp=sharing – Platinum_Lynx Oct 23 '18 at 15:08

4 Answers4

1
import inspect

# these are your variables
no = 10
beta = 20
stupid = 30
motorbike = 40
yes = 50
big_boom = 60

# this is your list of variables
list1 = [no, beta, stupid, motorbike, yes, big_boom]

# this function takes in a variable name and returns it as a string
def retrieve_name(var):
    callers_local_vars = inspect.currentframe().f_back.f_locals.items()
    return [var_name for var_name, var_val in callers_local_vars if var_val is var][0]

# call retrieve_name() function for each element in list1
list_of_variables = list(map(retrieve_name, list1))

print(list_of_variables)

This will print:

['no', 'beta', 'stupid', 'motorbike', 'yes', 'big_boom']
eiram_mahera
  • 950
  • 9
  • 25
  • Yes, this does help quite a bit, but this should only be a _part_ of the final product. Also, I should be able to make a list that I can just dump a bunch, instead of individually giving _every_ _single_ _one_ a separate value. – Platinum_Lynx Oct 23 '18 at 14:56
0

Just check if all of the items you want to appear in your list with all. If it is, a list comprehension will be the easiest way to give you back your new list with the items you do not want to be removed and finally print your string by joining the items.

li = ['no', 'beta', 'stupid', 'motorbike', 'yes', 'big_boom']
items = ['yes','no'] #items you want in the list before anything happens

if all(item in li for item in items):
    new_li = [i for i in li if i != 'no'] #remove the item you do not need

print(' '.join(new_li)) #print the string output
BernardL
  • 5,162
  • 7
  • 28
  • 47
0

Pretty much a step by step execution of your pseudo-code, use .split() to create the list, then list comprehension to create the final_lst and ' '.join() to piece it back together.

Note: The functionality of this code you designed is heavily dependent on the fact that your input meets the if requirements to create the variables, the code you designed will fail, when those if statements are not triggered due to the fact variables like my_var and final_lst will not get initiated.

s = 'no beta stupid motorbike yes big_boom'

if 'yes' in s and 'no' in s:
    my_var = 1

lst = s.split()

if my_var == 1:
    final_lst = [i for i in lst if i != 'no']

res = ' '.join(final_lst)
print(res)
# beta stupid motorbike yes big_boom
vash_the_stampede
  • 4,590
  • 1
  • 8
  • 20
-1

For your first example I would use this logic.

input = "no beta stupid motorbike yes big_boom"
list = input.split()

if is_in(['yes', 'no'], list):
    print('fail')
else:
    pass

def is_in:(a, b):
    return set(a) < set(b)

The declaration of is_in allows you to just make a list of what you'd like to find in the list then supply it the list. Astring.split()turns it into a list

Jab
  • 26,853
  • 21
  • 75
  • 114