I have a list of lists that I would like to convert to a list of strings where the strings are the names of the variables. I would like to loop through the list and extract the length of the lists into one list and the name of the list into another. To illustrate here's an attempt to do it using str(variable), which obviously doesn't work because it converts the value of the variable, not the name. I'm trying to convert the name
# Here are the initial lists
common_nouns = ['noun', 'fact', 'banana']
verbs = ['run', 'jump']
adjectives = ['red', 'blue']
# Here is my list of lists:
parts_of_speech = [common_nouns, verbs, adjectives]
labels=[]
data=[]
for pos in parts_of_speech:
if pos != []:
labels.append(str(pos)) # This part doesn't work
data.append(len(pos))
result:
labels = ["['noun', 'fact', 'banana']", "['run', 'jump']", "['red', 'blue']"]
desired result:
labels = ['common_nouns', 'verbs', 'adjectives']
EDIT: Added initial lists
This is the opposite of the frequent question on how to have "variable variables". But the answer is exactly the same: don't do that, use a dict.
Store this data as a single dict with those values as the keys, then you can use the .keys() method to give you the result you want.
I ended up doing a dictionary as Daniel Roseman suggested. Here is the implementation:
parts_of_speech = [common_nouns, verbs, adjectives]
all_labels = ['common_nouns', 'verbs', 'adjectives']
pos_dict = dict(zip(all_labels, parts_of_speech))
labels=[]
data=[]
for pos, lst in pos_dict.items():
if lst:
data.append(len(lst))
labels.append(pos)
Related
I have a list of lists of strings containing the taxonomies of different bacterial species. Each list has a consistent format:
['d__domain;p__phylum;c__class;o__order;f__family;g__genus;s__species','...','...']
I'm trying to pull out the genera of each string in each list to find the unique genera. To do this, my idea was to make nested for loops that would split each string by ';' and use list comprehension to search for 'g__', then lstrip off the g__ and append the associated genus name to a new, complimentary list. I attempted this in the code below:
finalList = []
for i in range(32586):
outputList = []
j = 0
for j in taxonomyData.loc[i,'GTDB Taxonomy'][j]:
## Access Taxonomy column of Pandas dataframe and split by ;
taxa = taxonomyData.loc[i,'GTDB Taxonomy'].split(';')
## Use list comprehension to pull out genus by g__
genus = [x for x in taxa if 'g__' in x]
if genus == [] :
genus = 'None'
## lstrip off g__
else:
genus = genus[0].lstrip('g__')
## Append genus to new list of genera
outputList.append(genus)
## Append new list of genera to larger list
finalList.append(outputList)
print(finalList)
break
print(genus)
I tested this for loop and it successfully pulled the genus out of the first string of the first list, but when I let the for loop run, it skipped to the next list, leaving all the other items in the first list. Any advice on how I can get this loop to iterate through all the strings in the first list before moving on to subsequent lists?
Solved
Final Code:
finalList = []
for i in range(32586):
## Access Taxonomy column of Pandas dataframe and split by ;
if pd.isna(taxonomyData.loc[i,'GTDB Taxonomy']) == True :
genus_unique = ['None']
finalList.append(genus_unique)
else:
taxa = taxonomyData.loc[i,'GTDB Taxonomy'].split(';')
## Use list comprehension to pull out genus by g__
genus_unique = {x[3:] for x in taxa if x.startswith('g__')}
genus_unique = list(genus_unique)
## Append new list of genera to larger list
finalList.append(genus_unique)
print(finalList)
Here's how you can get unique genus entries from a list with a single set comprehension:
taxa = ['d__abc', 'g__def', 'p__ghi', 'g__jkl', 'd__abc', 'g__def']
genus_unique = {x[3:] for x in taxa if x.startswith('g__')}
print(genus_unique)
Result:
{'def', 'jkl'}
You can also convert it into a list afterwards with list(genus_unique) if you need that.
I have code in Python that loops through an directory of images that returns an array 105 images. Now I need it to go through the array and find the matching images by name Example: Mainlist = [Image_Sun_01, Image_Sun_02, Image_Moon_01] and I want it create a seperate list for each matching image like so:
List_01 = [Image_Sun_01, Image_Sun_02]
List_02 = [Image_Moon_01]
What is the best way to do this?
Edit:
To clarify I want it to match the words with each other so "Sun" goes with "Sun" into a list and "Moon" with "Moon" into a new list
From the sample data shown in the question it appears that the "key" is part of a filename within two underscore characters. If that is the case then one idea would be to build a dictionary which is keyed on those tokens. Something like this:
Mainlist = ['Image_Sun_01.cr2', 'Image_Sun_02.jpg', 'Image_Moon_01.raw']
result = {}
for image in Mainlist:
key = image.split('_')[1]
result.setdefault(key, []).append(image)
print(result)
Output:
{'Sun': ['Image_Sun_01.cr2', 'Image_Sun_02.jpg'], 'Moon': ['Image_Moon_01.raw']}
Note:
Subsequently, access to 'Sun' or 'Moon' images is trivial
I have added data to your Mainlist to check the result of my proposed code and get 3 different size lists in output.
import re
Mainlist = ['Image_Sun_01', 'Image_Sun_02', 'Image_Moon_01', 'Image_Moon_02', 'Image_Moon_03', 'Image_Earth_01']
prev_pattern = ''
nb = 0
for i in range(len(Mainlist)):
new_pattern = re.search('[a-zA-Z\_]+', Mainlist[i]).group(0)
if new_pattern != prev_pattern:
nb+=1
prev_pattern = new_pattern
if f"List_{nb:02d}" in globals():
globals()[f"List_{nb:02d}"] += [Mainlist[i]]
else:
globals()[f"List_{nb:02d}"] = [Mainlist[i]]
print(List_01)
print(List_02)
print(List_03)
Output:
['Image_Sun_01', 'Image_Sun_02']
['Image_Moon_01', 'Image_Moon_02', 'Image_Moon_03']
['Image_Earth_01']
I have a large list of names which is in this format
list1 = ["apple", "orange", "banana", "pine-apple"]
And I want it in this format
list1 = ["'apple'", "'orange'", "'banana'", "'pine-apple'"]
Basically, I want to add punctuation marks to every single word in the list
but since the list is too large, I can't do it manually.
So is there any python function or way to do this task. Thank You.
The names in python are already strings enclosed in the quotes like you have shown here. I am supposing you want to wrap the string with specific quote to look this '"apple"' or "'apple'". To do so, you should use the following snippet
q = "'" # this will be wrapped around the string
list1 = ['apple','orange','banana','pine-apple']
list1 = [q+x+q for x in list1]
For reference, the syntax I have used in last line is known as list comprehension
According to latest comment posted by #xdhmoore
If you are using vim/nano (linux/macos) or notepad(windows), then i would rather suggest you to use IDLE python (shipped with python setup)
Str function is the built in function to convert a value into string.
You can run this code;
For i in range(len(list1)):
new = str(list1[i])
list1.remove(list[i])
list1.append(new)
Using for loop to process each line, two ways to go
text = "list1 = [apple,orange,banana,pine-apple]"
start = text.find('[')+1
stop = text.find(']')
lst = text[start:stop].split(',') # ['apple', 'orange', 'banana', 'pine-apple']
new_lst = [f'"{item}"' for item in lst] # ['"apple"', '"orange"', '"banana"', '"pine-apple"']
new_text1 = text[:start]+','.join(new_lst)+text[stop:] # 'list1 = ["apple","orange","banana","pine-apple"]'
text = "list1 = [apple,orange,banana,pine-apple]"
new_text2 = text.replace('[', '["').replace(']', '"]').replace(',', '","')
Plese help me with below question
sample_list = ['Ironman.mdc.googlesuite.net', 'Hulk.nba.abc.googlekey.net',
'Thor.web.gg.hh.googlestream.net', 'Antman.googled.net',
'Loki.media.googlesuite.net','Captain.googlekey.net']
I would want everything preceeding 'googlesuite.net', 'googlekey.net','googlestream.net' and 'googled.net' in list1 and corresponding prefixes in another list as:
result_list1=['Ironman.mdc', 'Hulk.nba.abc', 'Thor.web.gg.hh', 'Antman',
'Loki.media', 'Captain']
result_list2=['googlesuite.net', 'googlekey.net', 'googlestream.net', 'googled.net',
'googlesuite.net', 'googlekey.net']
You can always split each string in the list with '.' and get a new list. In this case, if you are only interested in the first split, you should use the second argument in the split method (which tells the occurrence):
first_list =[x.split('.')[0] for x in sample_list]
For the second list:
second_list =[x.split('.',1)[1] for x in sample_list]
A better way is to iterate only once through the sample_list and get both the lists. As shown below:
first_list, second_list = zip(* [x.split('.',1) for x in sample_list])
Using a list comprehension along with split:
sample_list = ['Ironman.googlesuite.net', 'Hulk.googlekey.net',
'Thor.googlestream.net', 'Antman.googled.net', 'Loki.googlesuite.net',
'Captain.googlekey.net']
result_list1 = [i.split('.')[0] for i in sample_list]
print(result_list1)
This prints:
['Ironman', 'Hulk', 'Thor', 'Antman', 'Loki', 'Captain']
This strategy is to retain, for each input domain, just the component up to, but not including, the first dot separator. For the second list, we can use re.sub here:
result_list2 = [re.sub(r'^[^.]+\.', '', i) for i in sample_list]
print(result_list2)
This prints:
['googlesuite.net', 'googlekey.net', 'googlestream.net', 'googled.net',
'googlesuite.net', 'googlekey.net']
thank you for the answers, it does help but what if I have list like this:
sample_list = ['Ironman.mdc.googlesuite.net', 'Hulk.nba.abc.googlekey.net',
'Thor.web.gg.hh.googlestream.net', 'Antman.googled.net', 'Loki.media.googlesuite.net','Captain.googlekey.net']
I would want everything preceeding 'googlesuite.net', 'googlekey.net','googlestream.net' and 'googled.net' in list1 and corresponding prefixes in another list as:
result_list1=['Ironman.mdc', 'Hulk.nba.abc', 'Thor.web.gg.hh', 'Antman', 'Loki.media', 'Captain']
result_list2=['googlesuite.net', 'googlekey.net', 'googlestream.net', 'googled.net',
'googlesuite.net', 'googlekey.net']
So I am given a list and I am supposed to sort it down into two lists, one with the names of the companies and one with the prices in a nested list.
['Acer 481242.74\n', 'Beko 966071.86\n', 'Cemex 187242.16\n', 'Datsun 748502.91\n', 'Equifax 146517.59\n', 'Gerdau 898579.89\n', 'Haribo 265333.85\n']
I used the following code to separate the names properly:
print('\n'.join(data))
namelist = [i.split(' ', 1)[0] for i in data]
print(namelist)
But now it wants me to seperate all the prices from the list and put them in a single list nested together and I don't know how to do that.
To build two separate lists, just use a regular loop:
names = []
prices = []
for entry in data:
name, price = entry.split()
names.append(name)
prices.append(price)
If you needed the entries together in one list, each entry a list containing the name and the price separately, just split in a list comprehension like you did, but don't pick one or the other value from the result:
names_and_prices = [entry.split() for entry in data]
I used str.split() without arguments to split on arbitrary whitespace. This assumes you always have exactly two entries in your strings. You can still limit the split, but then use None as the first argument, and strip the line beforehand to get rid of the \n separately:
names_and_prices = [entry.strip().split(None, 1) for entry in data]
Demo for the 'nested' approach:
>>> data = ['Acer 481242.74\n', 'Beko 966071.86\n', 'Cemex 187242.16\n', 'Datsun 748502.91\n', 'Equifax 146517.59\n', 'Gerdau 898579.89\n', 'Haribo 265333.85\n']
>>> [entry.split() for entry in data]
[['Acer', '481242.74'], ['Beko', '966071.86'], ['Cemex', '187242.16'], ['Datsun', '748502.91'], ['Equifax', '146517.59'], ['Gerdau', '898579.89'], ['Haribo', '265333.85']]
split() is the right approach, as it will give you everything you need if you don't limit it to just one split (the , 1) in your code). If you provide no arguments to it at all, it'll split on any size of whitespace.
>>> data = ['Acer 481242.74\n', 'Beko 966071.86\n', 'Cemex 187242.16\n', 'Datsun 748502.91\n', 'Equifax 146517.59\n', 'Gerdau 898579.89\n', 'Haribo 265333.85\n']
>>> nested_list = [i.split() for i in data]
>>> nested_list
[['Acer', '481242.74'], ['Beko', '966071.86'], ['Cemex', '187242.16'], ['Datsun', '748502.91'], ['Equifax', '146517.59'], ['Gerdau', '898579.89'], ['Haribo', '265333.85']]
>>> print(*nested_list, sep='\n')
['Acer', '481242.74']
['Beko', '966071.86']
['Cemex', '187242.16']
['Datsun', '748502.91']
['Equifax', '146517.59']
['Gerdau', '898579.89']
['Haribo', '265333.85']