Removing characters from a 2d list - python

lst = [['1', 'ertt', '9', '8', '', '', '', '\n'], ['1', 'ertt',
'9', '', '32', '6', '0.6', '0.7\n'], ['1', 'ertt',
'9', '8', '', '', '', '\n'], ['1', 'ertt', '9', '', '',
'6.6', '', '\n'], ['1', 'ertt', '9', '', '32', '', '', '1.8\n']]
How can i remove the \n from the last column of each row. i want to keep the numbers but just remove the pesky \n?

You can simply recreate the list with list comprehension, like this
print [item[:-1] + [item[-1].rstrip("\n")] for item in lst]
For every item in lst, we create a new list like this
item[:-1] + [item[-1].rstrip("\n")]
Here item[:-1] means that, everything except the last element and item[-1] means the last element. We simply right strip the \n from that.

Related

Get a element from list, from pair of 3

Hello I have a list in which elemnets are in pair of 3 list given below,
labels = ['', '', '5000','', '2', '','', '', '1000','mm-dd-yy', '', '','', '', '15','dd/mm/yy', '', '', '', '3', '','', '', '200','', '2', '','mm-dd-yy', '', '','', '', '','', '', '']
in above list elements are coming in pair of 3 i.e. ('', '', '5000') one pair, ('', '2', '') second pair, ('mm-dd-yy', '', '') third pair and so on.
now i want to check ever 3 pairs in list and get the element which is not blank.
('', '', '5000') gives '5000'
('', '2', '') gives '2'
('mm-dd-yy', '', '') gives 'mm-dd-yy'
and if all three are blank it should return blank i.e.
('', '', '') gives '' like last 2 pair in list
so from the above list my output should be:
required_list = ['5000','2','1000','mm-dd-yy','15','dd/mm/yy','3','200','2','mm-dd-yy','','']
as it is fixed you have to create 3 pairs each time you can do with for loop by specifying step in range(start,end,step)
labels = ['', '', '5000','', '2', '','', '', '1000','mm-dd-yy', '', '','', '', '15','dd/mm/yy', '', '', '', '3', '','', '', '200','', '2', '','mm-dd-yy', '', '','', '', '','', '', '']
res1=[]
for i in range(0,len(labels),3):
res1.append(labels[i]+labels[i+1]+labels[i+2])
print(res1)
#List Comprehension
res2=[labels[i]+labels[i+1]+labels[i+2] for i in range(0,len(labels),3)]
print(res2)
Output:
['5000', '2', '1000', 'mm-dd-yy', '15', 'dd/mm/yy', '3', '200', '2', 'mm-dd-yy', '', '']
I think this should give you the required result. Not ideal performance but gets the job done and should be pretty easy to follow
labels = ['', '', '5000','', '2', '','', '', '1000','mm-dd-yy', '', '','', '', '15','dd/mm/yy', '', '', '', '3', '','', '', '200','', '2', '','mm-dd-yy', '', '','', '', '','', '', '']
def chunks(ls):
chunks = []
start = 0
end = len(ls)
step = 3
for i in range(start, end, step):
chunks.append(ls[i:i+step])
return chunks
output = []
for chunk in chunks(labels):
nonEmptyItems = [s for s in chunk if len(s) > 0]
if len(nonEmptyItems) > 0:
output.append(nonEmptyItems[0])
else:
output.append('')
print(output)
All the previous answers laboriously create a new list of triplets, then iterate on that list of triplets.
There is no need to create this intermediate list.
def gen_nonempty_in_triplets(labels):
return [max(labels[i:i+3], key=len) for i in range(0, len(labels), 3)]
labels = ['', '', '5000','', '2', '','', '', '1000','mm-dd-yy', '', '','', '', '15','dd/mm/yy', '', '', '', '3', '','', '', '200','', '2', '','mm-dd-yy', '', '','', '', '','', '', '']
print(gen_nonempty_in_triplets(labels))
# ['5000', '2', '1000', 'mm-dd-yy', '15', 'dd/mm/yy', '3', '200', '2', 'mm-dd-yy', '', '']
Interestingly, there are many different ways to implement "get the element which is not blank".
I chose to use max(..., key=len) to select the longest string.
Almost every answer you received uses a different method!
Here are a few different methods that were suggested. They are equivalent when at most one element of the triplet is nonempty, but they behave differently if the triplet contains two or more nonempty elements.
# selects the longest string
max(labels[i:i+3], key=len)
# selects the first nonempty string
next((i for i in labels[i:i+3] if i), '')
# concatenates all three strings
labels[i]+labels[i+1]+labels[i+2]
Iterate over a 3 sliced list and then get the first non-null element with next.
labels = ['', '', '5000','', '2', '','', '', '1000','mm-dd-yy', '', '','', '', '15','dd/mm/yy', '', '', '', '3', '','', '', '200','', '2', '','mm-dd-yy', '', '','', '', '','', '', '']
length = len(labels)
list_by_3 = [labels[i:i+3] for i in range(0, length, 3)]
required_list = []
for triplet in list_by_3:
required_list.append(
next(i for i in triplet if i, "")
)
>>> required_list
['5000', '2', '1000', 'mm-dd-yy', '15', 'dd/mm/yy', '3', '200', '2', 'mm-dd-yy', '', '']

