I want to know about alphabets using map? - python-3.x

why we use this particular range from 97 to 123? And I want to know more about alphabets using map ?
list(map(chr,range(97,123)))

ASCII codes for the lower case English alphabets range from 97 to 122.
The range function in the line you provided above, creates an iterable object with the elements from 97 to 122. You are mapping these with the chr method. This method returns the associated ASCII character. For example,
>>> chr(97)
'a'
>>> chr(100)
'd'
>>> chr(122)
'z'
Now, your map function doing all these operations for the numbers between 97 to 123.
>>> map(chr,range(97,123))
<map object at 0x000002EEAE8F46C8>
But the map returns the map object, and to convert that to a list , you can use list method.
>>> list(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']
Regards

Related

How to join array into string in Julia

How can I join [5, 'N', 'K', 'r', 9, 'j', 'K', '(', 'E', 't'] into making it a single string like this "5NKr9jK(Et"
Just join it :):
julia> join([5, 'N', 'K', 'r', 9, 'j', 'K', '(', 'E', 't'])
"5NKr9jK(Et"
The individual elements of the vector are converted to string using the print function.

Python 3.8 sort - Lambda function behaving differently for lists, strings

Im trying to sort a list of objects based on frequency of occurrence (increasing order) of characters. Im seeing that the sort behaves differently if list has numbers versus characters. Does anyone know why this is happening?
Below is a list of numbers sorted by frequency of occurrence.
# Sort list of numbers based on increasing order of frequency
nums = [1,1,2,2,2,3]
countMap = collections.Counter(nums)
nums.sort(key = lambda x: countMap[x])
print(nums)
# Returns correct output
[3, 1, 1, 2, 2, 2]
But If I sort a list of characters, the order of 'l' and 'o' is incorrect in the below example:
# Sort list of characters based on increasing order of frequency
alp = ['l', 'o', 'v', 'e', 'l', 'e', 'e', 't', 'c', 'o', 'd', 'e']
countMap = collections.Counter(alp)
alp.sort(key = lambda x: countMap[x])
print(alp)
# Returns Below output - characters 'l' and 'o' are not in the correct sorted order
['v', 't', 'c', 'd', 'l', 'o', 'l', 'o', 'e', 'e', 'e', 'e']
# Expected output
['v', 't', 'c', 'd', 'l', 'l', 'o', 'o', 'e', 'e', 'e', 'e']
Sorting uses stable sort - that means if you have the same sorting criteria for two elements they keep their relative order/positioning (here it being the amount of 2 for both of them).
from collections import Counter
# Sort list of characters based on increasing order of frequency
alp = ['l', 'o', 'v', 'e', 'l', 'e', 'e', 't', 'c', 'o', 'd', 'e']
countMap = Counter(alp)
alp.sort(key = lambda x: (countMap[x], x)) # in a tie, the letter will be used to un-tie
print(alp)
['c', 'd', 't', 'v', 'l', 'l', 'o', 'o', 'e', 'e', 'e', 'e']
This fixes it by using the letter as second criteria.
To get your exact output you can use:
# use original position as tie-breaker in case counts are identical
countMap = Counter(alp)
pos = {k:alp.index(k) for k in countMap}
alp.sort(key = lambda x: (countMap[x], pos[x]))
print(alp)
['v', 't', 'c', 'd', 'l', 'l', 'o', 'o', 'e', 'e', 'e', 'e']
See Is python's sorted() function guaranteed to be stable? or https://wiki.python.org/moin/HowTo/Sorting/ for details on sorting.

Is there a method that splits a string into individual characters? (python) [duplicate]

This question already has answers here:
How do I split a string into a list of characters?
(15 answers)
Closed 2 years ago.
Say I have a string that is a sentence, i.e. text = 'Say I have a string that is a sentence' ; is there a method that can be called on text to split the assigned value for string into individual characters, so a list of each individual index I suppose?
Your string already is a sequence of separate characters that can be indexed like you can with a list.
text = 'Say I have a string that is a sentence'
text[0]
>>> S
text[4]
>>> I
No need to use a fancy function for this.
But if you, for some reason need a variable of type List, you can use list(text).
list(text)
>>> ['S', 'a', 'y', ' ', 'I', ' ', 'h', 'a', 'v', 'e', ' ', 'a', ' ', 's', 't', 'r', 'i', 'n', 'g', ' ', 't', 'h', 'a', 't', ' ', 'i', 's', ' ', 'a', ' ', 's', 'e', 'n', 't', 'e', 'n', 'c', 'e']
list the whole string straight away. drop an example next time
answer = 'Is this your what you are talking about'
list(anwser)
#output
'I', 's', ' ', 't', 'h', 'i', 's', ' ', 'w', 'h', 'a', 't', ' ', 'y', 'o', 'u', ' ', 'a', 'r', 'e', ' ', 't', 'a', 'l', 'k', 'i', 'n', 'g', ' ', 'a', 'b', 'o', 'u', 't']
string='Here is what you are looking for';
print(list(string));

The English alphabet as a vector of characters in Rust

The title says it all. I want to generate the alphabet as a vector of characters. I did consider simply creating a range of 97-122 and converting it to characters, but I was hoping there would be a nicer looking way, such as Python's string.ascii_lower.
The resulting vector or string should have the characters a-z.
Hard-coding this sort of thing makes sense, as it can then be a compiled constant, which is great for efficiency.
static ASCII_LOWER: [char; 26] = [
'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',
];
(Decide for yourself whether to use static or const.)
This is pretty much how Python does it in string.py:
lowercase = 'abcdefghijklmnopqrstuvwxyz'
# ...
ascii_lowercase = lowercase
Collecting the characters of a str doesn't seem like a bad idea...
let alphabet: Vec<char> = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".chars().collect();
Old question but you can create a range of chars, so
('a'..='z').into_iter().collect::<Vec<char>>()

Comparing lists, is the subset list within the first list

How do I write a syntax for something like this?
if our list 1 = ['F', 'W', 'I', 'N', 'T', 'E', 'R', 'S']
our second list looks like = ['WINTER']
how would I write that list 1 contains a sequence of strings WINTER?
You could convert both to sets and use issubset ?
>>> list1
['F', 'W', 'I', 'N', 'T', 'E', 'R', 'S']
>>> list2
['W', 'I', 'N', 'T', 'E', 'R']
>>> set(list2).issubset(set(list1))
True
Or maybe convert them both to sets and then test list2 - list1 ?
Or (taken straight from the docs):
{x for x in list2 if x not in list1}

Resources