Hello everyone,
I'm trying to work through a bit of a logic issue I'm having with a
script I'm writing. Essentially, I have a list that's returned to
me from another command that I need to regroup based on some aribitrary
length.
For the purposes of this question, the list will be:
t = [ "a", "b", "c", "n", "a", "a", "t", "t", "t" ]
Now, I know that every 3rd element of the list belongs together:
Group 1 = 0, 3, 6
Group 2 = 1, 4, 7
Group 3 = 2, 5, 8
I'm trying to sort this list out so that I get a list of lists that
contain the correct elements:
Goal = [ [ "a", "n", "t"], [ "b", "a", "t"],
["c", "a", "t" ] ]
The actual data isn't as simple as this, but if I can get the logic
sorted out, I can handle the other part.
Anyone have any good ideas on how to do this? 5 1199
Steven skrev:
For the purposes of this question, the list will be:
t = [ "a", "b", "c", "n", "a", "a", "t", "t", "t" ]
Now, I know that every 3rd element of the list belongs together:
Group 1 = 0, 3, 6
Group 2 = 1, 4, 7
Group 3 = 2, 5, 8
I'm trying to sort this list out so that I get a list of lists
that contain the correct elements:
Goal = [ [ "a", "n", "t"], [ "b", "a", "t"],
["c", "a", "t" ] ]
#v+
>>t = [ "a", "b", "c", "n", "a", "a", "t", "t", "t" ] [t[i::3] for i in range(3)]
[['a', 'n', 't'], ['b', 'a', 't'], ['c', 'a', 't']]
>>>
#v-
Cheers,
--
Klaus Alexander Seistrup
SubZeroNet, Copenhagen, Denmark http://magnetic-ink.dk/
Steven wrote:
Hello everyone,
I'm trying to work through a bit of a logic issue I'm having with a
script I'm writing. Essentially, I have a list that's returned to
me from another command that I need to regroup based on some aribitrary
length.
For the purposes of this question, the list will be:
t = [ "a", "b", "c", "n", "a", "a", "t", "t", "t" ]
Now, I know that every 3rd element of the list belongs together:
Group 1 = 0, 3, 6
Group 2 = 1, 4, 7
Group 3 = 2, 5, 8
I'm trying to sort this list out so that I get a list of lists that
contain the correct elements:
Goal = [ [ "a", "n", "t"], [ "b", "a", "t"],
["c", "a", "t" ] ]
The actual data isn't as simple as this, but if I can get the logic
sorted out, I can handle the other part.
Anyone have any good ideas on how to do this?
how about :
>>t = [ "a", "b", "c", "n", "a", "a", "t", "t", "t" ] [t[i::3] for i in range(0,len(t)/3)]
[['a', 'n', 't'], ['b', 'a', 't'], ['c', 'a', 't']]
--
Bill Pursell
For the purposes of this question, the list will be:
>
t = [ "a", "b", "c", "n", "a", "a", "t", "t", "t" ]
Now, I know that every 3rd element of the list belongs together:
Group 1 = 0, 3, 6
Group 2 = 1, 4, 7
Group 3 = 2, 5, 8
I'm trying to sort this list out so that I get a list of lists that
contain the correct elements:
Goal = [ [ "a", "n", "t"], [ "b", "a", "t"],
["c", "a", "t" ] ]
Well, the following worked for me:
>>t = [ "a", "b", "c", "n", "a", "a", "t", "t", "t" ] stride = 3 Goal = [t[i::stride] for i in range(stride)] Goal
[['a', 'n', 't'], ['b', 'a', 't'], ['c', 'a', 't']]
Or, if you like, in this example:
>>[''.join(t[i::stride]) for i in range(stride)]
['ant', 'bat', 'cat']
if that's of any use.
-tkc
Klaus Alexander Seistrup wrote:
>t = [ "a", "b", "c", "n", "a", "a", "t", "t", "t" ] [t[i::3] for i in range(3)]
[['a', 'n', 't'], ['b', 'a', 't'], ['c', 'a', 't']]
Klaus,
Thanks for the fast reply! Had I taken the time to look at the
list-type docs (which I did to understand how you were spliting the
list), I'd probably have seen the slicing with step option. Another
RTFM issue for me.
Thanks again,
Steven
On 2006-08-21, Steven <sf********@gmail.comwrote:
Hello everyone,
I'm trying to work through a bit of a logic issue I'm having with a
script I'm writing. Essentially, I have a list that's returned to
me from another command that I need to regroup based on some aribitrary
length.
For the purposes of this question, the list will be:
t = [ "a", "b", "c", "n", "a", "a", "t", "t", "t" ]
Now, I know that every 3rd element of the list belongs together:
Group 1 = 0, 3, 6
Group 2 = 1, 4, 7
Group 3 = 2, 5, 8
from itertools import islice
grouped = []
grouped.append(list(islice(t, 0, None, 3))
grouped.append(list(islice(t, 1, None, 3))
grouped.append(list(islice(t, 2, None, 3))
grouped.sort()
This can probably be simplified and generalized, but I'm a novice, and
that's a start.
--
Neil Cerutti This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Rajarshi Guha |
last post by:
Hi,
is there an efficient (pythonic) way in which I could split a list into
say 5 groups? By split I mean the the first x members would be one group,
the next x members another group and so on 5...
|
by: TB |
last post by:
Hi,
Is there an elegant way to assign to a list from a list of unknown
size? For example, how could you do something like:
>>> a, b, c = (line.split(':'))
if line could have less than three...
|
by: Xah Lee |
last post by:
here's another interesting algorithmic exercise, again from part of a
larger program in the previous series.
Here's the original Perl documentation:
=pod
merge($pairings) takes a list of...
|
by: Guy Hocking |
last post by:
Hi there,
I am having a few problems compiling a list box that is conditional on what
is selected in another list box.
What i need is a List box (lstArea) that displays one thing when the List...
|
by: Alex Dempsey |
last post by:
Recently I tried to slice every element of a list of strings. First I tried:
f = open("export.xls", "r")
lines = f.readlines()
for line in lines:
line = line
line = line.split('\"\t\"')
...
|
by: Andy M |
last post by:
ALERT
There is a person by the name of Mike Cox who's trying
to turn this mailing list into a Big-8 newsgroup. Many
of you know that this and most of the other postresql
mailing lists are...
|
by: Rob Cowie |
last post by:
I'm having a bit of trouble with this so any help would be gratefully
recieved...
After splitting up a url I have a string of the form
'tag1+tag2+tag3-tag4', or '-tag1-tag2' etc. The first tag...
|
by: rkmr.em |
last post by:
Hi
I need to process a really huge text file (4GB) and this is what i
need to do. It takes for ever to complete this. I read some where that
"list comprehension" can fast up things. Can you point...
|
by: Jordan |
last post by:
I want to store small collections of objects that have a unique id
string:"Name", so I opted to use a Dictionary<string,MyObject>
collection. However, since my collection will rarely exceed 10 or...
|
by: shadow_ |
last post by:
Hi i m new at C and trying to write a parser and a string class.
Basicly program will read data from file and splits it into lines then
lines to words. i used strtok function for splitting data to...
|
by: Naresh1 |
last post by:
What is WebLogic Admin Training?
WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
|
by: antdb |
last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine
In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
|
by: WisdomUfot |
last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
|
by: Matthew3360 |
last post by:
Hi,
I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
|
by: Oralloy |
last post by:
Hello Folks,
I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA.
My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
|
by: Carina712 |
last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
|
by: BLUEPANDA |
last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
|
by: Johno34 |
last post by:
I have this click event on my form. It speaks to a Datasheet Subform
Private Sub Command260_Click()
Dim r As DAO.Recordset
Set r = Form_frmABCD.Form.RecordsetClone
r.MoveFirst
Do
If...
|
by: jack2019x |
last post by:
hello, Is there code or static lib for hook swapchain present?
I wanna hook dxgi swapchain present for dx11 and dx9.
| |