Check if multiple elements are in a list

i have 2 lists :
A = ['1', '2', '3', '4', '5']
B = ['0', '1', '9', '3', '0']
and i want to check if elements in list B are in A and return a list, if so it should return the same number, if not it should return empty string '', here is the result i'm looking for :
C = ['', '1', '', '3', '']
i Tried using for loop and append result to an empty list, but i got this :
C = ['', '1', '', '', '', '', '', '', '3', ''...]
it doubled the number of elements in the list cuz it looks for the first number in the entire list then move to second one, which makes sense since i'm using for loop, what should i use instead to get back a 5 elements list please ?
thanks in advance.
To get your required output, you can just loop through B and check if the item exists in A. If it does, then append it in c otherwise append an empty string to c.
A = ['1', '2', '3', '4', '5']
B = ['0', '1', '9', '3', '0']
c = []
for i in B:
if i in A:
c.append(i)
else:
c.append('')
print(c)
The output of the above code
['', '1', '', '3', '']

Python: List index Out of Range Error when list indexing

I have a list of lists property_lists which is structured as follows:
property_lists = [['Semi-Detached', '|', '', '2', '|', '', '2'], ['Detached', '|', '', '5', '|', '', '3'], ['Detached', '|', '', '5', '|', '', '5']]
and so on.
I am attempting list indexing in order to put all the elements into separate lists of their own.
For example:
typeOfProperty = [item[0] for item in property_lists]
returns
['Semi-Detached', 'Detached', 'Detached']
However, the below results in an index list out of range error:
bedrooms = [item[3] for item in property_lists]
But I don't understand why as each 'sub' list has 7 elements?
To be clear, i am trying to output:
['2', '5', '5']
First can you please give example, of what you want in the output?
If you want the output like the below:
propertyList1 = ['Semi-Detached', '|', '', '2', '|', '', '2']
propertyList2 = ['Detached', '|', '', '5', '|', '', '3']
propertyList3 = ['Detached', '|', '', '5', '|', '', '5']
then Here's the solution:
property_lists = [['Semi-Detached', '|', '', '2', '|', '', '2'], ['Detached', '|', '', '5', '|', '', '3'], ['Detached', '|', '', '5', '|', '', '5']]
propertyList1 = [items for items in property_lists[0]]
propertyList2 = [items for items in property_lists[1]]
propertyList3 = [items for items in property_lists[2]]
So what I am doing here is, I am taking the property_lists every index and creates its own list using list comprehension
You are taking the only first index of the item in your question, that's why it is the result list contains only "Semi-Detached" and "Detached" values.
AND
your second question is about the list index out of range, That is happening because you are running the for loop on the property_lists, and item variable will store the value that is coming from the property_lists, so you cannot access the element with an index of the item. you can access the index-wise items after creating the separate lists like I created in the code.
The problem is with your full data that referred in your comment https://pastecode.io/s/pg56vfyk.
One of the elements of property_lists has the the length of 1. That is property_lists[8] = ['Land for sale'].
You should have a check like this.
bedrooms = [item[3] if len(item) > 2 else 0 for item in p]
Similar way for other indices > 0.

