Line processing in a loop

I am trying to write a function that takes two string arguments and returns the number of times a character from the first line appears in the second line.

I am a complete newbie in python and in a dead end. If someone could point me in the right direction, that would be great. I was given this to start with:

def occurrences(text1, text2): """Return the number of times characters from text1 occur in text2 occurrences(string, string) -> int """ #Your code goes here 

As you can see, 2 lines are needed. I thought line 1 and line 2 would be sufficient, but I do not know how to define them.

I started from this until now, and I have not even succeeded.

 for c in "string": print c if c == char c in "string2": count += 1 

I am just throwing random variables, because how can I find char (AZ) in a string that I don't even know?

EDIT: some of the tips you guys told me I haven't learned yet. For this question, I should use:

  • for the cycle
  • in

I was also given some tips:

Tip 1. You may find in useful for testing if one line is on the other line.

Hint 2. Look at each character in the second argument and see if it is in the first argument.

+4
source share
5 answers

Start here and discuss a bit:

As you can see, 2 lines are needed. I thought line 1 and line 2 would be sufficient, but I do not know how to define them.

They are for you: they are called text1 and text2 . They come from code that calls functions. Have any functions been explained to you at some point? How do you think the function works? What does the code that cause occurrences look like?

(Hint for the last part: there is an example listed on your assignment sheet.)

Further:

 if c == char c in "string2": 

What do you expect from this? In particular, what do you expect char ? (Have you studied programming languages โ€‹โ€‹other than Python before?)

+4
source

Since this sounds like homework, I will just give you some guidance on what you need to do:

  • Find out what characters are in the string text1 . The fact is that you will not invade the same charter twice. set() can help you with this:

     >>> set('fooled') set(['d', 'e', 'l', 'o', 'f']) 

    Try playing a little with them.

  • Iterate over the set of (different) charachters from text1 . You can use .count() :

     >>> 'hello world'.count('e') 1 >>> 'hello world'.count('o') 2 

    This can count how many times a charachter happens in a row, you need to sum all these values โ€‹โ€‹and return that amount.

Note. There are many ways to do what you ask, this is only one of them (and not the best of them). If you look around the search for โ€œcount string entriesโ€ or something like this, you can find other interesting solutions :)


Another approach might be to start with text2 :

  • Iterate with a for text2 loop
  • If the char from text2 is found in text1 , than an increment of one of your total.

Update: Have you tried playing a little with them?

Check the difference between:

 >>> word = 'fooled' >>> for c in word: ... print(c) 

and

 >>> word = 'fooled' >>> for c in set(word): ... print(c) 

Cannot call inside text2.count(c) loop.

If that still doesn't make sense , I suggest reading a good Python Tutorial and come back later.

+1
source

You must put your code in a function and use the arguments that the function has. This will help you understand how it works:

 def occurrences(text1, text2): """Return the number of times characters from text1 occur in text2 occurrences(string, string) -> int """ # loop through the string in the variable `text1` for c in text1: print c # see if its in `text2` if c in text2: pass # you do the rest ;) # calls the function with 'fooled' -> text1 and 'hello world' -> text2 print occurrences('fooled', 'hello world') 
+1
source

So, you know how to iterate over characters in a string: ok.

What you are not doing is something that can count them. You need a set of counters to track each character in a string. One way to achieve this is with dict or collections.defaultdict or another class found in collections .

Use documents, Luke.

Bonus tip: you can do this (readily) O (m + n) times, on one line, using the appropriate data structures and list comprehension (google it!).

0
source
-1
source

Source: https://habr.com/ru/post/1402088/


All Articles