LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 09-25-2014, 03:18 PM   #1
Andy_Crowd
Member
 
Registered: Jan 2014
Posts: 62

Rep: Reputation: Disabled
Question 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
 
Old 09-25-2014, 03:28 PM   #2
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,237

Rep: Reputation: 1656Reputation: 1656Reputation: 1656Reputation: 1656Reputation: 1656Reputation: 1656Reputation: 1656Reputation: 1656Reputation: 1656Reputation: 1656Reputation: 1656
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.
 
Old 09-25-2014, 03:43 PM   #3
Andy_Crowd
Member
 
Registered: Jan 2014
Posts: 62

Original Poster
Rep: Reputation: Disabled
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
 
Old 09-25-2014, 03:47 PM   #4
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 9,984

Rep: Reputation: 4570Reputation: 4570Reputation: 4570Reputation: 4570Reputation: 4570Reputation: 4570Reputation: 4570Reputation: 4570Reputation: 4570Reputation: 4570Reputation: 4570
Quote:
Originally Posted by Andy_Crowd View Post
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.
 
Old 09-25-2014, 04:14 PM   #5
Andy_Crowd
Member
 
Registered: Jan 2014
Posts: 62

Original Poster
Rep: Reputation: Disabled
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]}
 
Old 09-25-2014, 04:36 PM   #6
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 9,984

Rep: Reputation: 4570Reputation: 4570Reputation: 4570Reputation: 4570Reputation: 4570Reputation: 4570Reputation: 4570Reputation: 4570Reputation: 4570Reputation: 4570Reputation: 4570
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.
 
Old 09-25-2014, 04:41 PM   #7
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
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
 
Old 09-25-2014, 04:53 PM   #8
Andy_Crowd
Member
 
Registered: Jan 2014
Posts: 62

Original Poster
Rep: Reputation: Disabled
Thumbs up [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])
 
Old 09-25-2014, 05:07 PM   #9
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 9,984

Rep: Reputation: 4570Reputation: 4570Reputation: 4570Reputation: 4570Reputation: 4570Reputation: 4570Reputation: 4570Reputation: 4570Reputation: 4570Reputation: 4570Reputation: 4570
I updated my post (to make the code better) while you were writing that.
 
  


Reply

Tags
array, calculate, dimension, duplicates, python3


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] How to change the delimiter when I do a "${myarray[@]}" in Bash? user1x Linux - Newbie 8 04-29-2013 09:28 AM
How come *++argv works and *++myarray doesn't? oscaringolilingo Programming 4 02-07-2011 04:37 PM
"warning: assignment makes integer from pointer without a cast" in C christyyim Programming 4 12-23-2008 11:42 AM
LXer: Title: Linux Kernel "get_fdb_entries()" Integer Overflow ... LXer Syndicated Linux News 0 11-30-2006 03:03 PM
"packed" byte integer,etc kpachopoulos Programming 1 02-09-2006 09:25 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration