Related
From a list of numbers, I'd like to randomly create 5 lists of 5 numbers.
import random
def team():
x = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
y = []
Count = 0
count = 0
while Count < 5:
while count<5:
count += 1
z = random.choice(x)
y += z
x.remove(z)
print(y)
Count +=1
team()
Output:
Expected Output:
I want something like 5 groups of non-repeating numbers
change the code to
import random
def team():
Count = 0
while Count < 5:
x = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
y = []
count = 0
while count<5:
count += 1
z = random.choice(x)
y += z
x.remove(z)
print(y)
Count +=1
team()
in fact, for your original code, the count becomes 5 and break the second for loop. but next time, your count is still 5, so it doesn't go into the second for loop.
You just print the first got y five 5 times.:)
This is a one-liner if you use a nested list comprehension. Also we can just directly sample the integers 0..9 instead of your list of string ['0', '1',...,'9']
import random
teams = [[random.randrange(10) for element in range(5)] for count in range(5)]
# Here's one example run: [[0, 8, 5, 6, 2], [6, 7, 8, 6, 6], [8, 8, 6, 2, 2], [0, 1, 3, 5, 8], [7, 9, 4, 9, 6]]
or if you really want string output instead of ints:
teams = [[str(random.randrange(10)) for element in range(5)] for count in range(5)]
[['3', '8', '2', '5', '3'], ['7', '1', '9', '7', '9'], ['4', '8', '0', '4', '1'], ['7', '6', '5', '8', '2'], ['6', '9', '2', '7', '3']]
or if you really want to randomly sample an arbitrary list of strings:
[ random.sample(['0','1','2','3','4','5','6','7','8','9'], 5) for count in range(5) ]
[['7', '1', '5', '3', '0'], ['7', '1', '6', '2', '8'], ['3', '0', '9', '7', '2'], ['5', '1', '7', '3', '2'], ['6', '2', '5', '0', '3']]
I have user entered numbers but when I entered in descending order then only list is sorted else it's not.
values = input("Enter the number")
values1 = input("Enter the number")
values2 = input("Enter the number")
templist = values.split(","),values1.split(","),values2.split(",")
print('List : ',templist)
finallist = sorted(templist)
print('Final List : ',finallist)
When you use sorted(templist) you tuple of lists gets sorted and not the inner list!
You need to iterate through your inner list and sort them separately like,
map(sorted,templist)
or
[sorted(l) for l in templist]
That is,
>>> values = input("Enter the number")
Enter the number"5,4,3,2,1"
>>> values1 = input("Enter the number")
Enter the number"9,8,7,6"
>>> values2 = input("Enter the number")
Enter the number"8,6,4,2"
>>> templist = values.split(","),values1.split(","),values2.split(",")
>>> templist
(['5', '4', '3', '2', '1'], ['9', '8', '7', '6'], ['8', '6', '4', '2'])
>>> print('List : ',templist)
('List : ', (['5', '4', '3', '2', '1'], ['9', '8', '7', '6'], ['8', '6', '4', '2']))
>>> sorted(templist)
[['5', '4', '3', '2', '1'], ['8', '6', '4', '2'], ['9', '8', '7', '6']]
>>>
>>> map(sorted,templist)
[['1', '2', '3', '4', '5'], ['6', '7', '8', '9'], ['2', '4', '6', '8']]
>>>
>>> #or
...
>>> [sorted(l) for l in templist]
[['1', '2', '3', '4', '5'], ['6', '7', '8', '9'], ['2', '4', '6', '8']]
This is sorting list of strings.
If you want to sort list of integers,
>>> [sorted(int(n) for n in l) for l in templist]
[[1, 2, 3, 4, 5], [6, 7, 8, 9], [2, 4, 6, 8]]
I want my code to cycle through each item of the list and convert it from str to int but it only converts half of the list and in an irregular order.
My code:
for item in list:
list.append(int(item))
list.remove(item)
print (list)
For example if list is ['5', '6', '3', '5', '6', '2', '6', '8', '5', '4', '2', '8']
The final would be ['6', '8', '5', '4', '2', '8', 3, 6, 2, 6, 5, 5]
Which is only half converted and not in order.
I could do it another way but that is a lot longer so would like to fix this and add to my knowledge about for loops.
My knowledge and experience with Python is tiny, so I most probably won't understand unless it's really basic and jargon is explained.
Using list comprehension:
l = ['5', '6', '3', '5', '6', '2', '6', '8', '5', '4', '2', '8']
output = [int(i) for i in l]
print(output)
[5, 6, 3, 5, 6, 2, 6, 8, 5, 4, 2, 8]
If you don't understand list comprehension you could use simple for loop:
l1 = []
for i in l:
l1.append(int(i))
print(l1)
[5, 6, 3, 5, 6, 2, 6, 8, 5, 4, 2, 8]
Both answers above are good but why your code didn't work also should be adressed.
First , you are changing the list while you are iterating on it. This is something you should not do. It will probably cause problems, like in your question.
Second, remove method removes the first element in the list that it encounters which fits the given argument, it also should be used with care.
Third, you should not use list as an variable name. As it is a built-in class.
for item in list:
print (list)
list.append(int(item))
list.remove(item)
# Prints
['5', '6', '3', '5', '6', '2', '6', '8', '5', '4', '2', '8']
['6', '3', '5', '6', '2', '6', '8', '5', '4', '2', '8', 5]
['6', '5', '6', '2', '6', '8', '5', '4', '2', '8', 5, 3]
['5', '6', '2', '6', '8', '5', '4', '2', '8', 5, 3, 6]
['5', '2', '6', '8', '5', '4', '2', '8', 5, 3, 6, 6]
['2', '6', '8', '5', '4', '2', '8', 5, 3, 6, 6, 5]
['6', '8', '5', '4', '2', '8', 5, 3, 6, 6, 5, 2]
['6', '8', '5', '4', '2', '8', 3, 6, 6, 5, 2, 5]
['6', '8', '5', '4', '2', '8', 3, 6, 5, 2, 5, 6]
['6', '8', '5', '4', '2', '8', 3, 6, 2, 5, 6, 5]
['6', '8', '5', '4', '2', '8', 3, 6, 2, 6, 5, 5]
['6', '8', '5', '4', '2', '8', 3, 6, 2, 6, 5, 5]
['6', '8', '5', '4', '2', '8', 3, 6, 2, 6, 5, 5]
As you see, not working as expected
Why don't you use something like this:
l = list(map(int, l))
It simply calls function int on each item from l.
Here's doc.
While the other two answers give you better ways of converting your list of strings to integers, they really don't answer your question. Your main problem is that you are mutating (altering) the list structure while your for loop operates on it. You should not mutate the list structure (remove elements or append) because the loop iteration variable item gets out of sync. There's no way to re-sync item to the new list structure.
BTW: It's not a random order. It's every other item.
You could write your conversion loop like so, because you're not mutating the structure of the list, only the individual elements:
for i in xrange(len(l)):
l[i] = int(l[i])
Don't write it like this:
for item in l:
item = int(item)
It doesn't mutate the individual list elements, even though you would think that it does. It has to do with how Python iterators work.
Try this:
>>> list = ['5', '6', '3', '5', '6', '2', '6', '8', '5', '4', '2', '8']
>>> for item in list[:]:
... list.append(int(item))
... list.remove(item)
...
>>> print(list)
[5, 6, 3, 5, 6, 2, 6, 8, 5, 4, 2, 8]
Explanation: Here we are iterating over a clone of the list but doing operations on the original list.
PS: list is a keyword in python. Its usage as a variable name should be avoided.
I have a list of strings that look like this, and I need to add the first, second, and third entry of each to a integer list a b and c accordingly
print cuts gives me
[['3', '5', '10'], ['2', '8', '15'], ['7', '9', '25'], ['4', '6', '20'], ['9', '12', '50'], ['5', '7', '22'], ['3', '8', '17'], ['6', '9', '24'], ['8', '11', '40'], ['7', '10', '30'], []]
such that
a = [3,2,7, ... , 7]
b = [5, 8, 9, ... , 10]
c = [10, 15, ... , 30]
(Also want these to be ints not strings)
I tried to delimit by
cuts[i] = cuts[i].strip(",")
thinking that it would give me [[3] [5] [10] ] which would let me add with a for loop but python told me lists don't have a strip attribute
Here are list comprehensions in basic form:
data = [['3', '5', '10'], ['2', '8', '15'], ['7', '9', '25'], ['4', '6', '20'],
['9', '12', '50'], ['5', '7', '22'], ['3', '8', '17'], ['6', '9', '24'],
['8', '11', '40'], ['7', '10', '30'], []]
a = [int(x[0]) for x in data if len(x) >= 1]
b = [int(x[1]) for x in data if len(x) >= 2]
c = [int(x[2]) for x in data if len(x) >= 3]
print a
print b
print c
Or, to keep them in a 2-dimensional list:
abc = [[int(x[i]) for x in data if len(x) >= i+1] for i in range(len(data[0]))]
print abc
Output:
[[3, 2, 7, 4, 9, 5, 3, 6, 8, 7],
[5, 8, 9, 6, 12, 7, 8, 9, 11, 10],
[10, 15, 25, 20, 50, 22, 17, 24, 40, 30]]
This can be done using the built in zip function.
a, b, c = zip(*cuts)
You can read about zip here. The gist is here -
this function returns a list of tuples, where the i-th tuple contains
the i-th element from each of the argument sequences or iterables
If you then want your lists to hold numbers instead of strings, you can map them over the int function.
a = map(int, a)
b = map(int, b)
c = map(int, c)
I'm assuming that the empty list at the end of cuts is a typo -- otherwise your requirements don't quite make sense since there isn't a first, second and third element of an empty list. You could always use
cuts = [cut for cut in cuts if len(cut) > 0]
if you want to cut cuts down to size.
In any event, once that empty list is removed, what you are asking for is a sort of zip and can be done like thus:
>>> cuts = [['3', '5', '10'], ['2', '8', '15'], ['7', '9', '25'], ['4', '6', '20'], ['9', '12', '50'], ['5', '7', '22'], ['3', '8', '17'], ['6', '9', '24'], ['8', '11', '40'], ['7', '10', '30']]
>>> a,b,c = ([int(s) for s in strings] for strings in zip(*cuts))
>>> a
[3, 2, 7, 4, 9, 5, 3, 6, 8, 7]
>>> b
[5, 8, 9, 6, 12, 7, 8, 9, 11, 10]
>>> c
[10, 15, 25, 20, 50, 22, 17, 24, 40, 30]
I have the following list of strings of integers:
li = ['1', '2', '3', '4', '5', '6', '7']
What does my code need to be for my output to be the following?:
1 2 3 4 5 6 7
I tried doing both:
" ".join(str(val) for val in li)
and
" ".join(li)
but both of them don't work.
I want to get rid of the brackets, the quotation marks, and the commas.
You can use map to apply int() to every element in the list:
map(int, ['1', '2', '3', '4', '5', '6', '7']) # [1, 2, 3, 4, 5, 6, 7]
If you just want to print the numbers as a string, you can simply do:
' '.join(['1', '2', '3', '4', '5', '6', '7']) # 1 2 3 4 5 6 7