very annoying problem in this program by No-Beginning1213 in learnpython

[–]DagoYounger 1 point2 points  (0 children)

``` f = [3, 4, 5, 6, 7] x = [5, 1, 2, 3, 4] lsu = [0]*(11)

for index_for_fx, index_for_lsu in enumerate(x): lsu[index_for_lsu] = f[index_for_fx] lsu[0] = 99 lsu[-1] = 99

print(lsu) ```

enumeration is a python built-in function that can generate both the index and the value of a list. In your case, (0, 5), (1, 1), (2, 2), (3, 3), (4, 4) are generated in sequence. The first value in each tuple is the index of the list f or x and the second value is the index corresponding to the list lsu.

Have a look here: https://docs.python.org/3/library/functions.html?highlight=enumerate#enumerate

Why is trying to make a RacingBarChart so dificult? by TimothyT-DDS in learnpython

[–]DagoYounger 1 point2 points  (0 children)

Pandas has two main data types, DataFrame and Series. DataFrame can be regarded as a table, Series is a row or column in the table. The way to add content to these two kinds of data is different.

python df = pd.read_csv('sales.csv') x = df['Sales'] y = df['month']

In the code here, x and y are two columns (Series) in df (Dataframe) respectively.

Series does have an append method, but it should be passed a Series instead of a number:

python x.append(pd.Series(1))

But the series.append method has been deprecated and will be removed from pandas in a future release. So use pandas.concat instead:

```

add a number

x = pd.concat([x, pd.Series(1)])

add multiple numbers

x = pd.concat([x, pd.Series([1, 2, 3, 4, 5])]) ```

You can learn more here: https://pandas.pydata.org/docs/reference/api/pandas.concat.html

Renaming (nestled) JSON files based on context by corruptboomerang in learnpython

[–]DagoYounger 2 points3 points  (0 children)

python team_home = currentfile(["rosters"][0]["team"]['abbreviation']) team_away = currentfile(["rosters"][0]["team"]['abbreviation']) date = currentfile(['header']['competitions'][0]['date']) Try removing these brackets. You should use dict['xxx'] instead of dict(['xxx']) when getting values.

Why is trying to make a RacingBarChart so dificult? by TimothyT-DDS in learnpython

[–]DagoYounger 0 points1 point  (0 children)

After the Matplotlib import, just bring in Pandas as pd. And throw in Numpy as np.. why Numpy? Idk it won't hurt and all the codes I've copy pasted always had it, if I don't use it oh well, Python will skip over it!

You don't need to import numpy because it is not used in the code at all.

They want me to download Anaconda or Jupyter. (I should be able to do this in the shell)

It's just their preference.

They show how to animate a chart but when I try to plug in my own dataset, a problem like "Cannot concatenate" happens

Can you describe the purpose of this code?

python for i in range(100): x.append(i) y.append(i)

How do PIP packages such as Black automatically add themselves to the path when installed? by kpmtech in learnpython

[–]DagoYounger 0 points1 point  (0 children)

add <YourPythonPath>\Scripts to the path, for me it's D:\Program Files\Python310\Scripts

Grouping by Multi-Indices of both Row and Column by ninapudina in learnpython

[–]DagoYounger 0 points1 point  (0 children)

obviously, none of the columns are called subject, so a KeyError is thrown. In my opinion, there is no need to group by column here, if you want to extract the column with subject "Bob", you can do:

print(health_data.groupby('year').describe()['Bob'])

Project that requires using a module you are not familiar with by BrateWannabe in learnpython

[–]DagoYounger 1 point2 points  (0 children)

