Convert certain values in list to int - python

I have a list of lists and I want to convert the second value in each list to an int since it is currently a string
[['hello','how','are','you','1'],['hello','how','are','you','2']]
I am trying to convert index 4 to an int in each list within this larger list but when I do
for hi in above:
int(hi[4])
It is just returning the int when I print the list and not the entire list.

Just traverse it and convert it using the int() function for every 4th element in every list inside :
for li in my_list:
li[4] = int(li[4])

This list comprehension is one way to do it:
a_list = [[int(a) if item.index(a) == 4 else a for a in item] for item in a_list]
Demo:
>>> a_list = [['hello','how','are','you','1'],['hello','how','are','you','2']]
>>> a_list = [[int(a) if item.index(a) == 4 else a for a in item] for item in a_list]
>>> a_list
[['hello', 'how', 'are', 'you', 1], ['hello', 'how', 'are', 'you', 2]]
>>>

Related

How to remove elements from a list on the basis of the rows in Python without messing up the indexes? [duplicate]

This question already has answers here:
How to remove multiple indexes from a list at the same time? [duplicate]
(8 answers)
Closed 7 months ago.
Le't assume I have a list with elements, from which I want to remove a few rows on the basis of a list of row indexes. This is an example:
l = ['ciao','hopefully','we','are','going','to','sort','this','out']
idx = [1,3,5]
If I do the following, it doesn't work as the loop doesn't consider that the lenght of the list after the nth remove object is n-i:
for x in idx:
del l[x]
# what happens? The code only removes correctly the first element of idx, then it doesn't take into account that the list has shrunk and therefore the nth index no longer corresponds to the updated row list.
Note, I cannot turn the list into an array and then use np.delete as my list is coming from webscraping and it fails when I try to do so.
Can anyone suggest me a way to remove list's elements on the basis of its rows in one shot without messing up with the shifting of the index?
Thanks!
Another approach without enumerate
new_l = [item for item in l if l.index(item) not in idx]
You can use list comprehension and enumerate.
# enumerate give:
# : 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8
l = ['ciao','hopefully','we','are','going','to','sort','this','out']
idx = set([1,3,5])
out = [el for i, el in enumerate(l) if not i in idx]
print(out)
['ciao', 'we', 'going', 'sort', 'this', 'out']
If you must delete them in place:
>>> lst = ['ciao', 'hopefully', 'we', 'are', 'going', 'to', 'sort', 'this', 'out']
>>> idx = [1, 3, 5]
>>> for i in sorted(idx, reverse=True):
... del lst[i]
...
>>> lst
['ciao', 'we', 'going', 'sort', 'this', 'out']
If do not need, you can use list comprehension to create a new list:
>>> lst = ['ciao', 'hopefully', 'we', 'are', 'going', 'to', 'sort', 'this', 'out']
>>> idx = [1, 3, 5]
>>> idx_set = set(idx)
>>> [v for i, v in enumerate(lst) if i not in idx_set]
['ciao', 'we', 'going', 'sort', 'this', 'out']
Of course, you can also achieve the effect of deleting in place through slice assignment:
>>> lst[:] = [v for i, v in enumerate(lst) if i not in idx_set]

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 do I remove all elements from a list which is a subsequence of another bigger element in the same list in python?

