Count list elements without duplicates [closed] - python-3.x

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I'm new to Python and finding my way.
I have this code here:
L = ['l','u','k','e','l','i']
k = [L.count(c) for c in L]
print(k)
And it gives this result:
[2, 1, 1, 1, 2, 1]
which is great but I want to get to this result
[2, 1, 1, 1, 1]
That is, a result in which the 'l' is not counted twice.
How do I do that?
NB: the list L here is from user input, I convert user input to list L so I can't tell beforehand what the user will input but once its typed in, I want to convert the string into a list and count all the elements without counting an element twice.

you can do something like that
L = ['l','u','k','e','l','i']
import collections
s = collections.Counter(L)
s
Counter({'l': 2, 'u': 1, 'k': 1, 'e': 1, 'i': 1})
s.values()
dict_values([2, 1, 1, 1, 1])

Use a dictionary:
L = ['l','u','k','e','l','i']
counts = {}
for l in L:
counts[l] = counts.get(l, 0) + 1
print(counts)
# {'k': 1, 'l': 2, 'e': 1, 'u': 1, 'i': 1}

Related

beginner to coding so posting question here [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 months ago.
Improve this question
the question is Given an array A of length n. we need to create a new array B of length n-2 such that B[i] = max(A[i-1], A[i], A[i+1]) except from array A and n (1-based indexing) and return the new array, first and last element of array A has to excluded
def newArray(A):
B = []
for i in range(1, len(A)-2):
B[i] = (max(A[i-1], A[i], A[i+1]))
return B
this is my approach but landed with no output
sample testcase:
input
A = [1,2,3]
output 3
explanation: max(1,2,3) is 3 so answer is 3
Consider utilizing list.append and recognizing that you need to use len(A) - 1 instead of len(A) - 2 since the stop parameter for range is exclusive:
def new_array(A: list[int]) -> list[int]:
B = []
for i in range(1, len(A) - 1):
B.append(max(A[i - 1], A[i], A[i + 1]))
return B
print(f'{new_array([1, 2]) = }')
print(f'{new_array([1, 2, 3]) = }')
print(f'{new_array([1, 2, 3, 4]) = }')
print(f'{new_array([1, 2, 3, 2]) = }')
Output:
new_array([1, 2]) = []
new_array([1, 2, 3]) = [3]
new_array([1, 2, 3, 4]) = [3, 4]
new_array([1, 2, 3, 2]) = [3, 3]

How to check a value in the list is not equal [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
How to check a value in the list is not equal.
Want to check the values that are in the list. between two items, for example, if listA does not have any of the same items as in listB removing that item
listA = [0, 1, 2, 3]
listB = [2, 3]
Result = [2, 3]
try it...
listA = [0, 1, 2, 3]
listB = [2, 3]
result = [i for i in listA if i in listB]
print(result)
result = list(set(listA).intersection(set(listB)))
use set intersection
https://www.w3schools.com/python/ref_set_intersection.asp

How to get all ordered sublists from a list? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have a list: [1,2,3,4]. It could be much longer. There will be many more combinations.
The values are less important. 1,2,3,4 is more about the sequence or index of the elements (I did not use 0 for the first element).
For now, I want to have:
4-elements: [1,2,3,4]
3-elements: [1,2,3], [1,2,4], [1,3,4], [2,3,4]
or more generally, it will be nice I can specify the number of elements of the sublists.
I'm thinking using loops, pick up each element in order. To get four-elements, will take four loops. As the number of desired elements increases, it will become unmanageable. Just wondering if there is an easier solution.
Thank you!
you could use a dict with itertools.combinations:
from itertools import combinations
l = [1, 2, 3, 4]
{i: list(combinations(l, i)) for i in range(4, 2, -1)}
output:
{4: [(1, 2, 3, 4)], 3: [(1, 2, 3), (1, 2, 4), (1, 3, 4), (2, 3, 4)]}
each key form the dict represents the number of elements from a combination

Use the same value for the next 10 occurrences in python [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I have a column with values and I need to create a new column where the first instance of 1 is repeated for the next 5 occurrences.
You can do:
x = [0,0,0,0,0,0,1,0,0,0,0,0,0,0,0]
a = x[:]
for i,j in enumerate(a):
if j!=0: x[i:i+6] = [j]*6
x
[0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0]
You will have to create a copy ie a to be used in the enumeration since x will be changing

Check a tuple for at least 5 consecutive numbers [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have a tuple A and I would like to check if it contains at least 5 consecutive numbers somewhere. What is the most time efficient way to do this?
A= (1, 4, 5, 6, 7, 8, 12) -->TRUE
This is used in a montecarlo siulation to check if a 7 card poker hand contain a straight.
Assuming a sorted tuple of 7 different integers. If there are 5 integers in series, i.e. (n, n+1, n+2, n+3, n+4), they must start on position 0, 1 or 2 and end on position 4, 5, or 6 respectively.
straight = any(a[4+i] - a[i] == 4 for i in (0,1,2))
Update: If the sequence length is not fixed:
(suggested by tobias_k in comments)
straight = any(a[4+i] - a[i] == 4 for i in range(len(a)-4))
I don't think there's a simple one-liner for this, but you basically just have to loop the elements in the list and check whether it's one more than the last one, and return True if the running count reaches 5. You also have to consider the case that there are two cards with the same value.
def has_straight(values, req=5):
last = count = None
for x in values:
if x - 1 == last:
count += 1 # one more on the straight
elif x == last:
pass # same value as before
else:
count = 1 # start a new straight
if count >= req:
return True
last = x
return False
Some examples:
has_straight((1, 4, 5, 6, 7, 10, 12)) # no straight -> False
has_straight((1, 4, 5, 6, 7, 8, 12)) # straight in middle -> True
has_straight((1, 2, 3, 4, 5, 10, 12)) # at beginning -> True
has_straight((1, 2, 8, 9, 10, 11, 12)) # at very end -> True
has_straight((1, 2, 2, 3, 4, 4, 5)) # straight with dupes -> True
Complexity would be O(n), which is as good as it could possibly get, as you have to check each number.

Resources