Return only index zero in a list comprehension

To create a list with the values that I retrieve from a spreadsheet, I do it like this:
result = {'range': 'Winning_Margin!G2:O1000', 'majorDimension': 'ROWS',
'values': [['10148734', 'CD Olimpia', 'CD Vida', '1',
'71', '85', '14.00', '1.40', '2-0'],
['', '', '', '', '', '', '', '', ''],
['10024627', 'Sporting Kansas City', 'FC Dallas',
'2', '35', '1', '-34.00', '2.88']]}
for a in result["values"]:
a.extend([''] * (max(map(len, result["values"])) - len(a)))
spreadsheets_match_id = [item for sublist in result["values"] for item in (sublist or ['']) if sublist[-1] != '']
This way returns the entire index:
['10148734', 'CD Olimpia', 'CD Vida', '1', '71', '85', '14.00', '1.40', '2-0']
But my need is just the first index of each list of list, in this case the result is:
['10148734']
Tried to make each item become item[0] but when trying to do that, it either says that item doesn't exist or returns the first letter of each of the list's indexes within the list like ['1', 'C', 'C', '1', '7', '8', '1', '1', '2'].
Edit:
You can check if the last element of the sublist is '' or not and keep the first elements of the ones that aren't:
out = [sublist[0] for sublist in result["values"] if sublist[-1] !='']
Old answer:
IIUC, you want to get the first element of a sublist that is not '', right? Then you can create a single element list in a list comprehension and iterate over it in a nested loop to check if it is '' or not:
out = [item for sublist in result["values"] for item in [sublist[0]] if item !='']
Output:
['10148734', '10024627']

Adding elements based on the repeated elements in list of lists in Python

sorted_lst = [
['04-0200', 'str1', '1', 'n1'],
['04-0200', 'str2', '6', 'n1,n2,n3,n4,n5,n6'],
['11-0200', 'str3', '1', 'u1'],
['19-0000', '', '2', ''],
['19-0201', 'str4', '2', ''],
['19-0201', 'str10', '3', 'p1,p2,p3'],
['22-0001', 'str5', '5', 'a1,a2,a3,a4'],
['22-0001', 'str6', '184', 'b1,b2,b3,b4,b5'],
['22-0001', 'str7', '2', 'c1,c2'],
['9-02011', 'str8', '3', '', 'x,y,z']
]
I have a sorted list that will have a list of lists with repeated index0.
Based on the index0 from each list, i will have to add the index3 and then join the strings in index4.
Index#3 will be always numbers - this should be added if there is any repeated index#0
Index#4 could be anything - this should be merged and there shouldn't be any repeated strings/numbers after merge.
As an example, the result of the above list should be something like:
final_lst = [
['04-0200', 'str1', '7', 'n1,n2,n3,n4,n5,n6'],
['11-0200', 'str3', '1', 'u1'],
['19-0000', '', '2', ''],
['19-0201', 'str4', '5', 'p1,p2,p3'],
['22-0001', 'str5', '191', 'a1,a2,a3,a4,b1,b2,b3,b4,b5,c1,c2'],
['9-02011', 'str8', '3', '', 'x,y,z']
]
======================================================================
Edited:
Sorry, new to the community; adding more details.
Please close the thread; was able to merge them.
I'm able to achieve what I wanted..
post can be closed.
MERGED ITEMS:
[('04-0200', 'str1', 7, 'n1,n2,n3,n4,n5,n6'),
('11-0200', 'str3', '1', 'u1'),
('19-0000', '', '2', ''),
('19-0201', 'str4', 5, 'p1,p2,p3'),
('22-0001', 'str5', 191, 'a1,a2,a3,a4b1,b2,b3,b4,b5c1,c2'),
('9-02011', 'str8', '3', '')]

Categories