How to zip a list of lists

2024/7/8 7:52:27

I have a list of lists

sample = [['A','T','N','N'],['T', 'C', 'C', 'C']],[['A','T','T','N'],['T', 'T', 'C', 'C']].

I am trying to zip the file such that only A/T/G/C are in lists and the output needs to be a list

[['AT','TCCC'],['ATT','TTCC']]

When I use this code:

tt = ["".join(y for y in x if y in {'A','G','T','C'}) for x in sample]

However, I only get the output as:

['ATT','TTCC']

Any suggestions where I am going wrong?

In my actual code I am first transposing the lists:

seq_list = [['TCCGGGGGTATC', 'TCCGTGGGTATC', ...]]  # one nested listnumofpops = len(seq_list)### Tranposing. Moving along the columns only#column_list = []
for k in range(len(seq_list)):column_list = [[] for i in range(len(seq_list[k][0]))]for seq in seq_list[k]:for i, nuc in enumerate(seq):column_list[i].append(nuc)ddd = column_listprint dddtt = ["".join(y for y in x if y in {'A','G','T','C'}) for x in ddd]
print tt
Answer

Your actual code is discarding lists. You only ever process the last entry.

Your code works fine otherwise. Just do that in the loop and then append the result to some final list:

results = []for k in range(len(seq_list)):column_list = [[] for i in range(len(seq_list[k][0]))]for seq in seq_list[k]:for i, nuc in enumerate(seq):column_list[i].append(nuc)# process `column_list` here, in the loop (no need to assign to ddd)tt = ["".join(y for y in x if y in {'A','G','T','C'}) for x in column_list]results.append(tt)

Note that you could use the zip() function instead of your transposition list:

results = []
for sequence in seq_list:for column_list in zip(*sequence):tt = [''.join([y for y in x if y in 'AGTC']) for x in column_list]results.append(tt)
https://en.xdnf.cn/q/120137.html

Related Q&A

I want to make a Guess the number code without input

import random number = random.randint(1, 10)player_name = "doo" number_of_guesses = 0 print(I\m glad to meet you! {} \nLet\s play a game with you, I will think a number between 1 and 10 then …

How to zip keys within a list of dicts

I have this object: dvalues = [{column: Environment, parse_type: iter, values: [AirportEnclosed, Bus, MotorwayServiceStation]}, {column: Frame Type, parse_type: list, values: [All]}]I want a zipped out…

AttributeError: DataFrame object has no attribute allah1__27

Im trying to solve this and Im pretty sure the code is right but it keeps getting me the same Error.I have tried this:import datetime from datetime import datetime as datettest_df = shapefile.copy() te…

How to convert csv to dictionary of dictionaries in python?

I have a CSV file shown below.I need to convert CSV to dictionary of dictionaries using python.userId movieId rating 1 16 4 1 24 1.5 2 32 4 2 47 4 2 …

Mo Money- Making an algorithm to solve two variable algebra problems

A cash drawer contains 160 bills, all 10s and 50s. The total value ofthe 10s and 50s is $1,760.How many of each type of bill are in the drawer? You can figure thisout by trial and error (or by doing a…

How to explode Python Pandas Dataframe and merge strings from other dataframe?

Dataframe1 has a lot of rows and columns of data. One column is Text. Certain rows in Text column have strings and some strings include within the strings this {ExplodeEList2} How to explode (expand) t…

Creating a new column with numbers in Pandas to group with a column with existing numbers

Good day, I have a column from a data frame here:A231011 22My objective is to create a new column and associate the numbers like this:A file_number 23 8 10 6 11 6 22 8As…

How to get PDF file from the binary data of SoftLayers quote?

I got the binary data by "getPdf" method of SoftLayers API.Ref. BillingSoftLayer_Billing_Order_Quote::getPdf | SoftLayer Development Network - http://sldn.softlayer.com/reference/services/Sof…

How to list of elements and use those elements as a header of pandas dataframe?

I have a list with some elements. For example: list= [name, phone_number,age,gender] I want to use these elements as a header or column name in a pandas dataframe. I would really appreciate your ideas.…

Averaging Filter using python

I am new in python and trying apply averaging filter on image as the way i understand averaging concept summing up the neighboring elements including itself and divide it by number of elementstechnique…