How to find the python list item that start with - python

I have the list that contains some items like:
"GFS01_06-13-2017 05-10-18-38.csv"
"Metadata_GFS01_06-13-2017 05-10-18-38.csv"
How to find the list item that start with "GFS01_"
In SQL I use query: select item from list where item like 'GFS01_%'

You have several options, but most obvious are:
Using list comprehension with a condition:
result = [i for i in some_list if i.startswith('GFS01_')]
Using filter (which returns iterator)
result = filter(lambda x: x.startswith('GFS01_'), some_list)

You should try something like this :
[item for item in my_list if item.startswith('GFS01_')]
where "my_list" is your list of items.

If you really want the string output like this "GFS01_06-13-2017 05-10-18-38.csv","GFS01_xxx-xx-xx.csv", you could try this:
', '.join([item for item in myList if item.startswith('GFS01_')])
Or with quotes
', '.join(['"%s"' % item for item in myList if item.startswith('GFS01_')])
Filtering of list will gives you list again and that needs to be handled as per you requirement then.

Related

How to convert a list made up of one tuple into a two item list?

I have a function that returns a one item list, like so:
list = [('array_1','array_2')]
I want to change this so that the list is instead a two item one, without the parentheses or single quotes:
list = [array_1,array_2]
What would be the best way to go about doing this?
Try this
lists = [('array_1','array_2')]
print([y for x in lists for y in x])
output
['array_1', 'array_2']
Use chain.from_iterable
from itertools import chain
list(chain.from_iterable([('array_1','array_2')]))
['array_1', 'array_2']
You can try this:
lst_tuple = [('array_1', 'array_2')]
lst = []
for i in lst_tuple[0]:
lst.append(i)
By iteratating over the list that contains the tuple and appending each item to a new list, you can get this result:
['array_1', 'array_2']
You could just typecast like this.
list = list([('array_1','array_2')][0])

Get inner-most elements from triple nested list Python

I have list
players = [[['QB1',7000,20],['RB1',4500,12],['RB2',3800,11]],
[['QB1',7000,20],['RB2',3800,11],['RB1',4500,12]]]
How do I get the first element of each inner-most lists ('QB1', 'RB1' and 'RB2' from the first "secondary," if you will, list) to check if they are the same, however disordered labels as those in another secondary list (they are in this case as both secondary lists contain 'QB1', 'RB1' and 'RB2')?
EDIT:
My desired out is [['QB1','RB1','RB2'],['QB1','RB2','RB1']]. I want to have some way of identifying that these are, for my purpose, the same list.
You can do this:
output = [[i[0] for i in a] for a in players]
The output will be like this:
[['QB1', 'RB1', 'RB2'], ['QB1', 'RB2', 'RB1']]
you can use recursive search for that and get first element of each list or whole list
players = [[['QB1',7000,20],['RB1',4500,12],['RB2',3800,11]],[['QB1',7000,20],['RB2',3800,11],['RB1',4500,12]]]
def retrive_first(lst):
for item in lst:
if type(item) == list:
retrive_first(item)
else:
print "returning ---> ", lst[0]
return lst[0]
print retrive_first(players)
You could also try this:
from operator import itemgetter
[list(map(itemgetter(0), player)) for player in players]
do:
print(list(zip(players))[0])
Or:
print([i[0] for i in players])

How to make a list to split into each element and make a sublist

If I have this list,
list01 = ['GAGGT','TCCGT','ABECF']
I want to make each element in the list to split, but still remain in the same box of list.
Like this:
listalt = [['G','A','G','G','T'],['T','C','C','G','T'],['A','B','E','C','F']]
listalt = [list(i) for i in list01]
This is a list comprehension, and uses the fact that list can take an iterable (like a string) and turn it into a list
You can use this :
final_list=[]
for item in list01:
final_list.append(list(item))
print(final_list)
which is same as :
print([list(item) for item in list01])

Save list items to a new list if the item contains specific substring

I want to check if strings in a list of strings contain a certain substring. If they do I want to save that list item to a new list:
list = ["Maurice is smart","Maurice is dumb","pie","carrots"]
I have tried using the following code:
new_list = [s for s in list if 'Maurice' in list]
but this just replicates the list if one of its items is 'Maurice'.
So I was wondering if, maybe, there was a way to solve this by using the following syntax:
if "Maurice" in list:
# Code that saves all list items containing the substring "Maurice" to a new list
Result should then be:
new_list = ["Maurice is smart", "Maurice is dumb"]
If been looking for a way to do this but I can not find anything.
You could do this:
list = ["Maurice is smart","Maurice is dumb","pie","carrots"]
new_list = [x for x in list if "Maurice" in x]
print(new_list)
Output:
['Maurice is smart', 'Maurice is dumb']
You could use Python's builtin filter:
data = ["Maurice is smart", "Maurice is dumb", "pie", "carrots"]
res = filter(lambda s: 'Maurice' in s, data)
print(res)
Output:
['Maurice is smart', 'Maurice is dumb']
The first argument is a predicate function (a simple lambda here) which must evaluate to True for the element of the iterable to be considered as a match.
filter is useful whenever an iterable must be filtered based on a predicate.
Also, a little extra, imagine now this data to be filtered:
data = ["Maurice is smart","Maurice is dumb","pie","carrots", "maurice in bikini"]
res = filter(lambda s: 'maurice' in s.lower(), list)
print(res)
Ouput:
['Maurice is smart', 'Maurice is dumb', 'maurice in bikini']
You can use a list comprehension.
Also, make sure not to use the built in list as variable name.
my_list = ["Maurice is smart", "Maurice is dumb", "pie", "carrots"]
[e for e in my_list if 'Maurice' in e]

How to do a one-line dict delete operation

Is there a way to do the following in one line?
[del item for item in new_json if item['Country'] in countries_to_remove]
The above gives me a SyntaxError.
del is a statement and you cannot use that as an expression in list comprehenstion. That is why you are getting a SyntaxError.
You can use list comprehension to create a new list, without the elements you don't want, like this
[item for item in new_json if item['Country'] not in countries_to_remove]
This is actually equivalent to,
result = []
for item in new_json:
if item['Country'] not in countries_to_remove:
result.append(item)
This kind of operation is called filtering a list and you can use the builtin filter function, like this
list(filter(lambda x: x['Country'] not in countries_to_remove, new_json))
As suggested by mgilson in the comments section, if you just want to mutate the original list, then you can use slicing assignment, like this
new_json[:] = [x for x in new_json if x['Country'] not in countries_to_remove]
del is a statement in python, and you cannot have statements inside list comprehension (You can only have expressions there). Why not just create new_json as a new list or dictionary that does not include the items you want to delete. Example =
new_json = [item for item in new_json if item['Country'] not in countries_to_remove]

Categories

Resources