Python- How to append to an array


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Python- How to append to an array
# 1  
Old 02-01-2011
Python- How to append to an array

Hello, I wrote the following in python that parses mathematical expressions and I'm trying to store the results in an array. For example, if I enter 3+4*2, the array should be ['3', '4','*','2']. Unfortunately, I get the following:
['3']
['4']
['*']
['2']

Can anyone help me figure out what is wrong? The tokenizer seems to work fine. Just can't append to the array. I'd also like to add the word 'end' at the end of the array so that my ideal output would be ['3', '4','*','2','end']
Code:
from tokenizer import *

while get_expression():
	t = get_next_token()
	while t:
		if str.isdigit( t[0] ) : # we have a (non-negative) number
			op = 'operand'
		else:
			op = 'operator'
		li = []
		li.append(t)
		print li
		t = get_next_token()	
	print ''

---------- Post updated at 10:42 PM ---------- Previous update was at 10:22 PM ----------

hello everyone,
I was able to append the array correctly by doing the following:
Code:
#!/usr/bin/env python

from tokenizer import *

while get_expression():
	t = get_next_token()
	li = []
	while t:
		if str.isdigit( t[0] ) : # we have a (non-negative) number
			op = 'operand'
		else:
			op = 'operator'
		li.append(t)
		t = get_next_token()	
	print li
	print ''

However, I'm still having a hard time adding the string 'end' to the end of the array. Any suggestions?
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Bash to append array value to file before copying

The bash stores each uniqueid in an array and then passes them to %q to get the unique path. That seems to work what I am having trouble with is renaming each .png with the unique value in %q. I thought it was working but upon closer inspection, a .png file is being sent to scp.... but only 1 and... (21 Replies)
Discussion started by: cmccabe
21 Replies

2. Programming

Python script to run multiple command and append data in output csv file

Experts, I am writing a script and able to write only small piece of code and not able to collect logic to complete this task. In input file have to look for name like like this (BGL_HSR_901_1AG_A_CR9KTR10) before sh iss neors. Record this (BGL_HSR_901_1AG_A_CR9KTR10) in csv file Now have to... (0 Replies)
Discussion started by: as7951
0 Replies

3. Shell Programming and Scripting

Unable to print python array in shell script loop.

I am unable to loop print a python string array in my unix shell script: ~/readarr.sh '{{ myarr }}' more readarr.sh echo "Parameter 1:"$1 MYARRAY= $1 IFS= MYARRAY=`python <<< "print ' '.join($MYARRAY)"` for a in "$MYARRAY"; do echo "Printing Array: $a" done Can you... (10 Replies)
Discussion started by: mohtashims
10 Replies

4. Shell Programming and Scripting

Search and Replace+append a text in python

Hello all, I have a verilog file as following (part of it): old.v: bw_r_rf16x32 AUTO_TEMPLATE ( 1957 // .rst_tri_en (mem_write_disable), 1958 .rclk (clk), 1959 .bit_wen (dva_bit_wr_en_e), 1960 .din ... (5 Replies)
Discussion started by: Zam_1234
5 Replies

5. Shell Programming and Scripting

Append awk results into file or array

for a in {1..100} do awk '{ sum+=$a} END {print sum}' a=$a file1 > file2 done I know I will get only one number if following the code above, how can I get 100 sum numbers in file2? (2 Replies)
Discussion started by: wanliushao
2 Replies

6. Shell Programming and Scripting

Awk: Append new elements to an array

Hi all, I'm dealing with a bash script to merge the elements of a set of files and counting how many times each element is present. The last field is the file name. Sample files: head -5 *.tab==> 3J373_P15Ac1y2_01_LS.tab <== chr1 1956362 1956362 G A hom ... (7 Replies)
Discussion started by: lsantome
7 Replies

7. Shell Programming and Scripting

How to append to array within conditional block in ksh/korn shell?

Hi, I have one array created and some values are there in ksh. I want to append some other values to it based on some condition in if statement. #!/bin/ksh echo "---------------------------------------------------" set -A ipaddr_arr $(egrep -v '^#|^::|^$' /etc/hosts |awk '{print $1}'... (2 Replies)
Discussion started by: sanzee007
2 Replies

8. Shell Programming and Scripting

loop in array in python

Hi suppose in python I have a list(or array, or tuple, not sure the difference) How do I loop inside the size of array. The pseudo code is: a= for i = 1 to dim(a) print a end How to find the dimension in python? Also, anyone has a handbook to suggest so I can borrow from library (1 Reply)
Discussion started by: grossgermany
1 Replies

9. Shell Programming and Scripting

how to append into array thru awk

hey champs, i have variable as field_dtls, which has values like CLIENT ID|FAMILY NAME|MIDDLE NAME|FIRST NAME|COUNTRY NAME|ADDRESS|NATIONAL ID|PASSPORT NUMBER so, echo "$field_dtls" CLIENT ID|FAMILY NAME|MIDDLE NAME|FIRST NAME|COUNTRY NAME|ADDRESS|NATIONAL ID|PASSPORT NUMBER but i... (2 Replies)
Discussion started by: manas_ranjan
2 Replies
Login or Register to Ask a Question