Find the 1st occurence of elements in a list of strings, based on first character in the string

newbie

I am new to python. I am trying to get the 1st number in a list of strings that are numbers in a series, that start with a common number.

ex:

x = ['512','345','321','345','674','132','231','145','214','576']

my expected output:

result = ['512','345','674','132','231']

i.e say for example in all numbers starting with 1 in list, I should get the 1st number likewise for all other numbers.

I could use for loop twice to get it. but i want to know is there any better way of doing it.

NOTE: the list is a list of strings which are numbers given was just an example.

styvane

You can use a generator function to do this. In the function you just iterate your initial list and for each element check if there is another element that starts with the same character already exists in a set of string. If no match found, just yield that element.

>>> def get_num(lst):
...     found = set()
...     for element in lst:
...         if not any(item.startswith(element[0]) for item in found):
...             found.add(element)
...             yield element
... 
>>> x = ['512','345','321','345','674','132','231','145','214','576']
>>> list(get_num(x))
['512', '345', '674', '132', '231']

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Find the 1st occurence of elements in a list of strings, based on first character in the string

From Dev

Find string in a list of strings with 1 character difference

From Dev

How to find first occurence of character (not from the begining of the string) in cpp ?

From Dev

split string only on first occurence of specified character

From Dev

How to split a String based on first occurence?

From Dev

Check text/string for occurence of predefined list elements

From Dev

SQL How to return a certain string the 1st time you find a certain character/string

From Dev

Find every occurence of string in a file and store it in a List

From Dev

How to replace occurence of a character in a string except the first one

From Dev

Compare list of strings and string to find number of consecutive character matches

From Dev

finding the first occurence in a list

From Dev

List the first occurence in a sample

From Dev

ksh shell script to find first occurence of _ in string and remove everything until that

From Dev

Find First character in string that is a letter

From Dev

Find the first character in an array string

From Dev

Replacing a character in a elements of string list

From Dev

Filter character vector based on first two elements

From Dev

Getting only 1st elements of list using sapply

From Dev

RegEx : Find match based on 1st two chars

From Dev

Getting before first string occurence

From Dev

Separate row based on the same string in the 1st column in matlab

From Dev

Ignore the first occurence of a character if it exists later

From Dev

Replace only first occurence of a character in filenames

From Dev

Using Lists to Remove First Occurence of a Duplicate Character

From Dev

Javascript Regex first character after occurence

From Dev

Using Lists to Remove First Occurence of a Duplicate Character

From Dev

Replacing just the first occurence of a character in a line

From Dev

grep match till the first occurence of a character

From Dev

How to remove a character from every string in a list, based on the position where a specific character occurs in the first member of said list?

Related Related

  1. 1

    Find the 1st occurence of elements in a list of strings, based on first character in the string

  2. 2

    Find string in a list of strings with 1 character difference

  3. 3

    How to find first occurence of character (not from the begining of the string) in cpp ?

  4. 4

    split string only on first occurence of specified character

  5. 5

    How to split a String based on first occurence?

  6. 6

    Check text/string for occurence of predefined list elements

  7. 7

    SQL How to return a certain string the 1st time you find a certain character/string

  8. 8

    Find every occurence of string in a file and store it in a List

  9. 9

    How to replace occurence of a character in a string except the first one

  10. 10

    Compare list of strings and string to find number of consecutive character matches

  11. 11

    finding the first occurence in a list

  12. 12

    List the first occurence in a sample

  13. 13

    ksh shell script to find first occurence of _ in string and remove everything until that

  14. 14

    Find First character in string that is a letter

  15. 15

    Find the first character in an array string

  16. 16

    Replacing a character in a elements of string list

  17. 17

    Filter character vector based on first two elements

  18. 18

    Getting only 1st elements of list using sapply

  19. 19

    RegEx : Find match based on 1st two chars

  20. 20

    Getting before first string occurence

  21. 21

    Separate row based on the same string in the 1st column in matlab

  22. 22

    Ignore the first occurence of a character if it exists later

  23. 23

    Replace only first occurence of a character in filenames

  24. 24

    Using Lists to Remove First Occurence of a Duplicate Character

  25. 25

    Javascript Regex first character after occurence

  26. 26

    Using Lists to Remove First Occurence of a Duplicate Character

  27. 27

    Replacing just the first occurence of a character in a line

  28. 28

    grep match till the first occurence of a character

  29. 29

    How to remove a character from every string in a list, based on the position where a specific character occurs in the first member of said list?

HotTag

Archive