Make copies of lists at certain positions with Python

Asked

Viewed 72 times

3

Hello, please, I would like to copy a certain list in python. The total list size is 150 and I wish to remove the last 50 elements from the list. Here I will represent a list 100 times smaller, ie with 15 elements and I wish to remove the last 5.

l = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]

How could I do that in a gap for? or while?

Thank you very much!

  • I posted an answer made in a pythonica way, you need it to be inside a loop or have no need ?

  • (10 times smaller )

3 answers

2


Actually to copy the last five of them to another list:

l2 = l[-5:] # l2 = [11, 12, 13, 14, 15]

To remove the last five:

del l[-5:] # l = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

1

In Python version 2.7 just do it like this

l2= l[:-5]
  • I am using Python 3. Could you explain to me what was done on this line of yours? L2 = l[:-5]? I could replace with: L2 = l[:-taxa_c]?

  • 1

    yeah vc simply says how many elements you want to remove from the end of your list/vector in this case was -5 that is to remove the last 5 elements from the list, in your question if you want to remove the last 50 just put -50

  • Understood, friend, about what I asked there to replace the -5 by a variable is possible? Is that my algorithm is variant so the rate of how much should be removed varies depending on the size of the list.

  • 1

    But of course without any problem, can put variable will work perfectly, I tested on python3 here and worked tbm :-)

  • cool, thank you very much @ederwander. Take the opportunity to ask you also, I tried to find here in the community, but I did not find the answer. If for example I have a float value: value = 50.1345 but I would like to take only the entire part of it, in this case the 50 just, doing a cast here for int(value) work sometimes, but not always. It makes my while doesn’t always work. Know how to "truncate" in 50? No convert?

  • 1

    humm yes try that import math
b=50.1345
print(math.floor(b))

  • So, if you do a b how it works there, but here in the formula it didn’t work =/ maybe it’s because of the way it’s being solved? My formula is something like: b = (tamanho_populacao * taxa)

  • 1

    Ai no longer know, has little information, formula a question with as much information as possible that makes it easier to help you

  • 1

    Take some value that is giving problem in your while to understand ...

  • hello friend, join the chat and I’ll explain =) http://chat.stackexchange.com/rooms/42602/discussion-between-allan-and-ederwander

  • 1

    but it worked out that the problem was in tamanho_populacao =)

  • What if I needed to get the top 10? What would that look like you showed me?

Show 8 more comments

0

I found a solution, but I don’t know if it’s right:

l = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

percent_l = 0.5

TAM = len(l)
taxa_c = int(percent_l * TAM)

print("taxa_c",taxa_c)

new_list = []

for i in l[0:taxa_c]:
    new_list.append(i)

print("new_list", new_list)

In that case I am calculating by a rate, how many last in percentage I would like to remove.

  • 1

    what huge code haha, take a look at my see if it works its version is 2.7?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.