I need to remove the sub lists from a list in Python.
For example: The main list A contains
A=[ 'a,b,c', 'd,e,f' , 'g,h,i' , 'g,l,m' , 's,l,k' , 'd,k,l', 'a,g,d' ]
I need to remove the sub lists from A which begin with the items in the following list:
B = ['g','d']
so that Final List A = [ 'a,b,c', 's,l,k' , 'a,g,d' ]
Thanks in advance
Using list comprehension:
print([x for x in A if x[0] not in ['g', 'd']])
You can do it using list comprehension and split(",").
print([e for e in A if e.split(",")[0] not in B])
Output
['a,b,c', 's,l,k', 'a,g,d']
Your output above for your approach is wrong.
2nd Element 'd,e,f' should also be removed as start element 'd' is in second list.
Related
I got this type of strings inside a list :
[['AAAA-BBBBBBB-10.00.00.0', 'AAAAAAAA', '00:00:00:00:00:00', '000', 'BBBBBBB', 'AAAAA:00'], [
when i export this list to csv the strings inside the commas (,) are in different cells but 'AAAA-BBBBBBB-10.00.00.0' is all in one cell. I wanted it to look like this:
[['AAAA-BBBBBBB','10.00.00.0', 'AAAAAAAA', '00:00:00:00:00:00', '000', 'BBBBBBB', 'AAAAA:00'], [
so when i export it to csv file the strings 'AAAA-BBBBBBB','10.00.00.0' will be in 2 different cells.
I tried:
dp1 = [d.replace("-1"," 1").split() for d in data]
but it doesnt work it seems its ignoring it!
You can enumerate the elements of the outer list, change the composition of the 1st item of the inner list and assing it back to your outer list:
data = [['AAAA-BBBBBBB-10.00.00.0', 'AAAAAAAA', '00:00:00:00:00:00',
'000', 'BBBBBBB', 'AAAAA:00'],
[]]
for idx, d in enumerate(data):
if d:
orig = d[0].replace("-1"," 1").split() # create a 2 part list
d = orig + d[1:] # use list slicing
data[idx] = d # assign it back to your big list
print(data)
Output:
[['AAAA-BBBBBBB', '10.00.00.0', 'AAAAAAAA', '00:00:00:00:00:00',
'000', 'BBBBBBB', 'AAAAA:00'],
[]]
I have a huge list like
list = [["a", "bfgf", "c%2"], ["b", "hhj", "kkkk", "f%2"]]
I want to remove %2 end of every last item in lists of list.
I tried
list = [[item[-1].replace('%2', '') for item in lst] for lst in list]
and it did not work
Your way does not work because you are not changing the value in place. Your statement simply takes a copy of every sub-list, stores it in lst, then changes the data in that copy, and not the actual list itself.
Try it in this way, and tell me if it works.
myList= [["a","bfgf","c%2"],["b","hhj","kkkk","f%2"]]
for i in range(len(myList)):
Last=myList[i].pop()
Last=Last.replace("%2","")
myList[i].append(Last)
print (myList)
For your nested list you can also use a nested list comprehension to remove the unwanted characters.
ini_list = [["a", "bfgf", "c%2"],["b", "hhj", "kkkk", "f%2"]]
ini_list = [[val.replace('%2', '') for val in sublist] for sublist in ini_list]
print(ini_list)
# output
[['a', 'bfgf', 'c'], ['b', 'hhj', 'kkkk', 'f']]
I have two separate lists, "a" and "b". I'm trying to merge them together so it shows the items in the two lists with a hyphen in between.
I tried to using the zip and join functions but I wasn't able to solve the issue.
a = ['USA', 'Australia', 'Netherlands', 'Sweden', 'France', 'Spain']
b = ['Washington', 'Canberra', 'Amsterdam', 'Stockholm', 'Paris', 'Barcelona']
##I tried using zip
c = CT = zip(a,b)
##I got:
[('USA', 'Washington'),
('Australia', 'Canberra'),
('Netherlands', 'Amsterdam'),
('Sweden', 'Stockholm'),
('France', 'Paris'),
('Spain', 'Barcelona')]
Tried to use join as well, but that didn't work
Ideally, the output should be:
USA-Washington
Australia-Canberra
Netherlands-Amesterdam
Sweden-Stockholm
France-Paris
Spain-Barcelona
Any help would be appreciated.
You can apply a list comprehension over the zipped items:
[x + '-' + y for x,y in zip(a,b)]
You can apply join to each result of zip.
c = ['-'.join(x) for x in zip(a, b)]
Or you can use a for loop.
c = []
for i in range(min(len(a), len(b))):
c.append('{}-{}'.format(a[i], b[i]))
I want to remove a values from python list without changing the index.
For example, if list is [20,30,40] , if I want remove the 20 from the list.
that list index become 30-0 and 40-1.but I need 30-1 and 40-2.
Is there any solution to my problem??
You need to use dictionaries:
assoc = { 0:20, 1:30, 2:40}
print( assoc[2] )
del assoc[1]
print( assoc[2] )
print(assoc)
Running this gives:
40
40
{0: 20, 2: 40}
I have some coordinate data that is lng,lat however I need it lat,lng
I have them in a list of tuples and need to switch them around:
myList = [(-87.93897686650001, 41.8493707892),
(-87.93893322819997, 41.8471652588),
(-87.9292710931, 41.8474548975),
(-87.91960917239999, 41.8477438951),
(-87.91927828050001, 41.8404034535)]
I have tried
newList = []
for i in range(len(myList)):
for c, x in reversed(list(enumerate(myList[i]))):
newList.append(x)
I get the below output, which is close
[41.8493707892,
-87.93897686650001,
41.8471652588,
-87.93893322819997,
41.8474548975,
-87.9292710931,
41.8477438951,
-87.91960917239999,
41.8404034535,
-87.91927828050001]
But I am looking for
[(41.8493707892, -87.93897686650001),
(41.8471652588, -87.93893322819997),
(41.8474548975, -87.9292710931),
(41.8477438951, -87.91960917239999),
(41.8404034535, -87.91927828050001)]
ideally i would like to do this all in the nested for-loop; but I do not care if I have to do something with the newList to group the pairs.
As always any help is appreciated.
Unless you're really attached to the nested for-loop, it's probably easiest to use this sort of list comprehension:
newlist=[i[::-1] for i in myList]
>>> newlist
[(41.8493707892, -87.93897686650001), (41.8471652588, -87.93893322819997), (41.8474548975, -87.9292710931), (41.8477438951, -87.91960917239999), (41.8404034535, -87.91927828050001)]
You can do this very easy with list comprehension.
[(b,a) for a,b in myList]