Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am trying to select a random word that starts with a hashtag, Here is my current code and I have tried random.choice(), but it will only select one letter. I am guessing its because I am storing the results as one big string, maybe it needs a dictionary but I am still a little unsure how to do it.
trends1 = api.trends_place(1) # from the end of your code
# trends1 is a list with only one element in it, which is a
# dict which we'll put in data.
data = trends1[0]
# grab the trends
trends = data['trends']
# grab the name from each trend
names = [trend['name'] for trend in trends]
# put all the names together with a ' ' separating them
trendsName = ' '.join(names)
This returns
#NaFaltaDoQueFazerEu #Aliyaİzzetbegoviç #LawinPH + more
I want to randomly select a hashtag, but at the minute its choosing a random letter from the string, i.e. doing
random.choice(trendsName)
returns a random letter instead of a random hashtag
You would just need to call random.choice() on names instead of trendsName:
import random
random.choice(names)
random.choice() returns an element of a sequence. If your sequence is a string, then that element will be a single character from that string. However, if your sequence is a list, then that element will be a member of the list, which in the case of names, looks like it will be a hashtag.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 13 days ago.
Improve this question
I currently have two lists
songlist = []
artistlist = []
I return
list(zip(songlist, artistlist))
and it gives me what I want(a 3d list of a 2d list containing each song title and respective artist) but before each second item in each list is "\n"
for example a list in the 3d list would look like: ["Amarillo By Morning","\nGeorge Strait"]
I'm gonna be using the artist in a later function so I'd rather there not be the \n there.
instead of my above code i tried returning a variable containing:
[i + j for i, j in zip(songlist, artistlist)]
but that just returned: ["Amarillo By Morning\nGeorge Strait", ... cont.
To avoid making another iterator, you can just modify each artist as they come along, if you can't modify it at the source.
Also, return a list instead of using string concatenation if you want a list.
[[song, artist.strip()] for song, artist in zip(songlist, artistlist)]
Use the strip() method to remove the whitespace characters from the start and the end of the string (including the new line characters) :
zipped_list = list(zip([song.strip() for song in songlist], [artist.strip() for artist in artistlist]))
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
The following is my code:
string1 = (input("What is your name?")) #creates a string, stores the name in it
first1 = string1[:1]
string2 = (input("What is your last name?"))
first3 = string2[:3]
from random import randint
sentence = "".join((str(randint(0,9)), first1.lower(), first3.upper()))
print (sentence)
sentence = "".join((str(randint(0,9)), first1.lower(), first3.upper()))
print (sentence)
It works, but I am having some trouble. I need to loop this 5 times - but it doesn't work for some reason!
P.S. Python 3!
You are creating a tuple called sentence, rather than a string
If you change that line to this:
sentence = "".join((str(randint(0,9)), first1.lower(), first3.upper()))
It will create a string that has no gaps, like so when printed:
What is your name?First
What is your last name?Last
5fLAS
You are creating a list, not a string so it seems logical that you get issues when trying to print it...
To append to a string you can do that :
sentence = str(randint(0,9))+(first1.lower())+(first3.upper())
In Python, you don't give a type to your variables, it depends of what you put INTO them.
In Python, elements between parenthesis "()" are considered as TUPLE (a type similar to a list, but that cant be modified), "[]" stands for a list, "{}" stands for a dictionnary. So you must NOT put parts of a string between parenthesis and separated with commas or you will turn them into a list of words. Instead you can use "+" that means to Python that you are making a string concatenation. Also note that i casted your random number into string. If you give 3 strings separated by "+" Python will automatically detect sentence as a String. Wp you've created a string !
If you have a list and want to get rid of the spaces you can also use :
"".join(yourlist)
However i don't recommend it here, you would be creating a list and using a join for nothing since you can create a string from the begining.
Edit: As for the loop, we can't answer if you don't copy paste the code of the loop and the error, it could be a million things.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have a .txt file that has a really long RNAm sequence. I don´t know the exact length of the sequence.
What I need to do is extract the part of the sequence that is valid, meaning it starts with "AUG" and ends in "UAA" "UAG" or "UGA". Since the sequence is too long I don´t know the index of any of the letters or where the valid sequence is.
I need to save the new sequence in another variable.
Essentially, what you need to do, without coding the whole thing for you, is:
Example string:
rnaSequence = 'ACGUAFBHUAUAUAGAAAAUGGAGAGAGAAAAUUUGGGGGGGAAAAAAUAAAAAGGGUAUAUAGAUGAGAGAGA'
You will want to find the index of the 'AUG' and the index of 'UAA', 'UAG', or 'UGA' .. Something like this
rnaStart = rnaSequence.index(begin)
Then you'll need to set the slice of the string to a new variable
rnaSubstring = rnaSequence[rnaStart:rnaEnd+3]
Which in my string above, returns:
AUGGAGAGAGAAAAUUUGGGGGGGAAAAAAUAA
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
With the following expected input:
[u'able,991', u'about,11', u'burger,15', u'actor,22']
How can I split each string by the comma and return the second half of the string as an int?
This is what I have so far:
def split_fileA(line):
# split the input line in word and count on the comma
<ENTER_CODE_HERE>
# turn the count to an integer
<ENTER_CODE_HERE>
return (word, count)
One of the first things you'll need in learning how to code, is to get to know the set of functions and types you have natively available to you. Python's built-in functions is a good place to start. Also get the habit of consulting the documentation for the stuff you use; it's a good habit. In this case you'll need split and int. Split does pretty much what it says, it splits a given string into multiple tokens, given a separator. You'll find several examples with a simple search in google. int, on the other hand, parses a string (one of the things it does) into a numeric value.
In your case, this is what it means:
def split_fileA(line):
# split the input line in word and count on the comma
word, count = line.split(',')
# turn the count to an integer
count = int(count)
return (word, count)
You won't get this much here in stackoverflow, has other users are often reluctant to do your homework for you. It seems to me that you are at the very beginning of learning how to code so I hope this helps you get started, but remember that learning is also about trial and error.
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 fairly new to python
One of the exercises I have been given is to create the python pseudo code for the following problem:
write an algorithm that given a dictionary (list) of words, finds up to 10 anagrams of a given word.
I'm stuck on ideas on how to solve this.
Currently I have (it's not even proper pseudo)
# Go through the words in the list
# put each letter in some sort of array
# Find words with the letters from this array
I guess this is way too generalistic, I have searched online for specific functions I could use but have not found any.
Any help on specific functions that would help, in making slightly more specified pseudo code?
Here is some help, without writing the code for you
#define a key method
#determine the key using each set of letters, such as the letters of a word in
#alphabetical order
#keyof("word") returns "dorw"
#keyof("dad") returns "add"
#keyof("add") returns "add"
#ingest the word set method
#put the word set into a dictionary which maps
#key->list of up to 10 angrams
#get angrams method
#accept a word as a parameter
#convert the word to its key
#look up the key in the dictionary
#return the up to 10 angrams
#test case: add "dad" and "add" to the word set.
# getting angrams for "dad" should return "dad" and "add"
#test case: add "palm" and "lamp" to the word set.
# getting angrams for "palm" should return "palm" and "lamp"
#consider storing 11 angrams in the list
#a01, a02, a03, a04, a05, a06, a07, a08, a09, a10, a11.
#Then if a01 is provided, you can return a02-a11, which is 10 angrams