I am trying to put my results into a list.
Here is my code:
from ise import ERS
l = ise.get_endpoints(groupID=my_group_id)['response']
Here is my output:
[('AA:BB:CD', 'cvr5667'), ('AA:BB:CC', '8888')]
Here is my desired output which is a list of just the first elements of inside the parentheses:
['AA:BB:CD','AA:BB:CC']
I am new at python and working with lists/dicts so any suggestions would. All I am trying to do it put the first elements inside the parentheses in one list like i showed.
Using list comprehension (as suggested in comments too):
lst_1 = [('AA:BB:CD', 'cvr5667'), ('AA:BB:CC', '8888')]
lst_result = [i[0] for i in lst_1]
With something like this ?
result = [('AA:BB:CD', 'cvr5667'), ('AA:BB:CC', '8888')]
first_elements_to_list = [tmp[0] for tmp in result]
print(first_elements_to_list)
print:
['AA:BB:CD', 'AA:BB:CC']
Related
i am still learning python and i am struggling to find a way to get this to work the way i want.
#First Method
staticPaths = [
"[N4-NFS-NETAPP-8040-C01]/[N4-NHT-LEAF-VPC-FAS-C02-N2][vlan-480]",
"[N4-OLDCLOUD-IPSTORAGE/epg-N4-NFS-NETAPP-8040-C01]/[vlan-481]]",
"['N4-NFS-NETAPP-8040-C01]/[N4-NHT-LEAF-VPC-FAS-C02-N2][vlan-484]",
"['N4-NFS-NETAPP-8040-C01]/[N4-NHT-LEAF-VPC-FAS-C02-N2][vlan-485]",
"['N4-NFS-NETAPP-8040-C01]/[N4-NHT-LEAF-VPC-FAS-C01-N2][vlan-480]"
]
for path in staticPaths:
filter_object = filter(lambda a: 'vlan-480' in a, staticPaths)
print(list(filter_object))
So what i am trying to do here is filter out anything that matches ‘vlan-480’ and return the entire line, so for example, if i run that code, i receive the correct output. which would be -
[N4-NFS-NETAPP-8040-C01]/[N4-NHT-LEAF-VPC-FAS-C01-N2][vlan-480]
['N4-NFS-NETAPP-8040-C01]/[N4-NHT-LEAF-VPC-FAS-C02-N2][vlan-480]
However where is states ‘vlan-480’ in the lambda function i actually want to pass it a LIST but because i am using the “in” statement, it only allows me to pass a single string.
Again i want to check multiples, so for example, give me the output for ‘vlan-480’ AND ‘vlan-484’ and it should return the lines for me from the staticPaths
I cannot think of way of getting this done, might just be me been stupid but for some reason i cannot solve it.
Also tried an if statement but i have the same problem, with the single string option.
#Second Method
path_matches = []
for path_match in staticPaths:
if 'vlan-480' in path_match:
path_matches.append(path_match)
print(path_matches)
Can anyone think of a way of doing this, its probably really easy but for some reason i cannot think of it. I did try and use List Comprehension but struggled to get the output i needed.
much appericated
Try this:
substrings = ['vlan-480', 'vlan-484']
filter_object = filter(lambda a: any(x in a for x in substrings), staticPaths)
print(list(filter_object))
The list substrings contains substrings to search for
The output I get for your dataset is:
['[N4-NFS-NETAPP-8040-C01]/[N4-NHT-LEAF-VPC-FAS-C02-N2][vlan-480]', "['N4-NFS-NETAPP-8040-C01]/[N4-NHT-LEAF-VPC-FAS-C02-N2][vlan-484]", "['N4-NFS-NETAPP-8040-C01]/[N4-NHT-LEAF-VPC-FAS-C01-N2][vlan-480]"]
You can also use list comprehension
substrings = ['vlan-480', 'vlan-484']
path_matches = [x for x in staticPaths if substrings[0] in x or substrings[1] in x]
that results in :
path_matches
['[N4-NFS-NETAPP-8040-C01]/[N4-NHT-LEAF-VPC-FAS-C02-N2][vlan-480]',
"['N4-NFS-NETAPP-8040-C01]/[N4-NHT-LEAF-VPC-FAS-C02-N2][vlan-484]",
"['N4-NFS-NETAPP-8040-C01]/[N4-NHT-LEAF-VPC-FAS-C01-N2][vlan-480]"]
Your problem is with indentation i guess check this
path_matches = []
for path_match in staticPaths:
if 'vlan-480' in path_match:
path_matches.append(path_match)
Try to use this:
filter_object = filter(lambda a: 'vlan-480' in a or 'vlan-484' in a, staticPaths)
print(list(filter_object))
To do multi selection you can use operator OR/AND, so it will be such this 'vlan-480' in a or 'vlan-484' in a
I have a function modify as follows:
list_with_chunks = [['hi','hello','how are you?'],['i','am','fine'],['what','about you?','.']]
flatten_list = ['hi','hello',...]
empty_list = []
# building the function to convert our sentences in list_with_chunks into another form:
def modify(sentence):
# do stuff here
# returning the result and appending them in empty_list
return empty_list.append(sentence*2)
I call the function as below:
for i in flatten_list:
modify(i)
But, I want to send each sentence directly from list_with_chunks instead of flattening it and append the result in empty_list. How do I do that?
TIA.
I don't understand the question entirely! But is this what you looking for:
for x in list_with_chunks:
for y in x:
modify(y)
You just need to iterate every element inside list again in order to add them in the empty list.
Use a nested loop through list_with_chunks.
for i in range(len(list_with_chunks)):
for j in range(len(list_with_chunks[i])):
modify(list_with_chunks[i][j], top_n=5)
I have scraped a list of prices from a site that I want to get the average on. And correct me if I am wrong but my assumption is that the data needs to not have dollar signs to be a be added up to get the total sum so that it can be used to get the average price of the list.
My attempts include but are not limited to using a for loop to slice the 0 index off each list item.
for i in clean:
i = i[1:]
i also originally tried just running it without creating a variable but it does literally nothing to the output of printing the clean list
for i in clean:
i = i[1:]
example list of current list i have:
clean = [$123.56, $234.56, $561.12]
What I would like the output of the cleaned up list to be:
[123.56, 234.56, 561.12]
You don't actually have to use enumerate. Here is a very simple solution to your problem.
clean = ['$123.56', '$234.56', '$561.12']
result = []
for i in clean:
result.append(float(i[1:]))
print(result) # [123.56, 234.56, 561.12]
You should use the library 're':
import re
trim = re.compile(r'[^\d.,]+')
my_string = '$12.56' #works also with USD or other currency, with space or not
result = trim.sub('',my_string)
print(result)
>>> 12.56
For a list:
my_list = ['$123.56', '$234.56', '$561.12']
list_without_currency = [float(trim.sub('',e)) for e in my_list]
>>> [123.56, 234.56, 561.12]
EDIT:
see also: this (SO)
I have been trying to figure out how to use lists using one variable.
The code I am using right now:
catalog = ['Hello', 'World']
itemLists = item.getFeed(catalog) #It is not important what item.getFeed does
# However an exception will pop out due to there is a list of catalog.
My issue here is that I want to add one by one that are in the lists to:
itemLists = item.getFeed(catalog)
For example it would end up like this:
itemLists = item.getFeed(Hello)
itemLists = item.getFeed(World)
however this would override the itemLists and what I am trying to do is that I want to each value in the list to be append into the itemLists.
How could I make a simple line that would use all items that are in the catalog to run:
itemLists = item.getFeed(x)
one by one?
I think (if I understand your question correctly) that you want to append to the list?
This should work:
itemLists = list()
for element in catalog:
itemLists.append(item.getFeed(element))
Or it can be done with a list comprehension like this:
itemLists = [item.getFeed(x) for x in catalog]
Learn more about list comprehensions at the official Python Tutorials: https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions
If I understand your question correctly, you're looking for a "for" loop. For every item in your iterable, do something.
catalog = ['Hello', 'World']
for x in catalog:
itemLists = item.getFeed(x)
# do something with itemLists here because it'll be
# overwritten on the next iteration.
i ran into a little logic problem and trying to figure it out.
my case is as follows:
i have a list of items each item represents a Group
i need to create a set of nested groups,
so, for example:
myGroups = ["head", "neck", "arms", "legs"]
i need to get them to be represented like this:
(if you can imaging a folder structure)
head
|_> neck
|_> arms
|_>legs
and so on until i hit the last element.
what i thought would work (but don't know really how to advance here) is:
def createVNTgroups(self, groupsData):
for i in range(len(groupsData)):
print groupsData[i]
for q in range(1, len(groupsData)):
print groupsData[q]
but in this case, i am running over same elements in 'i' that i already took with 'q'.
could someone give me a hint?
thanks in advance!
If I understood well, you want a nested structure. For this case, you can use a recursive function:
myGroups = ["head", "neck", "arms", "legs"]
def createVNTgroups(alist):
temp = alist[:] # needed because lists are mutable
first = [temp.pop(0)] # extract first element from list
if temp: # if the list still contains more items,
second = [createVNTgroups(temp)] # do it recursively
return first + second # returning the second object attached to the first.
else: # Otherwise,
return first # return the last element
print createVNTgroups(myGroups)
this produces a nested list:
['head', ['neck', ['arms', ['legs']]]]
Is that what you were looking for?
>>> m
['head', 'neck', 'arms', 'legs']
>>> reduce(lambda x,y:[x,y][::-1] if x!=y else [x], m[::-1],m[-1])
['head', ['neck', ['arms', ['legs']]]]