Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Question on Tuple
#1
Hello Users,

I have my desired Tuple (see below) using the code mentioned. But I was wondering if there is any other clean way to obtain the same Output? (without using For/While Loops)

Desired Output:

((5, 5, 5, 5, 5, 5), (5, 5, 5, 5, 5, 5), (5, 5, 5, 5, 5, 5), (5, 5, 5, 5, 5, 5), (5, 5, 5, 5, 5, 5), (5, 5, 5, 5, 5, 5))

Used Code:

Ar_tuple=()
Mat_tuple=()
 
i=0
while i<6:
    Ar_tuple=(Ar_tuple + (5,)) 
    i=i+1                

j=0
while j<6:
    Mat_tuple=((Mat_tuple)+(Ar_tuple,)) 
    j=j+1
    
print(Mat_tuple)
Best Regards
Reply
#2
((5,) * 5,) * 6
Note that this does not work well with lists. Lists are mutable, so instead of a list of six distinct sub-lists you will get a list of six references to the same list. It works with tuples because they are immuntable.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
lcome to Termux!

Wiki:            https://wiki.termux.com
Community forum: https://termux.com/community
IRC channel:     #termux on freenode
Gitter chat:     https://gitter.im/termux/termux
Mailing list:    [email protected]

Search packages:   pkg search <query>
Install a package: pkg install <package>
Upgrade packages:  pkg upgrade
Learn more:        pkg help
$ ipython
Python 3.7.2 (default, Jan 16 2019, 21:05:09)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.2.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: from itertools import repeat

In [2]: list(repeat(repeat(5,3), 4))
Out[2]: [repeat(5, 3), repeat(5, 3), repeat(5, 3), repeat(5, 3)]

In [3]: list(repeat(tuple(repeat(5,3)), 4))
Out[3]: [(5, 5, 5), (5, 5, 5), (5, 5, 5), (5, 5, 5)]

In [4]:
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  code with no tuple gets : IndexError: tuple index out of range Aggam 4 2,806 Nov-04-2020, 11:26 AM
Last Post: Aggam
  How to get first line of a tuple and the third item in its tuple. Need Help, Anybody? SukhmeetSingh 5 3,188 May-21-2019, 11:39 AM
Last Post: avorane
  Question about doc strings and tuple separator ? mcgrim 1 1,908 Mar-20-2019, 12:57 PM
Last Post: Yoriz
  Newbie question for sum the specific element in named tuple zydjohn 1 4,204 Dec-12-2017, 07:29 PM
Last Post: buran
  Newbie question for sorting named tuple list zydjohn 3 7,730 Dec-11-2017, 10:13 PM
Last Post: zydjohn

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020