Python - removing items in multidimensional array - python

I have a multidimensional array such as this:
[["asdf","bmnl", "123","456,"0","999","1234","3456"],["qwer","tyui","789","657,"122","9","673","1"]]
However, in the multidimensional array, only the last 6items of each array are needed and the first two are not needed. How can I remove the first two pieces of data from each of the arrays within the multidimensional array so it would look like:
[["123","456,"0","999","1234","3456"],["789","657,"122","9","673","1"]]
So far, I have done this:
list1 = []
list2 = []
for row in rows:
list1.append(row[0].split(',')) #to put the split list into the top i i.e. [["asdf","bmnl", "123","456,"0","999","1234","3456"]["qwer","tyui","789","657,"122","9","673","1"]]
for i in list1:
for index in len(list1):
if index >=2:
list2.append(index) #this does not work and causes errors
How could I go about fixing this so the output would be:
[["123","456,"0","999","1234","3456"],["789","657,"122","9","673","1"]]
Thanks

Just use a list comprehension and grab every element from index 2 and beyond in each sublist:
>>> array = [["asdf","bmnl", "123","456","0","999","1234","3456"],["qwer","tyui","789","657","122","9","673","1"]]
>>> [sublist[2:] for sublist in array]
[['123', '456', '0', '999', '1234', '3456'], ['789', '657', '122', '9', '673', '1']]

lst = [["asdf","bmnl", "123","456","0","999","1234","3456"],["qwer","tyui","789","657","122","9","673","1"]]
for i in lst:
del i[0:2] #deleting 0 and 1 index from each list
print lst

This is a typical use case for a list comprehension:
list2 = [item[2:] for item in list1]

You can use a list comprehension like below:
[item[2:] for item in my_list]
item[2:] called list slicing, and it means that for each sub-list of my_list, we take items from the index 2 till the last item.
Output:
>>> my_list = [["asdf", "bmnl", "123", "456", "0", "999", "1234", "3456"], ["qwer", "tyui", "789", "657", "122", "9", "673", "1"]]
>>>
>>> [item[2:] for item in my_list]
[['123', '456', '0', '999', '1234', '3456'], ['789', '657', '122', '9', '673', '1']]

start = [["asdf","bmnl", "123","456","0","999","1234","3456"],
["qwer","tyui","789","657","122","9","673","1"]]
list_1, list_2 = [i[2:] for i in start] # I use list comprehension to create a multidimensional
# list that contains slices of each object in the base
# list and unwrap it to list_1 and list_2
Same as
n_list = [] #create an empty new list
for i in start: #loop through the origional
n_list.append(i[2:]) #append slices of each item to the new list
list_1, list_2 = n_list #unwrap the list

Related

Removing strings from a list dependent on their length?

I am trying to remove any strings from this list of strings if their length is greater than 5. I don't know why it is only removing what seems to be random strings. Please help. The item for sublist part of the code just changes the list of lists, into a normal list of strings.
list2 = [['name'],['number'],['continue'],['stop'],['signify'],['tester'],['racer'],['stopping']]
li = [item for sublist in list2 for item in sublist]
var=0
for words in li:
if len(li[var])>5:
li.pop()
var+=1
print(li)
The output is: ['name', 'number', 'continue', 'stop', 'signify']
Just include the check when flattening the list:
list2 = [['name'],['number'],['continue'],['stop'],['signify'],['tester'],['racer'],['stopping']]
li = [item for sublist in list2 for item in sublist if len(item) <= 5]
['name', 'stop', 'racer']
You can use a list comprehension to build a new list with only items that are 5 or less in length.
>>> l = ['123456', '123', '12345', '1', '1234567', '12', '1234567']
>>> l = [x for x in l if len(x) <= 5]
>>> l
['123', '12345', '1', '12']
list(filter(lambda x: len(x[0]) <= 5, list2))

How to make a list of strings with a for loop

Assume I have a list=[1,2,3,4] then I want to make a list of strings which length of each string associated with corresponding value in a list.
It means the final output should be like this:
strs=['1', '11', '111', '1111']
I tried the code below but I am not sure how to continue.
lis=[1,2,3,4]
strs=[]
for i in range (len(lis)):
st=lis[i]
strs.append(st)
The multiplication for strings is repetition, so:
lst=[1,2,3,4]
result = []
for e in lst:
result.append('1'*e)
print(result)
Output
['1', '11', '111', '1111']
You first need to loop over the list of lengths lis
Then in each iteration looping n times (the given length of that iteration) and appending 1 each time to the newStr.
And after the iteration adding the newStr to the list strs
lis = [1,2,3,4]
strs = []
for n in lis:
newStr = ""
for x in range(n):
newStr += str('1')
strs.append(newStr)

Del list and next list element in list if string exist

