473,405 Members | 2,300 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Simple question about python logic.

I have a file containing following data. But the dimension can be
different.

A B C D E F G
3 4 1 5 6 2 4
7 2 4 1 6 9 3
3 4 1 5 6 2 4
7 2 4 1 6 9 3
..
..
..
..

What is the best approach to make a column vector with the name such
as A B, etc?

Oct 12 '07 #1
3 1013
On Oct 12, 11:32 am, "seungchan...@gmail.com" <seungchan...@gmail.com>
wrote:
I have a file containing following data. But the dimension can be
different.

A B C D E F G
3 4 1 5 6 2 4
7 2 4 1 6 9 3
3 4 1 5 6 2 4
7 2 4 1 6 9 3
.
.
.
.

What is the best approach to make a column vector with the name such
as A B, etc?
Use a dict. dict objects are the main way we map names to objects on
the fly. Simple example:

columns = {}
columns["A"] = [3,7,3,3]
columns["B"] = [4,2,4,2]
Filling in the columns with data from your file is left as an
exercise.
Carl Banks

Oct 12 '07 #2
I have a file containing following data. But the dimension can be
different.

A B C D E F G
3 4 1 5 6 2 4
7 2 4 1 6 9 3
3 4 1 5 6 2 4
7 2 4 1 6 9 3
.
.
.
.

What is the best approach to make a column vector with the name such
as A B, etc?

There are a couple different ways to go about it depending on

1) whether just one or whether both dimensions can be different
2) whether you want to extract each column vector

Both of the following should allow for an arbitrary number of
columns:

To map everything, you can do something like

#######################################
infile = file("in.txt")
header_row = infile.next().rstrip('\n').split()
values = [[] for header in header_row]
for line in infile:
line = line.rstrip('\n').split()
for dest, value in zip(values, line):
dest.append(value)
infile.close()
results = dict(zip(header_row, values))
#######################################
which you can then use with

results['A']

However, if you just want a particular column from the file and
don't care about the rest, you can do something like

#######################################
from itertools import islice

column_c = [
line.rstrip('\n').split()[2]
for line in islice(file('in'), 1, None)
]
#######################################

where "2" is the zero-based column offset you want (in this case,
column C = 2)

-tkc

Oct 12 '07 #3
Tim Chase wrote:
>I have a file containing following data. But the dimension can be
different.

A B C D E F G
3 4 1 5 6 2 4
7 2 4 1 6 9 3
3 4 1 5 6 2 4
7 2 4 1 6 9 3
.
.
.
.

What is the best approach to make a column vector with the name such
as A B, etc?


There are a couple different ways to go about it depending on

1) whether just one or whether both dimensions can be different
2) whether you want to extract each column vector

Both of the following should allow for an arbitrary number of
columns:

To map everything, you can do something like

#######################################
infile = file("in.txt")
header_row = infile.next().rstrip('\n').split()
values = [[] for header in header_row]
for line in infile:
line = line.rstrip('\n').split()
for dest, value in zip(values, line):
dest.append(value)
infile.close()
results = dict(zip(header_row, values))
#######################################
which you can then use with

results['A']

However, if you just want a particular column from the file and
don't care about the rest, you can do something like

#######################################
from itertools import islice

column_c = [
line.rstrip('\n').split()[2]
for line in islice(file('in'), 1, None)
]
#######################################

where "2" is the zero-based column offset you want (in this case,
column C = 2)

-tkc
Numpy appears to have this capability.

Colin W.

Oct 12 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

220
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have...
40
by: Shufen | last post by:
Hi all, Can someone who has use PHP before and know quite well about the language, tell me what are the stuffs that Python offers and PHP doesn't. A few examples will be nice. I know about the...
38
by: jrlen balane | last post by:
basically what the code does is transmit data to a hardware and then receive data that the hardware will transmit. import serial import string import time from struct import * ser =...
11
by: JKop | last post by:
Take the following simple function: unsigned long Plus5Percent(unsigned long input) { return ( input + input / 20 ); } Do yous ever consider the possibly more efficent:
19
by: adriancico | last post by:
Hi I am working on a python app, an outliner(a window with a TreeCtrl on the left to select a document, and a RichTextBox at the right to edit the current doc). I am familiarized with OOP...
15
by: Bjoern Schliessmann | last post by:
Hello all, I'm trying to simulate simple electric logic (asynchronous) circuits. By "simple" I mean that I only want to know if I have "current" or "no current" (it's quite digital) and the only...
0
by: Jean-Paul Calderone | last post by:
On Tue, 30 Oct 2007 14:09:39 -0000, Steven D'Aprano <steve@remove-this-cybersource.com.auwrote: If you have some time to experiment, PyPy has some support for logic programming with Python. ...
0
by: Stodge | last post by:
Hi folks, new to Boost Python and struggling to build a prototype at work. I thought I'd start with a conceptual question to help clarify my understanding. I already have a basic prototype working...
15
by: erik.oosterwaal | last post by:
Hi All, I have been developing websites in classic asp using VB script for a long while now. Due to the fact that I also took a detour to developing ColdFusion, and the fact the companies I...
7
by: CSharper | last post by:
Yesterday I had a heated discussion with my colleagues on what is a data centric application and having business logic in sql. I have group of people who wants to include all the business logic in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.