Is there a built-in method / module in Python to generate letters such as the built-in constant LETTERS or letters constant in R?
The R built-in constant works as letters[n] where if n = 1:26 the lower-case letters of the alphabet are produced.
Thanks.
It's called string.ascii_lowercase.
If you wanted to pick n many random lower case letters, then:
from string import ascii_lowercase
from random import choice
letters = [choice(ascii_lowercase) for _ in range(5)]
If you wanted it as a string, rather than a list then use str.join:
letters = ''.join([choice(ascii_lowercase) for _ in range(5)])
You can use map as in the following:
>>> map(chr, range(65, 91))
['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
>>> map(chr, range(97, 123))
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
>>> a = map(chr, range(65, 70))
>>> a
['A', 'B', 'C', 'D', 'E']
With list comprehensions and reference from the above, there is another method:
>>> [chr(x) for x in range(97, 123)]
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
Yet another way to do it which will give you directly a string:
>>> bytearray(range(97,123)).decode("utf-8")
u'abcdefghijklmnopqrstuvwxyz'
(it works with both python2 and python3, of course the u prefix won't be visible if it's python 3)
You can obviously tranform that string into a list like in other answers if that is what you prefer, for instance with:
>>> [x for x in bytearray(range(97,123)).decode("utf-8")]
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
It is also very easy to change it to pick n random letters (repetition allowed):
>>> import random
>>> n = 10
>>> bytearray(random.randint(97, 122) for x in range(n)).decode('utf-8')
'qdtdlrqidx'
Or without repetition:
>>> import random
>>> n = 10
>>> bytearray(random.sample(range(97, 123),n)).decode('utf-8')
'yjaifemwbr'
Related
normal = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
'w', 'x', 'y', 'z']
modified = ['s', 'n', 'v', 'f', 'r', 'g', 'h', 'j', 'o', 'k', 'l', 'a',
'z', 'm', 'p', 'q', 'w', 't', 'd', 'y', 'i', 'b', 'e', 'c', 'u', 'x']
word = input()
for char in word:
if char in normal:
char.replace(char, modified)
This is what i have so far,
I want to be able to type in a sentence and it will output the sentence with the modified alphabets
one of the way to map the normal list character with the modified list character and replace them in the sentence.
normal = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
'w', 'x', 'y', 'z']
modified = ['s', 'n', 'v', 'f', 'r', 'g', 'h', 'j', 'o', 'k', 'l', 'a',
'z', 'm', 'p', 'q', 'w', 't', 'd', 'y', 'i', 'b', 'e', 'c', 'u', 'x']
mapper = {}
for a, b in zip(normal, modified):
mapper[b] = a
word ="this is user commented word"
# new_word = ' '.join(map(lambda x: ''.join(mapper[i] for i in x), word.split()))
new_word = []
for i in word.split():
tmp = []
for j in i:
tmp.append(mapper[j])
new_word.append(''.join(tmp))
new_sentence = ' '.join(new_word)
print(new_sentence)
output
rgua ua yawe xinnwbrws qies
normal = 'abcdefghijklmnopqrstuvwxyz'
modified = 'lvxswdfguhjknbiopearycqztm'
word = input()
convert = str.maketrans(normal, modified)
print(word.translate(convert))
I'm attempting to do work with lists in Python and there is a certain part I've been stuck on:
Objective: Iterate through a master list (alphabet) of x amount of elements, and compare whether the index of said element is a factor of 7. If so, append this element to a new list (final). It seems very simple, and here is the code I've written so far:
def test():
alphabet = ['a', 'a', 'b' 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'li', 'a']
final = []
for letter in alphabet:
if (alphabet.index(letter) % 7 == 0):
final.append(letter)
print final
The output I am getting: ['a', 'a', 'g', 'n', 'u', 'a']
The output I am expecting should return a list of every element in the original list that has an index divisible by 7. I cannot figure out how to account for the duplicates.
Any assistance with this would be much appreciated - thank you very much in advance!
Do:
>>> a = ['a', 'a', 'b' 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'li', 'a']
>>> [j for i,j in enumerate(a) if i%7==0]
['a', 'g', 'n', 'u', 'a']
Note that, on index 2, you have 'b' 'b' which results in 'bb'.
I think this is what you're after
for index, letter in enumerate(alphabet):
if (index % 7 == 0):
final.append(letter)
print final
Two things.
First, instead of using a list to accumulate the results, use a set. Duplicates are automatically eliminated.
And, why look at every letter instead of just at every seventh letter?
final = set()
for i in range(len(alphabet)/7):
final.add(alphabet[i*7])
print final
try this:
>>> alphabet = ['a', 'a', 'b' 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'li', 'a']
>>> [letter for i,letter in enumerate(alphabet) if i%7==0]
['a', 'g', 'n', 'u', 'a']
I need help rearranging this list alphabetically
list= ['z', 'a', 'b', 'y', 'c', 'x', 'd', 'w', 'e', 'v', 'f', 'g', 'u', 'h', 'i', 'j', 't' ,'k', 'l', 's', 'm', 'n', 'r', 'o', 'p', 'q', ' ']
into "hello world" by indexing into the array. How exactly do I do that? I'm a beginner and I'm doing this in python 2.7.
As has been mentioned, your list can be sorted alphabetically by using the sort() function as follows:
mylist = ['z', 'a', 'b', 'y', 'c', 'x', 'd', 'w', 'e', 'v', 'f', 'g', 'u', 'h', 'i', 'j', 't' ,'k', 'l', 's', 'm', 'n', 'r', 'o', 'p', 'q', ' ']
mylist.sort()
print mylist
Which results in your list looking like:
[' ', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
But you go on to say 'index into' for 'hello world'. If you mean you want to create a simple cypher then this could be easily be achieved as follows:
import string
s_from = 'abcdefghijklmnopqrstuvwxyz '
s_to = 'zabycxdwevfguhijtklsmnropq '
cypher_table = string.maketrans(s_from, s_to)
print "hello world".translate(cypher_table)
This would convert your text as follows:
wcggi rikgy
Please could you edit your question to give an example of what you are trying to achieve.
Since there is a empty element in the list. You can try to avoid them by using sorted method of list
>>> sorted(list_alphabet, key=list_alphabet.remove(' '))
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
sorted does create new copy of actual list and sort them. So you can refer the output of the sorted to new variable. Like
>>> sorted_list_alphabet = sorted(list_alphabet, key=list_alphabet.remove(' '))
>>> sorted_list_alphabet
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
PS: do not use list as a variable name because it brings conflict between actual list and your list variable
I have a list:
word_list = ['dog', 'downvote', 'gestapo']
I would like this:
['d', 'o', 'g', 'w', 'n', 'v', 't', 'e', 's', 'a', 'p']
This is my code;
[list(word_list[j]) for j in range(len(word_list))]
This code returns this:
[['d', 'o', 'g'], ['d', 'o', 'w', 'n', 'v', 'o', 't', 'e'], ['g', 'e', 's', 't', 'a', 'p', 'o']]
Instead I tried this:
[(word_list[j])[k] for j in range(len(word_list)) for k in len(word_list[j])]
This returns an error: 'int' object is not iterable
I would like to rectify and update my final attempt so that I get the desired output.
If you want to preserve the original order of characters (as in the words from word_list):
def f(seq):
seen = set()
for x in (x for word in word_list for x in word):
if x not in seen:
seen.add(x)
yield x
list(f(word_list)) # ['d', 'o', 'g', 'w', 'n', 'v', 't', 'e', 's', 'a', 'p']
If you don't, just construct the set using set comprehension:
{x for word in word_list for x in word} # {'e', 'd', 'n', 'a', 't', 'w', 'o', 'g', 'v', 's', 'p'}
Although I think you are all technically correct.
The right way to do it in python would probably be:
from itertools import chain
set(chain(*word_list))
k=[]
for j in [list(i) for i in word_list]:
k += j
print list(set(k))
>>>>['d', 'o', 'g', 'w', 'n', 'v', 't', 'e', 's', 'a', 'p']
You could use reduce with operator.add, and a set.
>>> import operator
>>> words = ['dog', 'downvote', 'gestapo']
>>> set(reduce(operator.add, words))
set(['a', 'e', 'd', 'g', 'o', 'n', 'p', 's', 't', 'w', 'v'])
If you want it to be a list:
>>> list(set(reduce(operator.add, words)))
['a', 'e', 'd', 'g', 'o', 'n', 'p', 's', 't', 'w', 'v']
Note: it's not alphabetical order.
If the order is important, a simple way is to use(abuse?) an OrderedDict
>>> word_list = ['dog', 'downvote', 'gestapo']
>>> from collections import OrderedDict
>>> from itertools import chain
>>> OrderedDict.fromkeys(chain.from_iterable(word_list)).keys()
['d', 'o', 'g', 'w', 'n', 'v', 't', 'e', 's', 'a', 'p']
mir = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 't', 'u', 'v', 'x', 'y']
keq = ['.', 'c', 'z', 's', 'e', 'd', 'f', 'g', 'i', 'h', 'j', 'k', 'b', 'v', 'o', 'p', 'q', 'r', 't', 'u', 'x', ''\'', 'y']
I want to know if any of the characters in keq are pressed in raw_input('write text: ')
to transform them in characters that are shown in mir
Can someone help me do this... If you can write all the code it will help me so much
1. You could built a translation table for characters using the maketrans and translate methods, e.g.
>>> import string
>>> tb = string.maketrans('abc', '123')
>>> 'cyan banana'.translate(tb)
'3y1n 21n1n1'
2. You could concatenate all strings in an array using the ''.join method, e.g.
>>> arr = ['a', 'b', 'c']
>>> ''.join(arr)
'abc'
These should be enough to solve your problem.
mir = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 't', 'u', 'v', 'x', 'y']
keq = ['.', 'c', 'z', 's', 'e', 'd', 'f', 'g', 'i', 'h', 'j', 'k', 'b', 'v', 'o', 'p', 'q', 'r', 't', 'u', 'x', '\\', 'y']
trans = dict(zip(mir,keq))
myStr = raw_input()
print ''.join([trans.has_key(ch) and trans[ch] or ch for ch in myStr])
Create a dictionary for the transformation ...
{
'a' : '.',
'b' : 'c',
...
then use map to walk through your input, replacing keys with values.