Python Forum
How to get first line of a tuple and the third item in its tuple. Need Help, Anybody?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to get first line of a tuple and the third item in its tuple. Need Help, Anybody?
#1
I have these values which I store in a:
a = ((1.425, 4.82145395112958, 0.0656498375731458)
    (1.39716316090366, 7.2, 0.102480129941483)
    (7.425, 4.82145395112958, 0.0656498375731458)
    (7.39716316090366, 7.2, 0.102480129941483)
    (7.425, -0.928546048870416, 0.0656498375731458)
    (7.39716316090366, 0.95, 0.102480129941483)
    (1.425, -0.928546048870416, 0.0656498375731458)
    (1.39716316090366, 0.95, 0.102480129941483))
what I'm doing:
print a[2]
Gives:
Output:
0.0656498375731458 0.102480129941483 0.0656498375731458 0.102480129941483 0.0656498375731458 0.102480129941483 0.0656498375731458 0.102480129941483
I only need this single value to print: 0.0656498375731458
Reply
#2
Your sample of a is not a valid tuple in a tuple(missing , seperators), printing a[2] would not give the results shown.

a = ((1.425, 4.82145395112958, 0.0656498375731458),
    (1.39716316090366, 7.2, 0.102480129941483),
    (7.425, 4.82145395112958, 0.0656498375731458),
    (7.39716316090366, 7.2, 0.102480129941483),
    (7.425, -0.928546048870416, 0.0656498375731458),
    (7.39716316090366, 0.95, 0.102480129941483),
    (1.425, -0.928546048870416, 0.0656498375731458),
    (1.39716316090366, 0.95, 0.102480129941483))

print(a[0][2])
would give
Output:
0.0656498375731458
buy the first [0] would get the first tuple and the second [2] would get its 3rd item.
Reply
#3
error:
Traceback (most recent call last):
File "D:\Py\check cartesian.py", line 26, in <module>
print a[0][2]
TypeError: 'float' object has no attribute '__getitem__'
Reply
#4
Please, use BBcode as advised.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
Error:
Traceback (most recent call last): File "D:\Py\check cartesian.py", line 26, in <module> print a[0][2] TypeError: 'float' object has no attribute '__getitem__'
Reply
#6
Hello,

Can you send all code in module, please?

Best Regards,

Nicolas TATARENKO
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  using > < for tuple , list,... akbarza 3 400 Feb-05-2024, 01:18 PM
Last Post: deanhystad
Sad UnpicklingError: NEWOBJ expected an arg tuple. biyazelnut 0 408 Dec-20-2023, 03:16 AM
Last Post: biyazelnut
  Searche each element of each tuple based 3 numbes zinho 8 779 Dec-11-2023, 05:14 PM
Last Post: zinho
  tuple indices must be integers or slices, not str cybertooth 16 11,077 Nov-02-2023, 01:20 PM
Last Post: brewer32
Question mypy unable to analyse types of tuple elements in a list comprehension tomciodev 1 427 Oct-17-2023, 09:46 AM
Last Post: tomciodev
  Change font in a list or tuple apffal 4 2,635 Jun-16-2023, 02:55 AM
Last Post: schriftartenio
  search in dict inside tuple steg 1 646 Mar-29-2023, 01:15 PM
Last Post: rob101
Exclamation IndexError: Replacement index 2 out of range for positional args tuple - help? MrKnd94 2 5,958 Oct-14-2022, 09:57 PM
Last Post: MrKnd94
  Pulling username from Tuple pajd 21 3,223 Oct-07-2022, 01:33 PM
Last Post: pajd
  how to convert tuple value into string mg24 2 2,237 Oct-06-2022, 08:13 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

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