Python Forum
reach the elements in the list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
reach the elements in the list
#1
Greetings .. I just started learning phyton. but I did not solve a problem .. I created a new list .. this list is contains numbers .. How can I manipulate the numbers in the list ??
barkod_numarasi=input("barkod numarası girelim")
kac_tekrar=input("kaç tekrar olsun")

while True:
    if not barkod_numarasi:
        print("boş bırakılamaz")
        break
    elif len(barkod_numarasi) < 7 or len(barkod_numarasi) > 7:
        print("barkod numarası 7 'den küçük olamaz")
        break
    else:
        print("şimdi hesaplanıyor..")
        liste = [i for i in range(int(barkod_numarasi), int(barkod_numarasi) + int(kac_tekrar))]
        print(liste)
        break
and now i created a list.. great! but how can i the manipulate the numbers?
for example:
list =[1234567,1234568,1234569]

exm . 1234567;
1*3+2+3*3+4+5*3+6+7*3
int(i[0])*3+int(i[1])+int(i[2])*3+int(i[3])+int(i[4])*3+int(i[5])+int(i[6])*3

i can't solve this. Wall
Reply
#2
I'm guessing this is for python, as I'm not sure what phyton is.  That said, you can manipulate a list's contents via the indices of the list's items:
>>> items = list(range(10))
>>> items
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> items[1] = "second element"
>>> items
[0, 'second element', 2, 3, 4, 5, 6, 7, 8, 9]
Reply
#3
if you want to get the digits of a number, there are two ways to do it. One is to convert to string, pull out the digit, and convert it back to integer:

>>> x = 12345
>>> three = str(x)[2]
>>> int(three) + 2
5
The other way is to use the modulus operator (%) which returns the remainder after division, and integer division (//)

>>> x = 12345
>>> y = x // 100
>>> y
123
>>> three = y % 10
>>> three + 2
5
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#4
(Oct-03-2017, 03:54 PM)nilamo Wrote: I'm guessing this is for python, as I'm not sure what phyton is.  That said, you can manipulate a list's contents via the indices of the list's items:
>>> items = list(range(10))
>>> items
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> items[1] = "second element"
>>> items
[0, 'second element', 2, 3, 4, 5, 6, 7, 8, 9]
yes my fault sory. *Python*..my native language is not english. i learn english at my 30 years old and sometimes I have trouble with spells.

thank you for your help but i think you didn't understand what my trouble is or i didn't explain my problem..

(Oct-03-2017, 04:19 PM)ichabod801 Wrote: if you want to get the digits of a number, there are two ways to do it. One is to convert to string, pull out the digit, and convert it back to integer:

>>> x = 12345
>>> three = str(x)[2]
>>> int(three) + 2
5
The other way is to use the modulus operator (%) which returns the remainder after division, and integer division (//)

>>> x = 12345
>>> y = x // 100
>>> y
123
>>> three = y % 10
>>> three + 2
5
if you want to get the digits of a number


hi thank you for your respond. yes i want to get the digits of a number..but i made what you said but i still can't solve the problem Wall

        liste = [i for i in range(int(barkod_numarasi), int(barkod_numarasi) + int(kac_tekrar))]
        three = str(liste)[2]
        int(three) * 120
        print(three)
        print(liste)
2
[1234567, 1234568]
liste=[1234567,12345678,12345679]

1 2 3 4 5 6 7
int(i[0])*3+int(i[1])+int(i[2])*3+int(i[3])+int(i[4])*3+int(i[5])+int(i[6])*3

I can not process for every number in the list.what code should we add after this code..
[liste = [i for i in range(int(barkod_numarasi), int(barkod_numarasi) + int(kac_tekrar))]
Reply
#5
This: int(i[0])*3+int(i[1])+int(i[2])*3+int(i[3])+int(i[4])*3+int(i[5])+int(i[6])*3 will work with the string method I showed you for getting at the integers. i just needs to be a string before you do this.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#6
(Oct-04-2017, 01:27 PM)ichabod801 Wrote: This: int(i[0])*3+int(i[1])+int(i[2])*3+int(i[3])+int(i[4])*3+int(i[5])+int(i[6])*3 will work with the string method I showed you for getting at the integers. i just needs to be a string before you do this.
it doesn't work.

liste = [i for i in range(int(barkod_numarasi), int(barkod_numarasi) + int(kac_tekrar))]
        three = str(liste[1])*3
        print(int(three))
And the result:
barkod numarası girelim1234567
kaç tekrar olsun2
şimdi hesaplanıyor..
123456812345681234568
what the fuck. I just want to 1*3.. anyway i will search . thank you
Reply
#7
No, you convert the whole number to a string. Then you can access the individual digits with slices, like i[1]. You just need to convert those individual digits back to integers with int().
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#8
(Oct-04-2017, 07:19 PM)ichabod801 Wrote: No, you convert the whole number to a string. Then you can access the individual digits with slices, like i[1]. You just need to convert those individual digits back to integers with int().

barkod_numarasi=input("barkod numarası girelim")
kac_tekrar=input("kaç tekrar olsun")

while True:
    if not barkod_numarasi:
        print("boş bırakılamaz")
        break
    elif len(barkod_numarasi) < 7 or len(barkod_numarasi) > 7:
        print("barkod numarası 7 'den küçük olamaz")
        break
    else:
        print("şimdi hesaplanıyor..")
        liste = [str(i) for i in range(int(barkod_numarasi), int(barkod_numarasi) + int(kac_tekrar))]
        for i in liste:
            print(int(i[6]))

        break
barkod numarası girelim1234567
kaç tekrar olsun2
şimdi hesaplanıyor..
7
8
hey.. it's working..thank you so much. Doh Big Grin Blush
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  help with adding duplicates elements together in a list 2ECC3O 5 2,026 Sep-10-2022, 07:11 AM
Last Post: 2ECC3O
  Unexpected behavior accessing list elements. tonyflute 2 2,259 Apr-09-2021, 02:36 PM
Last Post: tonyflute
  How to find difference between elements in a list? Using beginner Basics only. Anklebiter 8 4,335 Nov-19-2020, 07:43 PM
Last Post: Anklebiter
  Get 5 most unique combinations of elements in a 2D list wanttolearn 1 2,310 Sep-24-2020, 02:26 PM
Last Post: buran
  Loop through elements of list and include as value in the dictionary Rupini 3 2,644 Jun-13-2020, 05:43 AM
Last Post: buran
  How can I print the number of unique elements in a list? AnOddGirl 5 3,264 Mar-24-2020, 05:47 AM
Last Post: AnOddGirl
  Extracting elements in a list to form a message using for loop Tony04 2 2,358 Oct-25-2019, 05:55 PM
Last Post: ichabod801
  Sum of elements on the list recursive sev 3 2,558 Jun-20-2019, 02:14 PM
Last Post: sev

Forum Jump:

User Panel Messages

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