PDA

View Full Version : [SOLVED] which is better in python: arrays,dictionaries,lists



dharanitharan
January 24th, 2010, 03:39 AM
Hi friends,
I'm new to pthon. I want to store some characters in a data structures(arrays or lists or dictionaries). then i will compare those things with certain characters obtained at run time. In this case wat data structure should i use. Im really need it... Plz help me :-)



Thanks for ur reply,
Dharanitharan.A
http://naturefactory.wordpress.com

Can+~
January 24th, 2010, 03:52 AM
Uhm, a string?

jflaker
January 24th, 2010, 03:56 AM
Hi friends,
I'm new to pthon. I want to store some characters in a data structures(arrays or lists or dictionaries). then i will compare those things with certain characters obtained at run time. In this case wat data structure should i use. Im really need it... Plz help me :-)



Thanks for ur reply,
Dharanitharan.A
http://naturefactory.wordpress.com

EVERYTHING is a string unless you can confirm it is a number...

dharanitharan
January 24th, 2010, 04:06 AM
Sorry friends. Im a beginner, im not aware of that.. Thaks for making me to understand.. Actually i wanna do this:

char a[]={'a','b','\n',';'};

How to decalre this in python

jeffathehutt
January 24th, 2010, 04:13 AM
a = "ab\n;"

That should do it :)

dharanitharan
January 24th, 2010, 04:16 AM
a = "ab\n;"

That should do it :)
No no. I want to know how to declare in python

snova
January 24th, 2010, 04:22 AM
No no. I want to know how to declare in python

That is how you do it in Python. jeffathehutt's suggestion is, by all means, also probably the way it should be done. I can't see why you would want a list of characters instead of a string, but if you insist on that:


a = ["a", "b", "\n", ";"]

a = list("ab\n;")

Can+~
January 24th, 2010, 04:50 AM
Ok, let's say it in another way:

What do you want to do with said string?

For instance, if you want to compare, you don't need to do it C-style (it seems that you have C background):


>>> "Hello World" == "Hello World"
True
>>> "orl" in "Hello World"
True
>>> "Hello World"[:4]
'Hell'
>>> "Hello World"[::-1]
'dlroW olleH'
>>> "Hello World".replace("World", "Universe")
'Hello Universe'


Take a look at string methods (http://docs.python.org/library/stdtypes.html#string-methods) and sequence types (http://docs.python.org/library/stdtypes.html#sequence-types-str-unicode-list-tuple-buffer-xrange).

dharanitharan
January 24th, 2010, 04:55 AM
Ok, let's say it in another way:

What do you want to do with said string?
Actually i want to design a C language parser using python....

If my input is lik this:

#include<stdio.h>
void main()
{
printf("Hello World !!!");
}

then i want the output after parsing lik:
#include, <stdio.h>, void, main, (, ), {, }, printf and so on

Can+~
January 24th, 2010, 05:21 AM
Actually i want to design a C language parser using python....

If my input is lik this:

#include<stdio.h>
void main()
{
printf("Hello World !!!");
}

then i want the output after parsing lik:
#include, <stdio.h>, void, main, (, ), {, }, printf and so on

Ok, that's not "parsing", it's more like... changing the format.

But anyway, this is way out of your league. Building a "compiler" or an "interpreter" is not as simple as it seems, there's a lot of theory involved, you'd need a book like Aho's Dragon books (http://en.wikipedia.org/wiki/Dragon_Book_%28computer_science%29).

But whatever, first focus on learning the language, your posts suggest that you're rushing too much, assuming that you need to declare things in python, it's dynamically typed, you don't need to. Everything in python are objects and have methods and properties, it's not like C where there's memory and functions, it's another paradigm.

dharanitharan
January 24th, 2010, 05:28 AM
Ok, that's not "parsing", it's more like... changing the format.

But anyway, this is way out of your league. Building a "compiler" or an "interpreter" is not as simple as it seems, there's a lot of theory involved, you'd need a book like Aho's Dragon books (http://en.wikipedia.org/wiki/Dragon_Book_%28computer_science%29).

But whatever, first focus on learning the language, your posts suggest that you're rushing too much, assuming that you need to declare things in python, it's dynamically typed, you don't need to. Everything in python are objects and have methods and properties, it's not like C where there's memory and functions, it's another paradigm.
thanks man... First i ve to come from basics.. i understood ..

CptPicard
January 24th, 2010, 03:35 PM
Ok, that's not "parsing", it's more like... changing the format.


Well, it seems like tokenization. Perhaps a good exercise for the uninitiated... first, read a file, then, learn to split at whitespace... finally, perhaps implement finite language recognizer using a state machine :p