[Solved] How to add a label to all words in a file? [closed]

If I understand the correct output format word-O, you can try something like this: words = open(‘filename’).read().split() labeled_words = [word+”-O” for word in words] # And now user your output format, each word a line, separate by tabs, whatever. # For example new lines with open(‘outputfile’,’w’) as output: output.write(“\n”.join(labeled_words)) 3 solved How to add a … Read more

[Solved] Determining the First Digit

raw_input returns a string. Strings are never equal to numbers. >>> ‘0’ == 0 False Compare the string with a string. For example, to check whether the string starts with specific character (sub-string), using str.startswith: if number.startswith(‘0’): … solved Determining the First Digit

[Solved] Base58 Random String generator

In python 2.7 you can use random.choice (singular) and have it executed on a range: x = ”.join(random.choice(‘123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz’) for _ in range(4)) 5 solved Base58 Random String generator

[Solved] how to find the words that are likely to follow the single word in large string in python .?

You can use a regex to look for words that follow the word python, for example >>> import re >>> re.findall(r’Python (\w+)’, s) [‘is’, ‘has’, ‘features’, ‘interpreters’, ‘code’, ‘is’, ‘Software’] Since this list may contain duplicates, you could create a set if you want a collection of unique words >>> set(re.findall(r’Python (\w+)’, s)) {‘Software’, ‘is’, … Read more

[Solved] Print a list with names and ages and show their average age

This also works: pairs = dict([tuple(namn[i:i+2]) for i in range(0, len(namn), 2)]) for name, age in sorted(pairs.items()): print(“%s: %d” % (name, age)) avg_age = sum(pairs.values())/ len(pairs) print(“Average Age: %f” % (avg_age)) Output: Anna: 27 Emelie: 32 Erik: 30 Johanna: 29 Jonas: 26 Josefine: 20 Kalle: 23 Lena: 22 Peter: 19 Average Age: 25.333333 You could … Read more

[Solved] Write a Python function to concatenate all elements (except the last element) of a given list into a string and return the string

def conexclast(strlst): ”’ function to concatenate all elements (except the last element) of a given list into a string and return the string ”’ output = “” for elem in strlst: strng = str(elem) output = output+strng return ‘ ‘.join(strlst[0:-1]) print(“Enter data: “) strlst = raw_input().split() print(conexclast(strlst)) The main correction is splitting the user’s input … Read more

[Solved] Grocery Store for Python

I think the problem is in the part where you insert your item into the cartList. You only insert the item, not the money with it. So in the return part, it must be: if ask == ‘return’: ret = input(‘What item do you want to return?\n’) for i in cartList: if ret==i: … Or … Read more