How can I remove the single quotes from the following so it can be recognized as a list of one dictionary object as apposed to a list of one string object? Given that it is a list, I cannot use pd.replace.
['{"PLAYER":"Player Name","SALARY":"0000.00","OPP":"CI","POS":"BR","TEAM":"IT","SCHEDULE_ID":"40623","PLAYERID":"12322","GP":"5","TAR":"64","RZTAR":"6","POW TAR":"32.99%","WEEK 2":"11","WEEK 3":"14","WEEK 4":"9","WEEK 5":"19","ARDS":"545","YPT":"8.52","REC":"40","REC RATE":"62.50%"}']
You can use ast.literal_eval:
import ast
s = ['{"PLAYER":"Player Name","SALARY":"0000.00","OPP":"CI","POS":"BR","TEAM":"IT","SCHEDULE_ID":"40623","PLAYERID":"12322","GP":"5","TAR":"64","RZTAR":"6","POW TAR":"32.99%","WEEK 2":"11","WEEK 3":"14","WEEK 4":"9","WEEK 5":"19","ARDS":"545","YPT":"8.52","REC":"40","REC RATE":"62.50%"}']
final_s = [ast.literal_eval(i) for i in s]
Output:
[{'SALARY': '0000.00', 'REC RATE': '62.50%', 'OPP': 'CI', 'YPT': '8.52', 'TAR': '64', 'GP': '5', 'PLAYERID': '12322', 'WEEK 3': '14', 'POS': 'BR', 'ARDS': '545', 'WEEK 2': '11', 'PLAYER': 'Player Name', 'SCHEDULE_ID': '40623', 'POW TAR': '32.99%', 'WEEK 4': '9', 'TEAM': 'IT', 'RZTAR': '6', 'REC': '40', 'WEEK 5': '19'}]
just use eval() for the purpose
s=['{"PLAYER":"PlayerName","SALARY":"0000.00","OPP":"CI","POS":"BR","TEAM":"IT","SCHEDULE_ID":"40623","PLAYERID":"12322","GP":"5","TAR":"64","RZTAR":"6","POW TAR":"32.99%","WEEK 2":"11","WEEK 3":"14","WEEK 4":"9","WEEK 5":"19","ARDS":"545","YPT":"8.52","REC":"40","REC RATE":"62.50%"}']
s = [eval(item) for item in s]
Related
grades = [
['Student', 'Exam 1', 'Exam 2', 'Exam 3'],
['Thorny', '100', '90', '80'],
['Mac', '88', '99', '111'],
['Farva', '45', '56', '67'],
['Rabbit', '59', '61', '67'],
['Ursula', '73', '79', '83'],
['Foster', '89', '97', '101']
]
I want to create a new list named Students which has all the student names except for the header.
I tried:
students = [item[0] for item in grades]
but this gives me the header "students" as well.
You're close. You simply need to limit the grades in your statement:
students = [item[0] for item in grades[1:]]
This will iterate over grades starting with the second item (index of 1) to the end (nothing after the :).
Just throw away the first line.
students = students[1:]
The above solution uses list comprehansion. This solution utilizes a more comon way you'd see it being writen in other programming languages.
students = []
for data in grades[1:]:
students.append(data[0])
Although it has been answered but if headers are not sorted and occurs at some other position:
# Look for header containing certain string
header = lambda s: "Student" not in s
# Get everything from the list except containing header
students = [i for i in grades if header(i)]
I'm trying to make a list of values to a list of dictionary's based on my set keys. I tried the following but i'm loosing all the other values because of the duplicate key names.
>>> values = ['XS ', '1', 'S ', '10', 'M ', '1', 'L ', '10', 'XL ', '10']
>>> keys = ['size', 'stock'] * (len(values) / 2)
>>> result = dict(zip(keys, values))
>>> print result
{'stock': '10', 'size': 'XL '}
What i'm trying to achieve is a list of the dicts like below. How can I achieve this?
[{'stock': '10', 'size': 'XL '}, {'stock': '10', 'size': 'L'}, ......]
You can use a list comprehension like following:
>>> values = ['XS ', '1', 'S ', '10', 'M ', '1', 'L ', '10', 'XL ', '10']
>>> [{'size':i, 'stock':j} for i, j in zip(values[0::2], values[1::2])]
[{'stock': '1', 'size': 'XS '}, {'stock': '10', 'size': 'S '}, {'stock': '1', 'size': 'M '}, {'stock': '10', 'size': 'L '}, {'stock': '10', 'size': 'XL '}]
Note that in this case you don't have to multiply the keys.
Usually the point of using a dict is to associate unique keys to associated values, you were originally trying to associate size: ... and stock: ... for each item but why not link the size to stock? In that case you would simply do:
result = dict(zip(values[::2], values[1::2]))
or without needing slicing:
value_iter = iter(values)
result = dict(zip(value_iter, value_iter))
This grabs two elements from the list at a time.
This way you still know that a given key in the dict is the size and the associated value is the stock for that size.
My Input list is of the following type (Note that it includes empty strings):
Input = ['NO', 'Part Number', '1', '12334', '2', '45fd33', '']
I need to create a list of dictionaries like (Where the empty strings are ignored)
[
{'NO': '1', 'Part Number': '12334'},
{'NO': '2', 'Part Number': '45fd33'}
]
What I have tried so far:
dict(enumerate(new_name_list, start=1))
and got following output:
{1: 'NO', 2: 'Part Number', 3: '1', 4: '12334', 5: '2', 6: '45fd33', 7: ''}
How can I get expected output and exclude empty string value from the list?
There are two separate questions that you ask here.
To remove '' from your list you can pass it through a list comprehension
>>> Input = ['NO', 'Part Number', '1', '12334', '2', '45fd33','']
>>> Input_Changed = [i for i in Input if i!='']
>>> Input_Changed
['NO', 'Part Number', '1', '12334', '2', '45fd33']
The syntax of your string follows the general rule that the odd elements baring the first two is meant to be NO and the even ones are meant to be Part_Number.
For this we can use following the iter trick:
>>> i = iter(Input_Changed[2:])
>>> [{Input_Changed[0]:x,Input_Changed[1]:y} for x,y in zip(i,i)]
[{'Part Number': '12334', 'NO': '1'}, {'Part Number': '45fd33', 'NO': '2'}]
The main idea is that iter returns an iterator and hence each time you pass an element you consume an element. Thus you get the required output when you zip (which returns adjacent elements as you are passing the same iterator as both the arguments).
Currently working with a text file that is read into python and then must be made into lists with a list (nested I guess?) So far I've tried using linear searching code but it only checks one of the lists in the nested list:
def populationreader():
with open("PopulationofAnnecy", "r") as in_file:
nested = [line.strip().split(',') for line in in_file][1:]
print nested
This yields the following nested list:
[['Alabama', '126', '79', '17'], ['Alaska', '21', '100', '10'], ['Arizona', '190', '59', '16'], ['Arkansas', '172', '49', '28'], ['California', '4964', '76', '22'] etc …. ]
But it should look something more like:
[[California 4964,76,22],[Texas 3979,62,23],[New York 1858,69,20],[Virginia 1655,60,19]etc …. ]
I've tried using something along the lines of this (pseudo):
for index in range(1,len(alist)):
currentvalue = alist[index]
position = index
while position>0 and alist[position-1]>currentvalue:
alist[position]=alist[position-1]
position = position-1
alist[position]=currentvalue
Trying to do it without using the built in python sort() or sorted() functions but I'm just having trouble sorting things within a list
Once you have your list read in from the file, you can use sort or sorted, but you want to make sure you sort by the second element [1] and make sure to reverse also. Otherwise the default is to sort by the first element of the list (the state name) and alphabetically since it is a string.
l = [['Alabama', '126', '79', '17'],
['Alaska', '21', '100', '10'],
['Arizona', '190', '59', '16'],
['Arkansas', '172', '49', '28'],
['California', '4964', '76', '22'],
['Texas', '3979','62','23'],
['New York', '1858','69','20'],
['Virginia', '1655','60','19']]
sorted(l, key = lambda i: int(i[1]), reverse=True)
Output
[['California', '4964', '76', '22'],
['Texas', '3979', '62', '23'],
['New York', '1858', '69', '20'],
['Virginia', '1655', '60', '19'],
['Arizona', '190', '59', '16'],
['Arkansas', '172', '49', '28'],
['Alabama', '126', '79', '17'],
['Alaska', '21', '100', '10']]
Many thanks to the SO community for helping me with the previous problems I had encountered. Love the help here!
I have yet another problem now. I have a flat list of DNA sequences that have associated "Construct Number" and "Part Number. As things stand right now, from my previous code, I have it as a csv file which I open up, read in, and import as a list of dictionary objects. Everything is sorted by "Construct Number" already, but I need to then sort by "Part Number". (It's sort of like in Excel, where they say "First sort by and then sort by _."
Does anybody know how to get this done? Thus far, all I have written is this:
primers_list = open('primers-list.csv', 'rU')
primers_unsorted = csv.DictReader(primers_list)
for row in primers_unsorted:
print(row)
A subset of the output thus far is just the following, for visualization of the data I'm working with:
{' Direction': 'fw primer', ' Construct Number': '1', ' Part Number': '2', 'Primer Sequence': 'AAGCGGCCGCTCGAGTCTAAgctcactcaaaggcggtaatcagataaaaaaaatccttag'}
{' Direction': 're primer', ' Construct Number': '1', ' Part Number': '1', 'Primer Sequence': 'attaccgcctttgagtgagcTTAGACTCGAGCGGCCGCTTTTTGACACCAGACCAACTGG'}
{' Direction': 'fw primer', ' Construct Number': '1', ' Part Number': '1', 'Primer Sequence': 'TTTAATTACTAACTTTATCTATGATAGATCCCGTCGTTTTACAACGTCGTGACTGGGAAA'}
{' Direction': 're primer', ' Construct Number': '1', ' Part Number': '2', 'Primer Sequence': 'AAAACGACGGGATCTATCATAGATAAAGTTAGTAATTAAACTTAAAAGTTGTTTAATGTC'}
{' Direction': 'fw primer', ' Construct Number': '2', ' Part Number': '2', 'Primer Sequence': 'gtaaatccaagttgtaataatactagagTAGCATAACCCCTTGGGGCCTCTAAACGGGTC'}
{' Direction': 're primer', ' Construct Number': '2', ' Part Number': '1', 'Primer Sequence': 'GGGGTTATGCTActctagtattattacaacttggatttaccacctttcttcgccttgatc'}
{' Direction': 'fw primer', ' Construct Number': '2', ' Part Number': '1', 'Primer Sequence': 'TACGACTCACTATAGGGAGAtactagagttaaggaggtaaaaaaaatgggtccggtcgtt'}
{' Direction': 're primer', ' Construct Number': '2', ' Part Number': '2', 'Primer Sequence': 'ttacctccttaactctagtaTCTCCCTATAGTGAGTCGTATTACTCTAGAAGCGGCCGCg'}
{' Direction': 'fw primer', ' Construct Number': '3', ' Part Number': '2', 'Primer Sequence': 'gtaaatccaagttgtaataatactagagTAGCATAACCCCTTGGGGCCTCTAAACGGGTC'}
{' Direction': 're primer', ' Construct Number': '3', ' Part Number': '1', 'Primer Sequence': 'GGGGTTATGCTActctagtattattacaacttggatttaccacctttcttcgccttgatc'}
{' Direction': 'fw primer', ' Construct Number': '3', ' Part Number': '1', 'Primer Sequence': 'TAACTATCACTATAGGGAGAtactagagttaaggaggtaaaaaaaatgggtccggtcgtt'}
{' Direction': 're primer', ' Construct Number': '3', ' Part Number': '2', 'Primer Sequence': 'ttacctccttaactctagtaTCTCCCTATAGTGATAGTTATTACTCTAGAAGCGGCCGCg'}
Another way:
import operator
primers_unsorted.sort(key=operator.itemgetter(' Construct Number', ' Part Number'))
for row in primers_unsorted:
print(row)
If you want to do it block by block you can do something like:
a=0
while a<len(primers_list):
b=a
current_construct=primers_list['Construct Number']
while primers_list[b]['Construct Number']==current_construct:
b=b+1
primers_list[a:b]=sorted( primers_list[a:b] , key = lambda e: (e[' Construct Number'],e[' Part Number']))
a=b
which might be useful if the list is very long.
My final code is this, and it worked out perfectly:
primers_list = open('primers-list.csv', 'rU')
primers_unsorted = csv.DictReader(primers_list)
primers_sorted = sorted(primers_unsorted, key=operator.itemgetter('Construct Number', 'Part Number'))
for row in primers_sorted:
print(row)
The key (pardon the pun) to this was to make use operator.itemgetter(...), which accepts as many arguments as needed. It's passed into the 'key' argument in sorted(...).
Many thanks to Eric for answering my question!