I have a list of lists which looks like this:
[['rest'],['look'],['rest','look'],['resting','look],['apple','mango'],['apple','man'],['apple','banana','mango'],['rest','resting','look','looked','it','spit']]
All the elements which are a substring/subsequence of another element must be removed. For example elements ['rest'] and ['look'] are already present in list elements ['rest','look'] and ['rest','resting','look','looked','it','spit'], so they must be removed from the final list. Also, element ['rest','look'] is subsequence of ['rest','resting','look','looked','it','spit'], so it should be removed. Similarly, ['resting','look']is a substring of['rest','resting','look','looked','it','spit']`, so it also must be removed.The element ['apple','mango'] as it is a subsequence of ['apple','banana','mango'] should be removed but ['apple','man'] should not be removed as it is not a common subsequence.The output has to be a list and not a set.
I tried this:
x = [['rest'],['look'],['rest','look'],['resting','look'],['apple','mango'],['apple','man'],['apple','banana','mango'],['rest', 'resting', 'look', 'looked', 'it', 'spit']]
res=[]
found=0
for i in x:
for item in i:
for j in x:
for item1 in j:
if item == item1:
found=1
if found==0:
res.append(item)
print res
The output I am getting is an empty list. The desired output is:
[['apple','man'],['apple','banana','mango'],['rest','resting','look','looked','it','spit']]
You could just use a set comprehension instead, by flattenning the list into a set.
x = [['rest'],['look'],['rest','look'],['resting','look'],['rest', 'resting', 'look', 'looked', 'it', 'spit']]
In [2]: results = {s_ for s in x for s_ in s}
In [2]: results
Out[3]: {'it', 'look', 'looked', 'rest', 'resting', 'spit'}
I think here's what you have tried to implement. I don't think you need variable found, you can just check for each item in sub-lists (e.g. ['rest', 'look'] is already appended to res.
x = [['rest'], ['look'], ['rest', 'look'], ['resting', 'look'], ['rest', 'resting', 'look', 'looked', 'it', 'spit']]
res = []
for L in x:
for item in L:
if item not in res:
res.append(item)
print(res)
# ['rest', 'look', 'resting', 'looked', 'it', 'spit']

Extracting the last items from nested lists in python [duplicate]

This question already has answers here:
Python - get the last element of each list in a list of lists
(5 answers)
Closed 2 years ago.
I have some nested lists. I want to extract the last occurring element within each sublist (eg 'bye' for the first sublist). I then want to add all these last occurring elements ('bye', 'bye', 'hello' and 'ciao') to a new list so that I can count them easily, and find out which is the most frequently occurring one ('bye).
The problem is that my code leaves me with an empty list. I've looked at Extracting first and last element from sublists in nested list and How to extract the last item from a list in a list of lists? (Python) and they're not exactly what I'm looking for.
Thanks for any help!
my_list = [['hello', 'bye', 'bye'], ['hello', 'bye', 'bye'], ['hello', 'hello', 'hello'], ['hello', 'bye', 'ciao']]
# Make a new list of all of the last elements in the sublists
new_list = []
for sublist in my_list:
for element in sublist:
if sublist.index(element) == -1:
new_list.append(element)
# MY OUTPUT
print(new_list)
[]
# EXPECTED OUTPUT
['bye', 'bye', 'hello', 'ciao']
# I would then use new_list to find out what the most common last element is:
most_common = max(set(new_list), key = new_list.count)
# Expected final output
print(most_common)
# 'bye'
You can call the last element of a list by calling the index -1, e.g. sublist[-1]. The following code should give you your expected result:
my_list = [['hello', 'bye', 'bye'], ['hello', 'bye', 'bye'], ['hello', 'hello', 'hello'], ['hello', 'bye', 'ciao']]
new_list = list()
for sublist in my_list:
new_list.append(sublist[-1])
print(new_list)
or as Risoko pointed out, you can do it in one line, so the code should look like
my_list = [['hello', 'bye', 'bye'], ['hello', 'bye', 'bye'], ['hello', 'hello', 'hello'], ['hello', 'bye', 'ciao']]
new_list = [sublist[-1] for sublist in my_list]
print(new_list)
You are looking for this:
for sublist in my_list:
new_list.append(sublist[-1])
The index -1 does not "really exist", it is a way to tell python to start counting from the end of the list. That is why you will not get a match when looking for -1 like you do it.
Additionally, you are walking over all the lists, which is not necessary, as you have "random access" by fetching the last item as you can see in my code.
There is even a more pythonic way to do this using list comprehensions:
new_list = [sublist[-1] for sublist in my_list]
Then you do not need any of those for loops.
output = [sublist[-1] for sublist in my_list]

Index of \n in Python list

Okay, here I have another problem, I need to find position of \n alone in my list.
list = ['abc', '\n', 'def', 'ghi', '\n', 'jkl']
So, I need to get the position of all '\n' entries from this list.
I used
a=list.index('\n')
but got only one value as '1'. How to get both positions?
e.g. I will get a list with the position of '\n'
position = ['1', '4']
'1' represents first position of \n in list and the '4' represents second at the fourth place in list.
You'll need to iterate over the elements. This can be easily done by using a list comprehension and enumerate for the indexes:
indexes = [i for i, val in enumerate(list) if val == '\n']
Demo:
>>> lst = ['abc', '\n', 'def', 'ghi', '\n', 'jkl']
>>> [i for i, val in enumerate(lst) if val == '\n']
[1, 4]
[i for i, x in enumerate(l) if x == "\n"]
# => [1, 4]
And don't call a list list since this is a builtin function.
To find indices of an item in a list with multiple occurrences, following code should work
print [i for i in range(len(list)) if list[i] == '\n']
Here I have used list as it was taken in your question but do not use keywords as variables .

Categories