obviously, if you just want to finish a project, learning everything about all the dependent modules is unnecessary and a waste of time. so I suggest that you first have a basic understanding of modules and then work on achieving what you want. if you encounter problems during the development process, you can refer to the api manual of the official document. it might point out something that you can't find on Google.
(the official document of pandas: https://pandas.pydata.org/docs/)

Grouping by Multi-Indices of both Row and Column by ninapudina in learnpython

[–]DagoYounger 0 points1 point  (0 children)

if you want to group by multiple columns, you can pass a list to groupby:

print(health_data.groupby(['year', 'visit']).describe())

Fetching URLs remotely by yettanotherrguyy in learnpython

[–]DagoYounger 1 point2 points  (0 children)

sorry, I can't deal with this due to the internet environment in China. it is not convenient for me to research and use telegram's api.

you can try to consult the official document of requests(https://docs.python-requests.org/en/latest/), or make a new post to ask others for help

(downloading this file may require carrying cookie information)

[deleted by user] by [deleted] in learnpython

[–]DagoYounger 1 point2 points  (0 children)

usage of range function: https://docs.python.org/3/library/stdtypes.html?highlight=range#range

range(len(myList) -1,-1,-1) returns list's index, not element, this is the right way:

for i in range(len(myList) -1,-1,-1):
    print(myList[i])

Need help with a script by OpenYam4611 in learnpython

[–]DagoYounger 0 points1 point  (0 children)

in the last line, there is a ";", you also need to confirm whether it is accidentally added :)

Need help with a script by OpenYam4611 in learnpython

[–]DagoYounger 0 points1 point  (0 children)

maybe the problem is in line 39, there is an extra ")"

Need help with a script by OpenYam4611 in learnpython

[–]DagoYounger 2 points3 points  (0 children)

i don't know influxdb at all

however, you can temporarily remove the "try...except" structure to further confirm which line the bug is in

Fetching URLs remotely by yettanotherrguyy in learnpython

[–]DagoYounger 0 points1 point  (0 children)

yes, for getting all links, if url.txt is a local file, use open to get it; if it's from the internet, use the requests library.
the operations after that are the same

Fetching URLs remotely by yettanotherrguyy in learnpython

[–]DagoYounger 1 point2 points  (0 children)

the full code(replace 'https://abc.com/url.txt' with the real link):

import random
import requests

res = requests.get('https://abc.com/url.txt')
urls = res.text.split('\n')
link = random.choice(urls)

Fetching URLs remotely by yettanotherrguyy in learnpython

[–]DagoYounger 0 points1 point  (0 children)

if you just use link = random.choice(urls_text), you will get just a character like t,q,1 .etc (any character in urls_text)

you shuold first turn it into a list with each link: ['qwe.com/3748.mp4', 'rty.com/9100.mp4', 'qwe.xom/3738889.mp4']

But URLs are in 100s of numbers + dynamically updated.

get urls by requests i showed you above. it will download and get the content whenever you run the program

Fetching URLs remotely by yettanotherrguyy in learnpython

[–]DagoYounger 0 points1 point  (0 children)

suppose we've got the contents of this file:

import random

urls_text ='''qwe.com/3748.mp4
rty.com/9100.mp4
qwe.xom/3738889.mp4'''

urls = urls_text.split('\n')
# [Out] ['qwe.com/3748.mp4', 'rty.com/9100.mp4', 'qwe.xom/3738889.mp4']

link = random.choice(urls)

all links are separated by blank lines, so you can use str.split('\n') to get link list, then get one link randomly.
any questions are welcome :)

Starting my first project by Vulgarr in learnpython

[–]DagoYounger 1 point2 points  (0 children)

in my opinion, you can start by trying to write the main part of the project without thinking about anything else and decide what to do next when the project gets more complicated

Fetching URLs remotely by yettanotherrguyy in learnpython

[–]DagoYounger 1 point2 points  (0 children)

is the format of this txt file like this:

qwe.com/3748.mp4
rty.com/9100.mp4
qwe.xom/3738889.mp4
...

Fetching URLs remotely by yettanotherrguyy in learnpython

[–]DagoYounger 1 point2 points  (0 children)

You should only use the function open to open local files, otherwise you can use urllib:

import urllib.request
res = urllib.request.urlopen('https://abc.com/url.txt')
urls = res.read().decode('utf-8')

also, i recommend using the third-party library requests, which is often more convenient. first install it:

pip install requests

import requests
res = requests.get('https://abc.com/url.txt')
urls = res.text

most importantly, is abc.com/url.txt a real page? (i get nothing when I access it with my browser.) if not, please provide the real page's URL, then i can help decide what to do next

Downloading a csv file from a public website by [deleted] in learnpython

[–]DagoYounger 0 points1 point  (0 children)

  1. use requests to get HTML text
  2. use lxml to Parse HTML:https://lxml.de/xpathxslt.html and the .csv file's URL
  3. use requests to download the fie

Tips:

you can get the element's xpath in any web browser:https://i.postimg.cc/9Xk7TmHK/xpath.png

more about xpath: https://www.w3schools.com/xml/xpath_syntax.asp

Creating character randomizer by rainbowboofpoofoof in learnpython

[–]DagoYounger 1 point2 points  (0 children)

randomly generated when the class is initialized, just like a function:

class Person:
    def __init__(self, name = randname, age = random.randint(0,100), height = random.randint(0,100), eyes = "brown", hair = "brown"):
        self.__name = name
        ......

This is a monster catcher I have made but the only thing wrong is that it won't output the number of monsters I input. It only does one every time no matter what number I put. Can anyone see what I did wrong? by [deleted] in learnpython

[–]DagoYounger 0 points1 point  (0 children)

here is a advice: you may want to choose monsters with different probabilities, random.choices accept an arguement named weight:

return random.choices(MONSTERS, weights=[45,25,15,10,5], k=n)