Zipping two lists won't make a tuple? - list

weekinfo=[]
for k in sats:
weekinfo.append(get_weekly(k, satdict))
yearend=get_weekly('end_year', satdict)
weektuples=zip(sats, weekinfo)
Sats is a list with 52 items. [ date, date, date, etc]
Weekinfo is a nested list (thus 52 lists in a list) with in each nested list with in each list 100 tuples.
I am trying to make a tuples (weektuples) in the last line via zip(sats, weekinfo). However, the output are not tuples but a lists.
Is there a solution to create tuples with Sats and Weekinfo?

Try:
x=52
sats = np.linspace(1,x,x)
print sats
weekinfo=[]
satdict= ['m', 't', 'w', 'u', 'f', 'sa', 'su']
for k in range(x):
weekinfo.append((sats[int(k)], satdict[int(k)%7]))
print weekinfo
Not sure that's the exact answer you're looking for, but something along those lines might work

Related

How to compare variables in a list (number chars in order from 1 to n without changing the position of the chars in a list)

In short i want to number chars in order from 1 to n without changing the position of the chars in a list.
Suppose I have a list called key = ['c', 'a', 't'] How would i go about
assigning a number to each letter depending on where it is situated in the alphabet with respect to the other letters. Starting at 1 and going until len(key) such that our key becomes [ 2, 1, 3]
I'm really stumped. I have a way to convert them to numbers but very unsure as to how to compare them such that the above happens any help, tips, ideas or explanations would be appreciated.
this is what i have so far...
import string
key = list(input("enter key: ").upper())
num = []
for i in key:
num.append(string.ascii_uppercase.index(i)+1)
This solution assumes that duplicate entries should be assigned the same number, so that
# ['c','a','t'] -> [2, 1, 3]
# ['c','a','t','c','a','t'] -> [2, 1, 3, 2, 1, 3]
You can write a simple function like this:
def get_alphabet_pos(lst):
uniques = sorted(set(lst)) # set() to filter uniques, then order by value
numbers = {letter: i+1 for i, letter in enumerate(uniques)} # build a lookup dict
return [numbers[key] for key in lst]
get_alphabet_pos('cat') # [2, 1, 3]
So here's what happens in the function:
In line 1 of the function we convert your list to a set to remove any duplicate values. From the docs # https://docs.python.org/3/tutorial/datastructures.html#sets:
A set is an unordered collection with no duplicate elements.
Still in line 1 we sort the set and convert it back into a list. Thanks to #StefanPochmann for pointing out that sorted() takes care of the list conversion.
In line 2, we use enumerate() so we can iterate over the indices and values of our list of unique values: https://docs.python.org/3/library/functions.html#enumerate
The rest of line 2 is a simple dict comprehension to build a dictionary of letter -> number mappings. We use the dictionary in line 3 to look up the numbers for each letter in our input dict.
You might have to modify this slightly depending on how you want to handle duplicates :)

Python run set.intersection with set of sets as input

I am working with biological datasets, straight from transcriptome (RNA) to finding certain protein sequences. I have a set of the protein names for each dataset, and want to find which are common to all datasets. Due to how the data is processed, I end up with a one variable that contains all the sub sets.
Due to how the set.intersect() command works, it requires at least 2 sets as input:
IDs = set.intersection(transc1 & trans2)
However I only have one input, colA which contains 30 sets of 80 to 100 entries. Here is what I have so far:
from glob import glob
for file in glob('*_query.tsv'): #input all 30 datasets, first column with protein IDs
sources = file
colnames = ['a', 'b', 'c', 'd', 'e', 'f']
df = pandas.read_csv(sources, sep='\t', names=colnames) #colnames headers for df contruction
colA = df.a.tolist() #turn column a, protein IDs, into list
IDs = set(colA) #turn lists into sets
If I print(colA), the output is something like this, with two unnamed elements as sets:
set(['ID2', 'ID8', 'ID35', 'ID77', 'ID78', 'ID199', 'ID211'])
set(['ID1', 'ID5', 'ID8', 'ID88', 'ID105', 'ID205'])
At this point I get stuck. I can't get set.intersection() working with the IDs set of sets. Also tried pandas.merge(*IDs) for which the syntax seemed to work, but the number of entries for comparison exceeded the maximum (12).
I wanted to use sets because unlike lists, it should be quick to find common IDs between all the datasets. If there is a better way, I am all for it.
Help is much appreciated.

List to Dictionary with key to be a String Counter

I am new to python and was trying to implement a dictionary from a list with the key value to be a string counter
ex. the list is ['a','b','c']
I want this list to be displayed as
dict {'A1':'a','A2':'b','A3':'c'}
Thanks in advance
Given I understand your question correctly you want for the i-th element, a key-value pair 'Ai':c with c the character at position i. You can use dictionary comprehension:
{'A%s'%i:e for i,e in enumerate(data,1)}
Running this in the interpreter:
>>> data = ['a','b','c']
>>> {'A%s'%i:e for i,e in enumerate(data,1)}
{'A2': 'b', 'A3': 'c', 'A1': 'a'}
This is dictionary comprehension. Dictionary comprehension has as syntax:
{key(element):value(element) for element in iterable}
Where key and value are expressions that do something with element. For each element in the iterable. It will evaluate the expression and associate the value with the derived key.

Add values from a list of dictionaries to separate lists and find/count common values

I have a list containing dictionaries like this:
[{"abc":"da123-tap","efg":"xyzf","acd":"123-brf"}, {"abc":"ab234-tap","efg":"yuvi","acd":"345-brf"}]
I want all the values of abc in a list (list1) and all the values of efg in another list (list2).
Update to question:
I read a file to get this list of dictionaries:
[{"Sample":"da123-tap","Name":"john","dof":"Date(304239600000)","Account":"Clinic1"},
{"Sample":"da456-tap-tap","Name":"smith","dof":"Date(304239600000)","Account":"Clinic2"},
{"Sample":"da678-tap-tap","Name":"jane","dof":"Date(304239600000)","Account":"Clinic1"},
{"Sample":"da987-tap-tap","Name":"doe","dof":"Date(304239600000)","Account":"Clinic2"}]
Find:
Values from Sample in list1
Values from Account in list2
Count values of Sample that belong to Clinic1
Count values of Sample that belong to Clinic2
Only for educational reasons, the elegant way to direct values of different keys into multiple lists could be to use zip() and unpacking:
>>> l = [
... {"abc":"da123-tap","efg":"xyzf","acd":"123-brf"},
... {"abc":"ab234-tap","efg":"yuvi","acd":"345-brf"}
... ]
>>>
>>> a, b = zip(*[(item["abc"], item["efg"]) for item in l])
>>> print(a)
('da123-tap', 'ab234-tap')
>>> print(b)
('xyzf', 'yuvi')

Sorting a list in python with items that begin with specified letters

If I have a list:
list = ('john', 'adam', 'tom', 'danny')
and I want a sorted output with the items where the first letter is between 'a' and 'h', like:
('adam', 'danny', 'john')
which sorting function in Python do I need to complete this task?
This is the code i tried:
l = list()
while True:
s = raw_input("Enter a username: ")
l.append(s)
print sorted(l)
You need 2 distinct things: a list with just the elements that begin with an acceptable letter, and then the sorted version of that list. The former can be done with a list comprehension (although, as #jonrsharpe points out, you look like you want tuples, so you meat need to convert to a list & the convert the result back).