comparing string to class list in python [closed] - python

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am trying to figure out how to compare a string to the 0th element in the list (technically the bottom). The list is also a class instance and looks like this:
#define class called GEL
class GEL:
def __init__(self,eventtime,type):
self.eventtime=eventtime
self.type=type
myList=[]
And when I add something to the list:
myList.append(GEL(time,type)) ##Type can be either 'Arrival' or 'Departure'
For which the list will be (1.343432,'Arrival')
So I want to compare 'Arrival' with the type item in the list.
for i in range(5): ##loop for insertions and deletions
Type=[a.type for a in myList] ##Actually goes to the last type, but want first
if 'Arrival' in Type:
##DO STUFF
else:
##HELLO WORLD
#sortlist
myList.pop(0)
What would be the correct way just get the first type in the list?
Sorry for the poor jargon, am still learning Python.
EDIT: I think I may have solved. It gets me what I want. If anyone could tell me if this would be ok:
if 'Arrival' in Type[0]:

I think you just need this
if mylist[0].type == 'Arrival':

Related

What can be the most efficient way to remove first 10 elements from tuple?(every elements) [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last year.
Improve this question
THE PROBLEM
Hi, I am currently learning python and opencv. I want to get only name of the dog species from the above pic, which removing the first 10 strings and numbers after the dog species.
For example:
[('n00000000-Maltese_dog', 252')] to 'Maltese_dog'.
I would appreciated if you can provide me any information of removing 'n' elements from tuple, or any help.
Thank you! Hope yall have a great day
Don't think of it in terms of removing items, think of extracting the data you want.
For each tuple, t, in the list take the first item t[0] and then take a slice of the string t[0][10:]. You can use a list comprehension to make a new list of all the strings:
l = [
('n00000000-Maltese_dog', '252'),
('n10030000-Australian terrier', '252')
]
[t[0][10:] for t in l]
# ['Maltese_dog', 'Australian terrier']

how can I solve this in python? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
Id codes at a company come in the form x-y-zzzzzz, where x is a digit and y is a letter and zzzzzz represents a string of 6 letters. Write a function which takes in a code as an input (e.g. 3-a-abaabb) and returns the zzzzzz part (e.g. abaabb).
I have no idea how to start and solve this question. any help would be much appreciated. My IDE is pycharm (solving python coding problems) I basically need to create a function which takes the code as an input and will return the last 6 letters
You can use str.spit('-') then search count in code with repeat is equal or not, like below:
def fnd_code(code):
repeat, char, search = code.split('-')
return search.count(char) == int(repeat)
print(fnd_code('3-a-abaabb'))
print(fnd_code('4-a-abaabb'))
Output:
True
False

How do I turn a list (with 1 item in it) into a variable? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I'm doing a python project and I don't know how to turn a list into a variable.
Here is my code so far:
list = ['Name1']
I want to turn the list into a variable. Is there a way I can do it?
l = ['Name1']
name = l[0]
After this, name will be 'Name1'.
Yes, for example using astype():
variable_string = list.astype(str)
I don't think you can actually change a list to a 'variable' as a list is already a variable.
You have kind of two paths.
Somehow get the index of the variable you want to get and pass this into a variable
E.g. If I wanted a first object in the list I would do:
my_variable = list[0]
Just use the list index as your variable
print(list[0])

Interactive Dictionary, TypeError: 'dict' not callable? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
import json
data = json.load(open("files1\data.json"))
def definitioner(w):
return data(w)
word = input("Enter the word you are looking for: ")
print(definitioner(word))
I am doing a course on UDEMY and after trying it myself it didn't work so I even copied the code to see if it was my code, couldn't figure out what the issue was, any help would be appreciated. I am running Python 3.8
Thanks.
You are calling data(w) like it's a function, but data is a dictionary. Use data.get(w) instead:
def definitioner(w):
return data.get(w)
That also allows you to specify what you would like returned by default if the word is not present, by adding a second argument:
def definitioner(w):
return data.get(w, 'Word not found!')

taking a value from double list and making it a menu [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have a list of data that reads
[['name','emailtype','phonetype'],['john','yahoomail', 'mobile'],['mark','yahoo','landline']]
I can manually pick out the values i.e print dL[0][0] prints name and dL[1][0] emailtype.
Is it possible to isolate all the names from the list. i.e john and mark. With a program / module and then print them
and produce them into something like this:
1) John
2) Mark
so that I can ask for a raw_input and then if I press 1 as selection it produces john as the answer.
so that it reads similar to the nicely written data that I can manually type as above.
You want to slice the list (to ignore the first row), then use a list comprehension to pick out just the first element of each nested list:
[row[0] for row in nested_list[1:]]

Categories

Resources