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)
Related
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 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.
I am new to Python. I have a Python 2D array [[a,b,c],[d,e,f],[g,h,j]] and would like to shuffle the 3 inner lists but not the contents. I can't seem to access numpy but would think this is quite simple. shuffle does not seem to work for a 2d array as it returns none. HELP Please!!
I would like something like [[d,e,f],[a,b,c],[g,h,j]] for example...
You can use the function shuffle from random:
import random
arr = [list('abc'), list('def'), list('ghj')]
>>> arr
[['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'j']]
random.shuffle(arr)
Example output:
>>> arr
[['g', 'h', 'j'], ['d', 'e', 'f'], ['a', 'b', 'c']]
Or, you could do the same with numpy.random.shuffle:
import numpy as np
arr = [list('abc'), list('def'), list('ghj')]
>>> arr
[['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'j']]
np.random.shuffle(arr)
>>> arr
[['g', 'h', 'j'], ['a', 'b', 'c'], ['d', 'e', 'f']]
[['s', 'a', 'b', 't'], ['s', 'c', 'd', 't'], ['s', 'c', 'e', 't','g']]
I want to count how many elements are in each list of the list.
so,[4,4,5].How can i do that in python?
Just use len for each element on the given list:
l = [['s', 'a', 'b', 't'], ['s', 'c', 'd', 't'], ['s', 'c', 'e', 't','g']]
[len(x) for x in l] # [4, 4, 5]
you can utilize the len() function. It takes a list as a parameter and returns the elements count.
python len function
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}