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 7 years ago.
Improve this question
I just started to learn Python and I am confused in its syntax.
I am not sure what square brackets around the variable word mean... I understand word should be our value and prev is a key.
mimic_dict[prev] = [word]
I also do not understand this expression
mimic_dict[prev] = mimic_dict.get(prev, []) + [word]
Need your help to clarify this.
I checked out documentation for python dictionary, but it was not much of help.
Sorry for such a basic question, but I really could not figure it out by googling.
mimic_dict[prev] = [word]
[word] is a list containing a single element, the value of the word variable.
mimic_dict.get(prev, []) + [word]
+ can be used to concatenate two lists. This adds word to the list returned by mimic_dict.get(prev, []).
And what does mimic_dict.get(prev, []) do? It is the same as mimic_dict[prev], but if the key prev doesn't exist then it returns a default value of [] (an empty list). It ensures you always get a list back whether or not the key exists.
Related
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 1 year ago.
Improve this question
I'm making an AI, and I want it to be able to search things. I don't know how to do this efficiently though.
Edit again: I fixed it on my own.
Instead of taking the second word inside of a list which contains every word the output has and added it to a search query. I took the output and removed the first word from it (which is "find", or "search") and then it will add that to the search query. Thanks for all the help.
If that's really all you want:
words = output.split(' ', 1)
if words[0] == 'search' and words[1] in Terms:
print("win")
For starters, you could forget about that long if condition and try this:
Terms = ['music','gaming','Minecraft','dank desmondo']
output = "search music"
if output.split()[1] in Terms:
print(True)
else:
print(False)
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])
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
Could you please tell me why it is considered as "not pythonic" when I need the index and the value when looping over a list and use:
a = [1,2,3]
for i in range(len(a)):
# i is the idx
# a[i] is the value
but rather it is recommended to use
for idx, val in enumerate(a):
print idx, val
who defines "pythonic" and why is the latter one better? I mean it's not that much better concerning readability, is it!?
Thanks in advance
First of all, the first way is ugly: You either need a separate variable assignment to get the element or use a[i] all the time which could theoretically be an expensive operation. Imagine a being a database cursor: When you iterate it (a.__iter__ being called) the object can safely assume that you are going to iterate over all its items. So all or at least multiple rows could be retrieved at once. When getting the length such an optimization would be stupid though since you surely don't want to retrieve data just because you want the number of items. Also, when retrieving a specific item you cannot assume that other items will be retrieved, too.
Additionally, using enumerate() works with any iterable while range(len()) only works with countable, indexable objects.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
Could you please tell me why it is considered as "not pythonic" when I need the index and the value when looping over a list and use:
a = [1,2,3]
for i in range(len(a)):
# i is the idx
# a[i] is the value
but rather it is recommended to use
for idx, val in enumerate(a):
print idx, val
who defines "pythonic" and why is the latter one better? I mean it's not that much better concerning readability, is it!?
Thanks in advance
First of all, the first way is ugly: You either need a separate variable assignment to get the element or use a[i] all the time which could theoretically be an expensive operation. Imagine a being a database cursor: When you iterate it (a.__iter__ being called) the object can safely assume that you are going to iterate over all its items. So all or at least multiple rows could be retrieved at once. When getting the length such an optimization would be stupid though since you surely don't want to retrieve data just because you want the number of items. Also, when retrieving a specific item you cannot assume that other items will be retrieved, too.
Additionally, using enumerate() works with any iterable while range(len()) only works with countable, indexable objects.
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':