LinuxQuestions.org [SOLVED] How to: "myarray[interger]=myarray[integer]+1" in python3
 Programming This forum is for all programming questions. The question does not have to be directly related to Linux and any language is fair game.

 09-25-2014, 03:18 PM #1 Andy_Crowd Member   Registered: Jan 2014 Posts: 62 Rep: How to: "myarray[interger]=myarray[integer]+1" in python3 Hello! How to "myarray[interger]=myarray[integer]+1" in python3 Or better to make: dualarray[numberOne,NumberTwo]=dualarray[numberOne,NumberTwo]+1 I need to calculate similar integers in an array. I can make it in bash not in python3
 09-25-2014, 03:28 PM #2 szboardstretcher Senior Member   Registered: Aug 2006 Location: Detroit, MI Distribution: GNU/Linux systemd Posts: 4,237 Rep: Called a 'list' and you can do this: Code: ```>>> mylist=[1,2,3,4] >>> print(mylist) [1, 2, 3, 4] >>> mylist[0]+=1000 >>> print(mylist) [1001, 2, 3, 4]``` Last edited by szboardstretcher; 09-25-2014 at 03:29 PM.
 09-25-2014, 03:43 PM #3 Andy_Crowd Member   Registered: Jan 2014 Posts: 62 Original Poster Rep: But how without a predefined items in a list? mylist=[] integers=[10,20,30,10,30,20,20] D=integers[1] mylist[D]=mylist[D]+1
09-25-2014, 03:47 PM   #4
dugan
LQ Guru

Registered: Nov 2003
Distribution: distro hopper
Posts: 9,984

Rep:
Quote:
 Originally Posted by Andy_Crowd I can make it in bash not in python3
I don't understand your question. Please post the BASH script, as it would probably make it clearer.

Code:
```mylist=[]
integers=[10,20,30,10,30,20,20]

D=integers[1]
mylist[D]=mylist[D]+1```
That's valid Python. What do you need?

Last edited by dugan; 09-25-2014 at 03:52 PM.

 09-25-2014, 04:14 PM #5 Andy_Crowd Member   Registered: Jan 2014 Posts: 62 Original Poster Rep: Code: ```unset DD; AA=(11 22 11 22 11 33 11); BB=\${#AA[@]};I=0; while [ \${BB} -gt \${I} ]; do echo \${AA[I]} ; XX=\${AA[I]}; DD[XX]=\$((DD[XX]+1)); I=\$((I+1)); done; echo Total 22: \${DD[22]}; echo Total 11: \${DD[11]};``` Inline code: Code: `unset DD;AA=(11 22 11 22 11 33 11);BB=\${#AA[@]};I=0;while [ \${BB} -gt \${I} ];do echo \${AA[I]} ;XX=\${AA[I]};DD[XX]=\$((DD[XX]+1));I=\$((I+1));done;echo Total: \${DD[22]}`
 09-25-2014, 04:36 PM #6 dugan LQ Guru   Registered: Nov 2003 Location: Canada Distribution: distro hopper Posts: 9,984 Rep: Code: ```from collections import defaultdict DD = defaultdict(int) AA = (11, 22, 11, 22, 11, 33, 11) for XX in AA: print XX DD[XX] += 1 print "Total 22: ", DD[22] print "Total 11: ", DD[11]``` See: Transforming code into Beautiful, Idiomatic Python by Raymond Hettinger It covers this specific case of using dictionaries to count items (thanks to firstfire for helping me to recognize that). Last edited by dugan; 09-25-2014 at 04:56 PM.
 09-25-2014, 04:41 PM #7 firstfire Member   Registered: Mar 2006 Location: Ekaterinburg, Russia Distribution: Debian, Ubuntu Posts: 709 Rep: Hi. How about Code: ```AA = [11, 22, 11, 22, 11, 33, 11] DD = { k: AA.count(k) for k in list(set(AA)) } print(DD) print(DD[11]) print(DD[22])``` Result: Code: ```\$ python3 /tmp/test.py {33: 1, 11: 4, 22: 2} Total 22: 2 Total 11: 4```
 09-25-2014, 04:53 PM #8 Andy_Crowd Member   Registered: Jan 2014 Posts: 62 Original Poster Rep: [SOLVED], 2D array with a default dict in python3 Thanks a lot! It works perfect even with a 2 Dimension arrays in python3. Code: ```#!/bin/python3 from collections import defaultdict DD = defaultdict(lambda: 0) AA = (11, 22, 11, 11, 11, 33, 11) SS = (11, 22, 22, 22, 11, 33, 11) BB = len(AA) I = 0 while BB > I: print(AA[I],SS[I]) XX = AA[I] KK = SS[I] DD[XX,KK] = DD[XX,KK] + 1 I += 1 print( "Total 22: ", DD[11,22]) print( "Total 11: ", DD[11,11])```
 09-25-2014, 05:07 PM #9 dugan LQ Guru   Registered: Nov 2003 Location: Canada Distribution: distro hopper Posts: 9,984 Rep: I updated my post (to make the code better) while you were writing that.

 Tags array, calculate, dimension, duplicates, python3

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is Off HTML code is Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post user1x Linux - Newbie 8 04-29-2013 09:28 AM oscaringolilingo Programming 4 02-07-2011 04:37 PM christyyim Programming 4 12-23-2008 11:42 AM LXer Syndicated Linux News 0 11-30-2006 03:03 PM kpachopoulos Programming 1 02-09-2006 09:25 AM

LinuxQuestions.org

All times are GMT -5. The time now is 09:52 PM.

 Contact Us - Advertising Info - Rules - Privacy - LQ Merchandise - Donations - Contributing Member - LQ Sitemap -