Comparing lists, is the subset list within the first list - python-3.3

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}

Related

I want to know about alphabets using map?

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

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.

How do you rearrange a list of lists where the first element holds multiple values and creates a new list of lists while repeating the first element?

Given a list of lists:
list_format = [['a', 'c', 'f', 'b'], ['j', 'l', 'o', 'c'], ['q', 's', 'v', 'e']]
'c', 'f', 'b' must be mapped to 'a'
'l', 'o', 'c' must be mapped to 'j'
's', 'v', 'e' must be mapped to 'q'
The output should look like this:
[['a','c'],['a','f'],['a','b'],['j','l'],['j','o'],['j','c'],['q','s'],['q','v'],['q','e']]
I've tried so far:
list_dict = {element[0]:element[1:] for element in list_format}
newer_lst = []
for key, value in list_dict.items():
newer_lst.append((key, value))
newer_lst
Gives me the output of tuples:
[('a', ['c', 'f', 'b']), ('j', ['l', 'o', 'c']), ('q', ['s', 'v', 'e'])]
I'm newer at this and trying to rearrange, any advice would be awesome, been stuck for days with trial and error(searched google countless times and constantly googling. I feel I'm getting close but can't seem to put it together.
Here is a one-liner, using slicing:
[[i[0],j] for i in list_format for j in i[1:]]
gives:
[['a', 'c'], ['a', 'f'], ['a', 'b'], ['j', 'l'], ['j', 'o'], ['j', 'c'], ['q', 's'], ['q', 'v'], ['q', 'e']]
Also, if you iterate through your value variable, you get your result:
list_dict = {element[0]:element[1:] for element in list_format}
newer_lst = []
for key, value in list_dict.items():
for i in value:
newer_lst.append((key, i))
print(newer_lst)
You don't need to create a loop, just loop on sub array and append new sub array to main output array on the fly, like new_list.append([lst[0], item])
new_list = []
for lst in list_format:
for item in lst[1:]:
new_list.append([lst[0], item])
print(new_list)
#output
#[['a', 'c'], ['a', 'f'], ['a', 'b'], ['j', 'l'], ['j', 'o'], ['j', 'c'], ['q', 's'], ['q', 'v'], ['q', 'e']]

Is there a function to create a list of unique elements in unique index using python?

Is there a way to create a new set of lists of elements, with each element of the original list in a unique index in the following lists?
orginal_list=['r', 'g', 'b', 'y']
output: ['y', 'g', 'r', 'b'],['g', 'y', 'b', 'r'],['r', 'b', 'y', 'g'],['b', 'r', 'g', 'y']
or
output: ['y', 'r', 'b', 'g'],['g', 'y', 'r', 'b'],['r', 'b', 'g', 'y'],['b', 'g', 'y', 'r']
or
...
I have tried to use iterators.permutations, but this does not fit with the unique index requirement.
Easiest would be to rotate the list. Here is a simple generator function producing all such rotations:
def rots(lst):
for i in range(len(lst)):
yield lst[i:] + lst[:i]
>>> list(rots(['r', 'g', 'b', 'y']))
[['r', 'g', 'b', 'y'],
['g', 'b', 'y', 'r'],
['b', 'y', 'r', 'g'],
['y', 'r', 'g', 'b']]
The rotating guarantees that each element actually occurs in each index exactly once.

How to create a list from another list in python

I am very new to python and i was stuck with this. I need to create a list of lists that is
formed from this list c: ['asdf','bbnm','rtyu','qwer'].
I need to create something like this:
b: [['a','s','d','f'],['b','b','n','m'],['r','t','y','u'],['q','w','e','r']]
I tried using a for-loop, but it is not working out. I don't know what mistake I am making.
You can use a list comprehension with list():
>>> c = ['asdf','bbnm','rtyu','qwer']
>>>
>>> b = [list(s) for s in c]
>>> b
[['a', 's', 'd', 'f'], ['b', 'b', 'n', 'm'], ['r', 't', 'y', 'u'], ['q', 'w', 'e', 'r']]
Notice that calling list() with a string argument returns a list containing the characters of that string:
>>> list('abc')
['a', 'b', 'c']
What we're doing above is applying this to every element of the list via the comprehension.
Use map function.
>>> a= ['asdf','bbnm','rtyu','qwer']
>>> map(list ,a )
[['a', 's', 'd', 'f'], ['b', 'b', 'n', 'm'], ['r', 't', 'y', 'u'], ['q', 'w', 'e', 'r']]
>>>
You could do something that's easier to understand:
b = []
for x in c:
list(x).append(b)

Resources