I have an example:
list = [['2 a', 'nnn', 'xxxx','last'], ['next, next'], ['3', '4', 'next']]
for i in range(len(list)):
if list[i][-1] == "last":
del(list[i+1])
del(list[i])
I'd like to delete this list where the last item is "last" and the next item on the list.
In this example there is a problem every time - I tried different configurations, replacing with numpy array - nothing helps.
Trackback:
IndexError: list index out of range
I want the final result of this list to be ['3', '4', 'next']
Give me some tips or help how I can solve it.
Try this:
l = [['2 a', 'nnn', 'xxxx','last'], ['next, next'], ['3', '4', 'next']]
delete_next = False
to_ret = []
for x in l:
if x[-1] == 'last':
delete_next = True
elif delete_next:
delete_next = False
else:
to_ret.append(x)
Using a variable to store if this needs to be deleted
Loop over the list, if the last element of that iteration == 'last' then skip, else, append to a new list.
Also, it is not recommended to edit lists while iterating over them as strange things can happen, as mentioned in the comments above, like the indexes changing.
l = [['2 a', 'nnn', 'xxxx','last'], ['next, next'], ['3', '4', 'next']]
newlist = []
for i in l:
if i[-1] == 'last':
continue
else:
newlist.append(i)

Flattening a list of lists, replacing empty sublists with a certain value

I have list of lists which contains a mix of empty and non-empty sublists.
The total length of the list is 240.
_remain = [['24'],
['24'],
['17'],
[],
['17'],
[],
['17'],...]
And I've tried to flatten the list of lists into one list with:
[name for sublist in _remain for name in sublist]
But when I do this I get a list with length 220. The empty sublists are gone.
My desire is to get a flattened list, replacing empty lists with np.nan so that I can insert it into a pandas DataFrame.
The resultant list I would like to get:
['24',
'24',
'np.nan',
'17',
'np.nan',
'17',...]
What should i try?
You can do something like the following:
>>> [name for sublist in _remain for name in (sublist or [np.nan])]
['24', '17', nan, '17', nan, '17']
Since pandas is tagged(though this can be done with vanilla python as suggested by the above answer), one way is:
pd.DataFrame(l).fillna(np.nan).squeeze().tolist()
['24', '17', nan, '17', nan, '17']
Here is one option:
L = [
['24'],
['17'],
[],
['17'],
[],
['17']
]
L = np.array([l[0] if l else np.nan for l in L])
Output:
L >> ['24' '17' 'nan' '17' 'nan' '17']
You can use a small helper function:
lst = [[1], [], [2], [], [3]]
def func(x):
try:
return x[0]
except IndexError:
return None
[func(i) for i in lst]
# [1, None, 2, None, 3]
you can use this :
flat_list = [item for sublist in l for item in sublist]
or
flat_list = []
for sublist in l:
if len(sublist) == 0:
flat_list.append(np.nan)
else:
for item in sublist:
flat_list.append(item)

List comprehension on lists within lists

If I create two lists that contain lists like this:
bad_list.append(['blue_widget', 'cracked', '776'])
bad_list.append(['red_widget', 'not_smooth', '545'])
bad_list.append(['yellow_widget', 'spots', '35'])
bad_list.append(['green_widget', 'smells_bad', '10'])
bad_list.append(['purple_widget', 'not_really_purple', '10'])
good_list.append(['blue_widget', 'ok', '776'])
good_list.append(['red_widget', 'ok', '545'])
good_list.append(['green_widget', 'ok', '10'])
I would love to be able to use list comprehension to compare the two lists and remove
all items in the bad list that are present in the good list using the first element
(x_widget) as item to compare. Using the example above I should be left with:
['yellow_widget', 'spots', '35']
['purple_widget', 'not_really_purple', '10']
I have tried using list comprehension and it works but the new list does not retain each line:
final_list = [x for x in bad_list[0] if x not in good_list[0]]
When I print out the contents using for item in final_list I get something like:
yellow_widget
smells_bad
10
Any clues would be much appreciated.
One liner
[x for x in bad_list if any(x[0] == y[0] for y in good_list)]
*thanks #Bakuriu
Not really optimized by any means, but this should work: http://codecube.io/AD7RHA
bad_list=[]
good_list=[]
bad_list.append(['blue_widget', 'cracked', '776'])
bad_list.append(['red_widget', 'not_smooth', '545'])
bad_list.append(['yellow_widget', 'spots', '35'])
bad_list.append(['green_widget', 'smells_bad', '10'])
bad_list.append(['purple_widget', 'not_really_purple', '10'])
good_list.append(['blue_widget', 'ok', '776'])
good_list.append(['red_widget', 'ok', '545'])
good_list.append(['green_widget', 'ok', '10'])
# ['yellow_widget', 'spots', '35']
# ['purple_widget', 'not_really_purple', '10']
labels = zip(*good_list)[0]
new_bad_list=[]
for item in bad_list:
if item[0] not in labels:
new_bad_list.append(item)
print new_bad_list
or this one-liner:
new_bad_list=[item for item in bad_list if item[0] not in zip(*good_list)[0]]
Try This:
print [ele for ele in bad_list if ele[0] not in [i[0] for i in good_list]]
Output:
[['yellow_widget', 'spots', '35'], ['purple_widget', 'not_really_purple', '10']]
There is a more efficient solution. Make a set from your list
bad_set = set(bad_list)
good_set = set(good_list)
Now to remove all items in the bad list that are present in the good list, you can simple substract the sets:
bad_set - good_set
Convert set back to list if you like.
the simplest way is:
final_list = [x for x in bad_list if x[0] not in [x[0] for x in good_list]]
but notice that to test an element's existing in a list is not that efficient.
So, you can first build a set:
good_list_names = set([x[0] for x in good_list])
and then:
final_list = [x for x in bad_list if x[0] not in good_list_names]

Categories