[Tutor] R: re question on array

jarod_v6 at libero.it jarod_v6 at libero.it
Thu Oct 30 09:32:01 CET 2014


Dear All,
Sorry for my bad presentation of my problem!!
I have this tipe of input:
A file with a long liste of gene ad the occurence for sample:

gene	Samples
FUS	SampleA
TP53	SampleA
ATF4	SampleB
ATF3	SampleC
ATF4	SampleD
FUS	SampleE
RORA	SampleE
RORA	SampleC

WHat I want to obtain is amtrix where I have the occurence for sample.
	SampleA	SampleB	SampleC	SampleD	SampleE
FUS	1	0	0	0	1
TP53	1	0	0	0	0
ATF4	0	1		1	0
ATF3	0	0	1	0	0
RORA	0	0	1	0	

In that way I count count the occurence in fast way!

At the moment I only able to do the list of the rownames and the sample names. 
Unfortunately I don't know how to create this matrix.
Cold you help me ?
Thanks for the patience and the help




>----Messaggio originale----
>Da: tutor-request at python.org
>Data: 30/10/2014 6.41
>A: <tutor at python.org>
>Ogg: Tutor Digest, Vol 128, Issue 74
>
>Send Tutor mailing list submissions to
>	tutor at python.org
>
>To subscribe or unsubscribe via the World Wide Web, visit
>	https://mail.python.org/mailman/listinfo/tutor
>or, via email, send a message with subject or body 'help' to
>	tutor-request at python.org
>
>You can reach the person managing the list at
>	tutor-owner at python.org
>
>When replying, please edit your Subject line so it is more specific
>than "Re: Contents of Tutor digest..."
>
>
>Today's Topics:
>
>   1. Re: Would somebody kindly... (Steven D'Aprano)
>   2. Re: Would somebody kindly... (Dave Angel)
>   3. question  on array Operation (jarod_v6 at libero.it)
>   4. Re: question  on array Operation (Peter Otten)
>   5. Re: question  on array Operation (Alan Gauld)
>   6. Re: question  on array Operation (Steven D'Aprano)
>   7. Re: Would somebody kindly... (Clayton Kirkwood)
>
>
>----------------------------------------------------------------------
>
>Message: 1
>Date: Wed, 29 Oct 2014 23:04:18 +1100
>From: Steven D'Aprano <steve at pearwood.info>
>To: tutor at python.org
>Subject: Re: [Tutor] Would somebody kindly...
>Message-ID: <20141029120417.GA26866 at ando.pearwood.info>
>Content-Type: text/plain; charset=us-ascii
>
>On Tue, Oct 28, 2014 at 04:13:19PM -0700, Clayton Kirkwood wrote:
>> Explain this double speak(>:
>> 
>> [pair for pair in values if key == pair[0]]
>
>Translated to a regular for-loop:
>
>result = []
>for pair in values:
>    if key == pair[0]:
>        result.append(pair)
>
>
>It iterates over the sequence `values`, extracting something called 
>`pair`. If the first item of `pair` == key, then the pair is placed in 
>the resulting list.
>
>py> values = [ ('a', 1), ('b', 2), ('a', 5), ('c', 7)]
>py> key = 'a'
>py> [pair for pair in values if key == pair[0]]
>[('a', 1), ('a', 5)]
>
>
>-- 
>Steven
>
>
>------------------------------
>
>Message: 2
>Date: Wed, 29 Oct 2014 08:29:56 -0400 (EDT)
>From: Dave Angel <davea at davea.name>
>To: tutor at python.org
>Subject: Re: [Tutor] Would somebody kindly...
>Message-ID: <m2qmd2$ji$1 at ger.gmane.org>
>Content-Type: text/plain; charset=UTF-8
>
>"Clayton Kirkwood" <crk at godblessthe.us> Wrote in message:
>> 
>> 
>> !-----Original Message-----
>> !From: Tutor [mailto:tutor-bounces+crk=godblessthe.us at python.org] On
>> !Behalf Of Dave Angel
>> !Sent: Tuesday, October 28, 2014 6:34 PM
>> !To: tutor at python.org
>> !Subject: Re: [Tutor] Would somebody kindly...
>> !
>> !
>> !>
>> ! Explain this double speak(>:
>> !>  [pair for pair in values if key == pair[0]]
>> !
>> !>  I understand the ?for pair in values?. I assume the first  ?pair?
>> !> creates the namespace
>> !
>> !The namespace question depends on the version of Python. Python  2.x
>> !does not do any scoping.
>> !
>> !But in version 3.x, the variable pair will go away.
>> !
>> !So please tell us the version you're asking about.
>> 
>> I am using 3.4.1.
>> 
>
>Have you somehow configured your email program to use exclamation
> points for quoting instead of the standard greater-than symbol?
> "!" instead of ">" ? If so, do you mind changing it
> back?
>
>In 3.4.1, let's consider the following code.
>
>thingie = 2
>mylist = [(2,55), "charlie", [2, "item2", 12]]
>x = [78 for item in mylist if item[0] == thingie]
>
>What will happen in the list comprehension, and what will be the
> final value of x ?
>
>First an anonymous list object will be created.  This eventually
> will be bound to x, but not till the comprehension is
> successfully completed. Next a locally scoped variable item is
> created.  This goes away at the end of the comprehension, 
> regardless of how we exit.
>
>Next the 0th value from mylist is bound to item. It happens to be
> a tuple, but not from anything the comprehension
> decides.
>Next the expression item [0] == thingie is evaluated.  If it's
> true, then the int 78 is appended to the anonymous
> list.
>
>Now the previous group of actions is repeated for the 1th value of
> mylist. So now item is a string, and the zeroth character of the
> string is compared with the int 2. Not equal, so 72 doesn't get
> appended.
>
>Similarly for the 2th item. The first element of that list is
> equal to 2, so another 72 is appended.
>
>Now the anonymous list is bound to x.
>
>print (x)
>[72, 72]
>
>
>
>-- 
>DaveA
>
>
>
>------------------------------
>
>Message: 3
>Date: Wed, 29 Oct 2014 17:08:28 +0100 (CET)
>From: "jarod_v6 at libero.it" <jarod_v6 at libero.it>
>To: tutor at python.org
>Subject: [Tutor] question  on array Operation
>Message-ID:
>	<700633221.1075021414598908994.JavaMail.defaultUser at defaultHost>
>Content-Type: text/plain; charset="utf-8"
>
>Dear All,
>
>I have a long matrix where I have the samples (1to n) and then I have (1to j )
elements.
>I would like to count how many times  each j element are present on each 
samples.
>
>So the question is which is the best algoritm for obtain the results. The 
result I want is a table like that.
>
>
>
>A,B,D,E,F
>AA,1,0,1,0
>BB1,1,1,1
>CC0,0,1,0
>DD01,0,1,0
>
>
>Do you have any suggestion on how to do this?  only using a loop like:
>
>A is a list with some names A=[AA,BB,ZZ,TT,NN]
>
>if AA in A:
>    print 1
>else :
>    print 0
>
>thanks in advance for any help
>-------------- next part --------------
>An HTML attachment was scrubbed...
>URL: <http://mail.python.
org/pipermail/tutor/attachments/20141029/ca8bdc9c/attachment-0001.html>
>
>------------------------------
>
>Message: 4
>Date: Wed, 29 Oct 2014 17:49:06 +0100
>From: Peter Otten <__peter__ at web.de>
>To: tutor at python.org
>Subject: Re: [Tutor] question  on array Operation
>Message-ID: <m2r5q2$ibm$1 at ger.gmane.org>
>Content-Type: text/plain; charset="ISO-8859-1"
>
>jarod_v6 at libero.it wrote:
>
>> Dear All,
>> 
>> I have a long matrix where I have the samples (1to n) and then I have (1to
>> j )elements.
>> I would like to count how many times  each j element are present on each
>> samples.
>> 
>> So the question is which is the best algoritm for obtain the results. The
>> result I want is a table like that.
>> 
>> 
>> 
>> A,B,D,E,F
>> AA,1,0,1,0
>> BB1,1,1,1
>> CC0,0,1,0
>> DD01,0,1,0
>> 
>> 
>> Do you have any suggestion on how to do this?  only using a loop like:
>> 
>> A is a list with some names A=[AA,BB,ZZ,TT,NN]
>> 
>> if AA in A:
>>     print 1
>> else :
>>     print 0
>> 
>> thanks in advance for any help
>
>I'm sorry I have no idea what you are trying to do.
>
>I can't believe that this is your "best effort" to explain the problem, so I 
>won't bother to guess.
>
>
>
>------------------------------
>
>Message: 5
>Date: Wed, 29 Oct 2014 17:29:12 +0000
>From: Alan Gauld <alan.gauld at btinternet.com>
>To: tutor at python.org
>Subject: Re: [Tutor] question  on array Operation
>Message-ID: <m2r858$2d5$1 at ger.gmane.org>
>Content-Type: text/plain; charset=windows-1252; format=flowed
>
>On 29/10/14 16:08, jarod_v6 at libero.it wrote:
>
>> I have a long matrix where I have the samples (1to n) and then I have
>> (1to j )elements.
>> I would like to count how many times  each j element are present on each
>> samples.
>
>That probably makes sense to you but it doesn't to me.
>Lets assuyme you have n=10 and j=5.
>That would suggest you have a 10x5 matrix of values?
>
>Now what do you mean by 'each j' element? The last one in the row?
>
>So for this small example:
>
>1,2,3,4,5,   -> count = 1 - there is only one 5
>1,2,1,2,1,   -> count = 3 - there are 3 ones
>A,3,B,2,A    -> count = 2 - there are 2 As
>
>Is that what you want?
>
>> So the question is which is the best algoritm for obtain the results.
>> The result I want is a table like that.
>>
>> A,B,D,E,F
>> AA,1,0,1,0
>> BB1,1,1,1
>> CC0,0,1,0
>> DD01,0,1,0
>
>That doesn';t senm to match your description above. You will
>need to show us both input and output data before we can
>understand the transformation you are looking for.
>
>> Do you have any suggestion on how to do this?
>
>Not until I understand what you want.
>
>
>-- 
>Alan G
>Author of the Learn to Program web site
>http://www.alan-g.me.uk/
>http://www.flickr.com/photos/alangauldphotos
>
>
>
>------------------------------
>
>Message: 6
>Date: Thu, 30 Oct 2014 09:06:29 +1100
>From: Steven D'Aprano <steve at pearwood.info>
>To: tutor at python.org
>Subject: Re: [Tutor] question  on array Operation
>Message-ID: <20141029220629.GB26866 at ando.pearwood.info>
>Content-Type: text/plain; charset=us-ascii
>
>On Wed, Oct 29, 2014 at 05:08:28PM +0100, jarod_v6 at libero.it wrote:
>> Dear All,
>> 
>> I have a long matrix where I have the samples (1to n) and then I have (1to 
j )elements.
>> I would like to count how many times  each j element are present on each 
samples.
>
>Jarod, looking at your email address, I am guessing that English is not 
>your native language. I'm afraid that I cannot understand what you are 
>trying to do. Please show a *simple* example.
>
>I will make a *guess* that you have something like this:
>
>matrix = [ # three samples of seven values each
>           [2, 4, 6, 8, 6, 9, 5], 
>           [3, 5, 1, 7, 9, 8, 8], 
>           [1, 2, 0, 6, 6, 2, 1],
>           ]
>
>and then you want to count how many each element [0, 1, 2, 3, 4, 5, 6, 
>7, 8, 9] appear in each sample:
>
>
>from collections import Counter
>for i, sample in enumerate(matrix, 1):
>    c = Counter(sample)
>    print("Sample %d" % i)
>    print(c)
>    
>
>which gives me:
>
>Sample 1
>Counter({6: 2, 2: 1, 4: 1, 5: 1, 8: 1, 9: 1})
>Sample 2
>Counter({8: 2, 1: 1, 3: 1, 5: 1, 7: 1, 9: 1})
>Sample 3
>Counter({1: 2, 2: 2, 6: 2, 0: 1})
>
>
>Does that help?
>
>If not, you have to explain what you need better.
>
>
>-- 
>Steven
>
>
>------------------------------
>
>Message: 7
>Date: Wed, 29 Oct 2014 22:39:53 -0700
>From: "Clayton Kirkwood" <crk at godblessthe.us>
>To: <tutor at python.org>
>Subject: Re: [Tutor] Would somebody kindly...
>Message-ID: <06b801cff403$eed61340$cc8239c0$@us>
>Content-Type: text/plain;	charset="UTF-8"
>
>
>
>>-----Original Message-----
>>From: Tutor [mailto:tutor-bounces+crk=godblessthe.us at python.org] On
>>Behalf Of Dave Angel
>>Sent: Wednesday, October 29, 2014 5:30 AM
>>To: tutor at python.org
>>Subject: Re: [Tutor] Would somebody kindly...
>>
>>"Clayton Kirkwood" <crk at godblessthe.us> Wrote in message:
>>>
>>>
>>> !-----Original Message-----
>>> !From: Tutor [mailto:tutor-bounces+crk=godblessthe.us at python.org] On
>>> !Behalf Of Dave Angel
>>> !Sent: Tuesday, October 28, 2014 6:34 PM
>>> !To: tutor at python.org
>>> !Subject: Re: [Tutor] Would somebody kindly...
>>> !
>>> !
>>> !>
>>> ! Explain this double speak(>:
>>> !>  [pair for pair in values if key == pair[0]] !
>>> !>  I understand the ?for pair in values?. I assume the first  ?pair?
>>> !> creates the namespace
>>> !
>>> !The namespace question depends on the version of Python. Python  2.x
>>> !does not do any scoping.
>>> !
>>> !But in version 3.x, the variable pair will go away.
>>> !
>>> !So please tell us the version you're asking about.
>>>
>>> I am using 3.4.1.
>>>
>>
>>Have you somehow configured your email program to use exclamation
>>points for quoting instead of the standard greater-than symbol?
>> "!" instead of ">" ? If so, do you mind changing it  back?
>>
>>In 3.4.1, let's consider the following code.
>>
>>thingie = 2
>>mylist = [(2,55), "charlie", [2, "item2", 12]] x = [78 for item in
>>mylist if item[0] == thingie]
>>
>>What will happen in the list comprehension, and what will be the  final
>>value of x ?
>>
>>First an anonymous list object will be created.  This eventually  will
>>be bound to x, but not till the comprehension is  successfully
>>completed. Next a locally scoped variable item is  created.  This goes
>>away at the end of the comprehension,  regardless of how we exit.
>>
>>Next the 0th value from mylist is bound to item. It happens to be  a
>>tuple, but not from anything the comprehension  decides.
>>Next the expression item [0] == thingie is evaluated.  If it's  true,
>>then the int 78 is appended to the anonymous  list.
>>
>>Now the previous group of actions is repeated for the 1th value of
>>mylist. So now item is a string, and the zeroth character of the  string
>>is compared with the int 2. Not equal, so 72 doesn't get  appended.
>>
>>Similarly for the 2th item. The first element of that list is  equal to
>>2, so another 72 is appended.
>>
>>Now the anonymous list is bound to x.
>>
>>print (x)
>>[72, 72]
>
>So, in this case, the assignment to x is external. Often I don't see an 
external assignment, but there is an item in the first position within the 
comprehension. You don't have that here. When you have [item for item in [list] 
if item[0] == key], after the iteration completes does item equal the matched 
entities or does it have the original item? I understand that if we had x = 
[dsfasdfasdf] x will be a list (never a tuple?) with the matches, but what 
happens to the first item?
>
>This is from a previous email--
>When I run:
>values = [ ('a', 1), ('b', 2), ('a', 5), ('c', 7)]
>key = 'a'
>pair=[]
>[pair for pair in values if key == pair[0]]
>print(pair)
>
>I get [].
>
>When I run:
>values = [ ('a', 1), ('b', 2), ('a', 5), ('c', 7)]
>key = 'a'
>pair=[]
>x=[pair for pair in values if key == pair[0]]
>print(x)
>
>I get [('a', 1), ('a', 5)]
>
>So, what does that first pair do? I see and have used the first 
comprehension.
>
>
>Clayton
>
>
>>
>>
>>
>>--
>>DaveA
>>
>>_______________________________________________
>>Tutor maillist  -  Tutor at python.org
>>To unsubscribe or change subscription options:
>>https://mail.python.org/mailman/listinfo/tutor
>
>
>
>
>------------------------------
>
>Subject: Digest Footer
>
>_______________________________________________
>Tutor maillist  -  Tutor at python.org
>https://mail.python.org/mailman/listinfo/tutor
>
>
>------------------------------
>
>End of Tutor Digest, Vol 128, Issue 74
>**************************************
>




More information about the Tutor mailing list