This question already has answers here:
How do I append one string to another in Python?
(12 answers)
How to concatenate (join) items in a list to a single string
(11 answers)
Which is the preferred way to concatenate a string in Python? [duplicate]
(12 answers)
Closed last month.
Im new to Python, like around an hour and a half into it new.. ive crawled my website using cewl to get a bespoke wordlist for password audits, i also want to combine randomly 3 of these words together.
IE Cewl wordlist ;
word1
word2
word3
word4
using a python script i want to further create another wordlist randomly joining 3 words together IE
word4word2word1
word1word3word4
word3word4word2
so far all ive come up with is;
import random
print(random.choice(open("test.txt").read().split()))
print (random.choice(open("test.txt").read().split()))
print(random.choice(open("test.txt").read().split()))
Whilst this is clearly wrong, it will give me 3 random words from my list i just want to join them without delimiter, any help for a complete novice would be massively appreciated
First thing to do is only read the words once and using a context manager so the file gets closed properly.
with open("test.txt") as f:
lines = f.readlines()
Then use random.sample to pick three words.
words = random.sample(lines, 3)
Of course, you probably want to strip newlines and other extraneous whitespace for each word.
words = random.sample([x.strip() for x in lines], 3)
Now you just need to join those together.
Using your code/style:
import random
wordlist = open("test.txt").read().split()
randomword = ''.join([random.choice(wordlist), random.choice(wordlist), random.choice(wordlist)])
print(randomword)
join is a method of the string type and it will join the elements of a list using the string as a delimiter. In this case we use an empty string '' and join a list made up of random choices from your test.txt file.
Related
This question already has answers here:
How to extract the substring between two markers?
(22 answers)
Closed last month.
I'm having a problem. I need to create the #everyone_or_person feature. A bit like discord. But I'll have to be able to read the word after the # and stop reading when there is a ("SPACE"/"_") and check for that word in the list. I've appended a simple version as an example. I knew it would not work but I couldn't think of anything else.
input = input("input: ")
value = input.find("#")
output = input.partition("#")[0]
print(str(output))
I've tried to look up how to do it but to no avail.
simply use split:
test = "Some input with #your_desired_value in it"
result = test.split("#")[1].split(" ")[0]
print(result)
this splits your text at the #, takes the entire string after the #, splits again at the first space, and takes the string before that.
This question already has answers here:
How can I check if two strings are anagrams of each other?
(27 answers)
Closed 11 months ago.
I want to write a function that finds anagrams. Anagrams are words that are written with the same characters. For example, "abba" and "baba".
I have gotten as far as writing a function that can recognize if a certain string has the same letters as another string. However, I can't account for the number of repeated letters in the string.
How should I do this?
This is the code I have written so far:
def anagrams(word, words):
list1 = []
for i in words:
if set(word) == set(i):
list1.append(i)
return list1
The inputs look something like this:
('abba', ['aabb', 'abcd', 'bbaa', 'dada'])
I want to find an anagram for the first string, within the list.
You kind of point to the solution in your question. Your current problem is that using a set, you ignore the count of times each individual letter is contained in the input and target strings. So to fix this, start using these counts. For instance you can have a mapping between letter and the number of its occurrences in each of the two strings and then compare those mappings.
For the purposes of learning I would like to encourage you to use dict to solve the problem. Still after you know how to do that, there is a built-in container in collections, called Counter that can do the same for you.
This question already has answers here:
Modifying a list while iterating when programming with python [duplicate]
(5 answers)
Closed 2 years ago.
I am trying to write a code that would loop through elements in a list of strings and combine the elements that start with a lower case letter with a previous element. For example, given this list:
test_list = ['Example','This is a sample','sentence','created to illustrate','the problem.','End of example']
I would like to end up with the following list:
test_list = ['Example','This is a sample sentence created to illustrate the problem.','End of example']
Here is the code I have tried (which doesn't work):
for i in range(len(test_list)):
if test_list[i].islower():
test_list[i-1:i] = [' '.join(test_list[i-1:i])]
I think there might be a problem with me trying to use this join recursively. Could someone recommend a way to solve this? As background, the reason I need this is because I have many PDF documents of varying sizes converted to text which I split into paragraphs to extract specific items using re.split('\n\s*\n',document) on each doc. It works for most docs but, for whatever reason, some of them have '\n\n' literally after every other word or just in random places that do not correspond to end of paragraph, so I am trying to combine these to achieve a more reasonable list of paragraphs. On the other hand, if anyone has a better idea of how to split raw extracted text into paragraphs, that would be awesome, too. Thanks in advance for the help!
you could use:
output = [test_list[0]]
for a, b in zip(test_list, test_list[1:]):
if b[0].islower():
output[-1] = f'{output[-1]} {b}'
else:
output.append(b)
output
output:
['Example',
'This is a sample sentence created to illustrate the problem.',
'End of example']
This question already has answers here:
Iterate over the lines of a string
(6 answers)
Closed 7 years ago.
I noticed when I try to iterate over a file with lines such as
"python"
"please"
"work"
I only get individual characters back, such as,
"p"
"y"
"t"...
how could I get it to give me the full word? I've been trying a couple hours and can't find a method. I'm using the newest version of python.
Edit: All the quotation marks are new lines.
You can iterate over a file object:
for line in open('file'):
for word in line.split():
do_stuff(word)
See the docs for the details:
http://docs.python.org/2/library/stdtypes.html#bltin-file-objects
If you are storing the words as a string, you can split the words by space using split function.
>>> "python please work".split(' ')
['python', 'please', 'work']
If you have your data in a single string which spans several lines (e.g. it contains '\n' characters), you will need to split it before iterating. This is because iterating over a string (rather than a list of strings) will always iterate over characters, rather than words or lines.
Here's some example code:
text = "Spam, spam, spam.\Lovely spam!\nWonderful spam!"
lines = text.splitlines() # or use .split("\n") to do it manually
for line in lines:
do_whatever(line)
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Python: Split string with multiple delimiters
I have a program where I am parsing a file by each line and splitting it into two half. After that for each half I am parsing each word from the line and appending it in a list.
Here mfcList1 is a list of lines from a text file. I am parsing each word in the line that are either separated by a comma or by a space. But it isn't exactly working.
for lines in mfcList1:
lines = lines.lstrip()
if lines!='':
p.append(string.split(lines,','or " "))
mfcList2 = reduce(lambda x,y:x+y,p)
print mfcList2
When i am using string.split it is working with only those elements who end with a comma it is ignoring the or operator I am using with split method. I want to slice off each and everyword from the line. They either end with a comma or with a space.
for eg. 'enableEmergencySpare=1 useGlobalSparesForEmergency=1 useUnconfGoodForEmergency=1',
this line is being stored as a single list element where as I am trying to split them using split method..
Can anyone pls suggest what i can do instead of using or operator... thanks..
You can use split() from the re module:
import re
...
p.extend(re.split('[ ,]', lines))
The [ ,] is a regular expression which means "a space or a comma". Also, assuming p is a list and you want to add all the words to it, you should use extend() rather than append(), the latter adds a single element.
Note also that if a line in the file contains command followed by space (or other sequence of commas and spaces) your list p will contain a corresponding number of empty strings.